我们可以认为格林威治时间就是时间协调时间(GMT=UTC)
GMT : 格林威治时间
UTC : 时间协调时间
1、time_t
| time_t time(time_t *t); |
取得从1970年1月1日至今的秒数。
time_t类型,这本质上是一个长整数( long ),表示从1970-01-01 00:00:00到目前计时时间的秒数,timeval则精确到毫秒
2、timeval
timeval类型,这是一个结构体类型,struct timeval 头文件为 time.h
| struct timeval { time_t tv_sec; /* Seconds. */ //秒 suseconds_t tv_usec; /* Microseconds. */ //微秒 }; |
使用:
| struct timeval tv; gettimeofday(&tv, NULL); printf("%dt%dn", tv.tv_usec, tv.tv_sec); |
3、timezone
| struct timezone { int tz_minuteswest; /* minutes west of Greenwich */ int tz_dsttime; /* type of DST correction */ }; |
4、struct tm
tm结构,这本质上是一个结构体,里面包含了各时间字段
| struct tm { int tm_sec; /* seconds after the minute - [0,59] */ int tm_min; /* minutes after the hour - [0,59] */ int tm_hour; /* hours since midnight - [0,23] */ int tm_mday; /* day of the month - [1,31] */ int tm_mon; /* months since January - [0,11] */ int tm_year; /* years since 1900 */ int tm_wday; /* days since Sunday - [0,6] */ int tm_yday; /* days since January 1 - [0,365] */ int tm_isdst; /* daylight savings time flag */ }; |
其中tm_year表示从1900年到目前计时时间间隔多少年,如果是手动设置值的话,tm_isdst通常取值-1。
功能:
tm结构体包含,年、月、日,时、分、秒
使用:
| time_t t_time; struct tm *tmp_ptr = NULL; time(&t_time); printf("t_time = %d.n", t_time); tmp_ptr = gmtime(&t_time); printf("after gmtime, the time is: n yday = %d n wday = %d n year = %d n mon = %d n mday = %d n hour = %d n min = %d n sec = %d n isdst =%d.n", tmp_ptr->tm_yday, tmp_ptr->tm_wday, tmp_ptr->tm_year, tmp_ptr->tm_mon, tmp_ptr->tm_mday, tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec, tmp_ptr->tm_isdst ); |
打印:
| t_time = 1513841065. after gmtime, the time is: yday = 354 wday = 4 year = 117 mon = 11 mday = 21 hour = 7 min = 24 sec = 25 isdst =0. |
5、ctime/asctime
| char *ctime(const time_t *timep); |
将timep转换为真是世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不一样。








