zoukankan      html  css  js  c++  java
  • Linux系统时间函数

    先来说说自己在做工程过程中的一些理解:

    1, 输入time_t,输出tm格式的函数 loctaltime(time_t) / gmtime(time_t)

      其中localtime会受时区和夏令时影响,也就是说系统会把函数输入的time_t格式的值作为UTC时间,然后根据本地的TZ环境变量,进行小时的偏移得到一个tm格式的时间;

      gmtime则不做环境变量相关的处理,直接获得tm格式的值。 

    2, 输入tm,输出time_t格式的函数mktime()

      这个函数也受TZ环境变量的影响,确切来说受夏令时标志的影响。如果tm中tm_isdst成员为1,则在做转化时,mk会自动获得程序运行环境的夏令时信息,所以结果受tm成员与夏令时偏移量共同影响。

    3, 字符串格式函数ctime(time_t)  asctime(tm)

      将对应的值根据本地时间环境变量打印。

    以下摘自于网友的好帖子

    系统函数的转化关系如下:

     time_t与struct tm之间的转换

    struct tm {

        int tm_sec;      /*Seconds (0-60)*/

        int tm_min;     /*Minites (0-59)*/

        int tm_hour;   /*Hours (0-23)*/

        int tm_mday;  /*Day of the month (1-31)*/

        int tm_mon;   /*Month (1-12)*/

        int tm_year;    /*Year since 1900*/

        int tm_wday;   /*Day of the week (Sunday = 0)*/

        int tm_yday;    /*Day in the year (0-365; 1 Jan = 0)*/

        int tm_isdst;    /*Daylight saving time flag > 0: DST is in effect; = 0: DST is not effect; < 0: DST information not available*/
    };

    结构体tm将日期和时间分解成多个独立的字段,这样能方便程序获取不同的字段值来处理。字段tm_sec的上限为60而不是59,这样的设计主要是考虑闰秒,偶尔用其将人类日历调整至精确的天文年(所谓的回归年)。如果程序中定义了_BSD_SOURCE测试宏,那么有glibc定义的tm结构还会包括两个字段,一个为long int tm_gmtoff,用于表示时间超出UTC以东的秒数,一个为const char* tm_zone,用于表示时区的缩写(例如:CEST为欧洲中部夏令时间)。

    gmtime()和localtime()两个函数可将time_t转换成struct tm。gmtime()直接将time_t分解成UTC时间的tm,localtime()需要考虑时区和夏令时的设置,具体声明如下:

    #include <time.h> 

    // Both return a pointer to a statically allocated broker-down time structure on success, or NULL on error

    struct tm* gmtime (const time_t *timep);

    struct tm* localtime (const time_t *timep);

    以上两个函数都是非线程安全的,线程安全版本为gmtime_r()和localtime_r()

    mktime()函数可以将struct tm转换成time_t,其声明如下:

    time_t mktime (struct tm *timeptr);

    该函数可能会修改timeptr对应的值,至少会确保对tm_wday和tm_yday字段的设置,确保这些字段与其他字段能够相互对应起来。同时,mktime()在进行转换时会对时区进行设置。此外,DST设置的使用与否取决于输入字段tm_isdst的值。

    • 若tm_isdst为0,则将这一时间视为标准时间(即,忽略夏令时)
    • 若tm_isdst大于0,则将这一时间视为夏令时
    • 若tm_isdst小于0,则试图判定DST在每年的这一时间是否生效。这往往是众望所归的设置

       定义于/usr/share/zoneinfo中。时区的设置会影响到ctime()、localtime()、mktime()、strftime()等函数,为了获取时区设置,这些函数都会调用tzset(3)对如下全局变量进行设置:

    char *tzname[2];     /*Name of timezone and alternate (DST) timezone*/

    int daylight;            /*Nonzero if there is an alternate (DST) timezone*/

    long timezone;       /*Seconds difference between UTC and local standard time*/

  • 相关阅读:
    AGC 014 B
    AGC 012 D
    AGC 012 C
    python
    Linux
    Linux
    Linux
    Linux 之 光标消失隐藏术
    Linux
    python
  • 原文地址:https://www.cnblogs.com/vincentfu/p/5398609.html
Copyright © 2011-2022 走看看