struct tm * localtime(const time_t *timep)
1.0、参考
1.1、此函数的作用

获得当地时间。

1.2、参数说明

time_t的定义:

typedef long int __time_t;
typedef __time_t time_t;

time_t表示从1970-01-01 00:00:00,到某个时间点,所经过的数, 它是相对时间。

time_t是一个有符号数,也就是它也可以是负数,表示1970-01-01 00:00:00之前的时间。

1.3、返回值说明

struct tm的定义:

struct tm {
    int tm_sec;   /* seconds [0,60] */
    int tm_min;   /* minutes [0,59] */
    int tm_hour;  /* hours   [0,23]*/
    int tm_mday;  /* day of the month [1,31] */
    int tm_mon;   /* month   [0,11] */
    int tm_year;  /* year since 1900 */
    int tm_wday;  /* day of the week [0,6] (Sunday =0) */
    int tm_yday;  /* day in the year [0,365] */
    int tm_isdst; /* daylight saving time */
};
1.4、使用示例
#include <stdio.h>
#include <time.h>

int main() {
    const char* week[] = {"日", "一", "二", "三", "四", "五", "六"};

    time_t tt = time(NULL);
    struct tm *tms = localtime(&tt);

    printf("现在是当地时间:%d年%d月%d日 星期%s %02d:%02d:%02d\n", 1900 + tms->tm_year, 1 + tms->tm_mon, tms->tm_mday, week[tms->tm_wday], tms->tm_hour, tms->tm_min, tms->tm_sec);

    return 0;
}

使用cc命令编译 ⤵︎

cc -o test_time test.c

运行结果如下 ⤵︎

现在是当地时间:2019年3月18日 星期一 03:00:14