本文共 3010 字,大约阅读时间需要 10 分钟。
(1)struct tm *gmtime(const time_t *timep);
将日历时间timep转换为用UTC时间表示的时间。它可能返回NULL,比如年份不能放到一个整数中。返回值指向一个静态分配的结构,该结构可能会被接下来的任何日期和时间函数调用覆盖。gmtime_r()函数功能与此相同,但是它可以将数据存储到用户提供的结构体中。
(2)struct tm *localtime(const time_t *clock);
localtime是 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,而gmtime函数转换后的时间没有经过时区变换,是UTC时间 。多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的。
注:日历时间,是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间。这个标准时间点对不同的来说会有所不同,但对一个来说,这个标准时间点是不变的,该编译系统中的时间对应的日历时间都通过该标准时间点来衡量,所以可以说日历时间是“相对时间”,但是无论你在哪一个时区,在同一时刻对同一个标准时间点来说,日历时间都是一样的。在C语言中通过time函数获得日历时间。
(3)time_t mktime(struct tm * timeptr);
mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数(带有时区转换,要求timeptr指向的结构是当地时间)。
由于mktime()要求传入的参数为本地时间,在转换中会先将本地时间转换为GMT时间,再转换为time_t表示的时间,因此认为mktime()带有时区转换.
From MSDN:
The mktime function converts the supplied time structure(possibly incomplete)pointed to by timeptrinto a fully defined structure with normalized valuesand then converts it to atime_t calendar time value. For description oftm structure fields, see. The converted time has the same encoding as the values returned by thetime function. The original values of the tm_wday andtm_yday components of thetimeptr structure are ignored, and the original values of the other components are not restricted to their normal ranges.
Note thatgmtime and localtime use a single statically allocated buffer for the conversion. If you supply this buffer tomktime, the previous contents are destroyed.
Linux 下 man mktime:
The mktime()function interprets the input structure according to the current time zone setting.
(4)time_t _mkgmtime( struct tm*timeptr);
Converts a UTC time represented by atm struct to a UTC time represented by atime_t type. (没有时区转换,但要求timeptr指向的数据是UTC时间)
_mkgmtime()要求传入的参数为GMT时间,在进行时间转换时不会进行时区转换。
(5)time_t time( time_t *timer);
MSDN:Gets the system time.
取系统时间可能受系统中设置的时区影响,因此不是UTC时间。
(6)
size_t strftime( char *strDest, size_tmaxsize, const char *format, const struct tm *timeptr);
size_t wcsftime( wchar_t *strDest, size_tmaxsize, const wchar_t *format, const struct tm *timeptr);
%a
Abbreviated weekday name
%A
Full weekday name
%b
Abbreviated month name
%B
Full month name
%c
Date and time representation appropriate for locale
%d
Day of month as decimal number (01 – 31)
%H
Hour in 24-hour format (00 – 23)
%I
Hour in 12-hour format (01 – 12)
%j
Day of year as decimal number (001 – 366)
%m
Month as decimal number (01 – 12)
%M
Minute as decimal number (00 – 59)
%p
Current locale’s A.M./P.M. indicator for 12-hour clock
%S
Second as decimal number (00 – 59)
%U
Week of year as decimal number, with Sunday as first day of week (00 – 53)
%w
Weekday as decimal number (0 – 6; Sunday is 0)
%W
Week of year as decimal number, with Monday as first day of week (00 – 53)
%x
Date representation for current locale
%X
Time representation for current locale
%y
Year without century, as decimal number (00 – 99)
%Y
Year with century, as decimal number
%z, %Z
Time-zone name or abbreviation; no characters if time zone is unknown
%%
Percent sign
转载地址:http://ajxws.baihongyu.com/