zoukankan      html  css  js  c++  java
  • 【转载】Linux时间相关结构与函数

    1 时间的获取

    在程序当中, 我们经常要输出系统当前的时间,比如日志文件中的每一个事件都要记录其产生时间。在 C 语言中获取当前时间的方法有以下几种,它们所获得的时间精度从秒级到纳秒,各有所不同。

    表 1. C 时间函数
    function 定义含义返回值精度
    time() time 函数获得从 1970 年 1 月 1 日 0 点到当前的秒数,存储在time_t结构之中。
    typedef long time_t; 


    gettimeofday() gettimeofday 函数返回从 1970 年 1 月 1 日 0 点以来,到现在的时间。用 timeval 数据结构表示。 struct timeval
    {
    time_t tv_sec;
    long int tv_usec;
    };
    微秒
    clock_gettime()
    clock_gettime 函数返回从 1970 年 1 月 1 日 0 点以来,到现在的时间。用 timespec 数据结构表示。
    支持不广泛。属于实时扩展。
    struct timespec
    {
    time_t tv_sec;
    long int tv_nsec;
    };
    纳秒
    ftime() 函数返回从 1970 年 1 月 1 日 0 点以来,到现在的时间。用timeb数据结构表示。
    已经过时, 被 time() 替代。尽量不使用。
    struct timeb {
    time_t time;
    unsigned short
    millitm;
    short timezone;
    short dstflag;
    };
    毫秒

    GUN/Linux 提供了三个标准的 API 用来获取当前时间,time()/gettimeofday()/clock_gettime(),它们的区别仅在于获取的时间精度不同,您可以根据需要 选取合适的调用。ftime() 是老的一些系统中的时间调用,很多 Linux 版本虽然支持它,但仅仅是为了向前兼容性,新开发的软件不建议使用 ftime() 来获得当前时间。

    2 时间显示和转换

    目前我们得到的时间是一个数字,无论精度如 何,它代表的仅是一个差值。比如精度为秒的 time() 函数,返回一个 time_t 类型的整数。假设当前时间为 2011 年 12 月 7 日下午 20 点 29 分 51 秒,那么 time_t 的值为:1323318591。即距离 1970 年 1 月 1 日零点,我们已经过去了 1323318591 秒。(这里的 1970 年 1 月 1 日零点是格林威治时间,而不是北京时间。)我们下面讨论的时间如果不特别说明都是格林威治时间,也叫 GMT 时间,或者 UTC 时间。

    字符串“1323318591 秒”对于多数人都没有太大的意义,我们更愿意看到“2011 年 12 月 7 日”这样的显示。因此当我们得到秒,毫秒,甚至纳秒表示的当前时间之后,往往需要将这些数字转换为人们所熟悉的时间表示方法。

    由 于国家,习惯和时区的不同,时间的表示方法并没有一个统一的格式。为了满足各种时间显示的需求,标准 C 库提供了许多时间格式转换的函数。这些函数的数量众多,容易让人迷惑,记住它们的用法十分不易。在这里我借用 Michael Kerrisk 在《Linux Programming Interface》一书中的插图,来对这些标准 C 函数进行一个总体的概览。

    图 1. 各种时间显示格式转换函数关系图
    各种时间显示格式转换函数关系图

    从上图可以看到,time()/gettimeofday() 从内核得到当前时间之后,该当前时间值可以被两大类函数转换为更加容易阅读的显示格式:

    • 固定格式转换
    • 用户指定格式转换函数。

    固定格式转换

    用 ctime() 函数转换出来的时间格式是系统固定的,调用者无法改动,因此被称为固定格式转换。如果您对日期格式没有特殊的要求,那么用它基本上就可以了,简单,不用记忆很多的参数。

    用户指定格式转换

    典型的 ctime() 格式如下:

    Wed Dec 7 20:45:43 PST 2011

    有些人觉得这个格式太长,类似 Wed,星期三这样的信息很多情况下都没有啥用途。人们可能更喜欢其他格式:比如2011-12-07 20:45。在这种情况下,就需要进行时间显示格式转换。做法为:先把从内核得到的时间值转换为 struct tm 类型的值,然后调用 strftime() 等函数来输出自定义的时间格式字符串。

    下面我列举一些实例,以便读者更清晰地理解众多的时间转换函数的用法。

    3 各标准 C 时间转换函数的解释和举例

    char *ctime(const time_t *clock);

    使用函数 ctime 将秒数转化为字符串. 这个函数的返回类型是固定的:一个可能值为”Thu Dec 7 14:58:59 2000”。这个字符串的长度和显示格式是固定的。

    清单 1,time 的使用
     1 #include <time.h>
     2 int main ()
     3 {
     4  time_t time_raw_format;
     5  time ( &time_raw_format ); //获取当前时间
     6  printf (" time is [%d] ", time_raw_format);
     7  //用 ctime 将时间转换为字符串输出
     8  printf ( "The current local time: %s", ctime(&time_raw_format));
     9  return 0;
    10 }

    自定义格式转换

    为了更灵活的显示,需要把类型 time_t 转换为 tm 数据结构。tm 数据结构将时间分别保存到代表年,月,日,时,分,秒等不同的变量中。不再是一个令人费解的 64 位整数了。这种数据结构是各种自定义格式转换函数所需要的输入形式。

    清单 2,数据结构 tm
     1 struct tm {
     2 int tm_sec; /* Seconds (0-60) */
     3 int tm_min; /* Minutes (0-59) */
     4 int tm_hour; /* Hours (0-23) */
     5 int tm_mday; /* Day of the month (1-31) */
     6 int tm_mon; /* Month (0-11) */
     7 int tm_year; /* Year since 1900 */
     8 int tm_wday; /* Day of the week (Sunday = 0)*/
     9 int tm_yday; /* Day in the year (0-365; 1 Jan = 0)*/
    10 int tm_isdst; /* Daylight saving time flag
    11  > 0: DST is in effect;
    12  = 0: DST is not effect;
    13  < 0: DST information not available */
    14 };

    可以使用 gmtime() 和 localtime() 把 time_t 转换为 tm 数据格式,其中 gmtime() 把时间转换为格林威治时间;localtime 则转换为当地时间。

    清单 3,时间转换函数定义
    #include <time.h>
    struct tm *gmtime(const time_t *timep);
    struct tm *localtime(const time_t *timep);

    使用 tm 来表示时间,您就可以调用 asctime() 和 strftime() 将时间转换为字符串了。asctime() 的输出格式固定,和 ctime() 相同。strftime() 则类似我们最熟悉的 printf() 函数,您可以通过输入参数自定义时间的输出格式。

    size_t strftime(char *outstr, size_t maxsize, const char *format,const struct tm *timeptr);
    清单 4,时间显示转换
    int main ()
    {
        time_t time_raw_format;
        struct tm * time_struct;
        char buf [100];
        time ( &time_raw_format );
        time_struct = localtime ( &time_raw_format );
        strftime (buf,100,"It is now: %I:%M%p.",time_struct);
        puts (buf);
        return 0;
    }

    该例子程序的输出结果如下:

    It is now: 02:45PM.

    从以上的例子可以看到,利用从 time() 得到的时间值,可以调用各种转换函数将其转换成更方便人们阅读的形式。

    此 外从前面的总结中我们也了解到,还有两个 C 函数可以获得当前时间,gettimeofday() 以及 clock_gettime(),它们分别返回 struct timeval 或者 timespec 代表的高精度的时间值。在目前的 GLibC 中,还没有直接把 struct timeval/timespec 转换为 struct tm 的函数。一般的做法是将 timeval 中的 tv_sec 转换为 tm,使用上面所述的方法转换为字符串,最后在显示的时候追加上 tv_usec,比如下面的例子代码:

    清单 5,更多时间显示转换
    struct timeval tv;
    time_t nowtime; 
    struct tm *nowtm; 
    char tmbuf[64], buf[64]; 
    gettimeofday(&tv, NULL); //获取当前时间到 tv
    nowtime = tv.tv_sec; //nowtime 存储了秒级的时间值
    nowtm = localtime(&nowtime); //转换为 tm 数据结构
    //用 strftime 函数将 tv 转换为字符串,但 strftime 函数只能达到秒级精度
    strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
    //将毫秒值追加到 strftime 转换的字符串末尾 
    snprintf(buf, sizeof buf, "%s.%06d", tmbuf, tv.tv_usec);
  • 相关阅读:
    java内部类
    接口与继承
    数据结构
    数据I/O流
    课程总结
    第六次实训作业异常处理
    常用类的课后作业
    窗口实训1
    实训作业4
    实训作业3
  • 原文地址:https://www.cnblogs.com/xiongyunqi/p/3950094.html
Copyright © 2011-2022 走看看