时间函数

      (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, seeasctime. 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


 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值