• Linux time.h头文件详解


    1、相关头文件

    与时间相关的头文件:time.h sys/time.h sys/times.h sys/timeb.h sys/timex.h;其中time.h是C标准库中的头文件,其余sys开头的都为Linux系统自带的头文件;

    在/usr/include/time.h 定义了常用的time函数;

    在/usr/include/sys目录下查看这几个文件:

    sys/time.h定义了timezone结构体和Linux系统的时间函数;

    sys/times.h 定义了进程使用CPU时间的结构体tms;

    sys/timeb.h定义了ftime函数的返回值的结构体timeb;

    sys/timex.h定义了关于时钟调整算法的结构体timex;

    2、常用函数和结构体

    time函数原型(time.h中)

    参数:time_t (类型变量的指针)

    返回值:time_t 相当于一个long类型,time用于取Epoch记年以来到现在经过的秒数(系统当前时间),Epoch记年从1970年1月1日开始。把取到的时间存在指针指向的变量中。

    localtime函数原型(在time.h中):

    struct tm *localtime(const time_t *calptr);

    参数:time_t 类型变量的指针;

    返回值:指向 tm 结构体的指针类型;作用是将time_t的值转换为tm结构体。然后可以打印输出。

    1. #ifdef _TIME_H
    2. __BEGIN_NAMESPACE_STD
    3. /* Used by other time functions. */
    4. struct tm
    5. {
    6. int tm_sec; /* Seconds. [0-60] (1 leap second) */
    7. int tm_min; /* Minutes. [0-59] */
    8. int tm_hour; /* Hours. [0-23] */
    9. int tm_mday; /* Day. [1-31] */
    10. int tm_mon; /* Month. [0-11] */
    11. int tm_year; /* Year - 1900. */
    12. int tm_wday; /* Day of week. [0-6] */
    13. int tm_yday; /* Days in year.[0-365] */
    14. int tm_isdst; /* DST. [-1/0/1]*/
    15. # ifdef __USE_MISC
    16. long int tm_gmtoff; /* Seconds east of UTC. */
    17. const char *tm_zone; /* Timezone abbreviation. */
    18. # else
    19. long int __tm_gmtoff; /* Seconds east of UTC. */
    20. const char *__tm_zone; /* Timezone abbreviation. */
    21. # endif
    22. };
    23. __END_NAMESPACE_STD
    24. #if defined __USE_XOPEN || defined __USE_POSIX
    25. __USING_NAMESPACE_STD(tm)
    26. #endif

    ftime.h函数原型(timeb.h)

    int ftime(struct timeb *tp);

    参数:指向timeb结构体变量的指针;

    返回值:将当前系统时间存入timeb结构体中,包括了秒和毫秒。作用是能获取当前时间精确到毫秒;

    timeb结构体(sys/timeb.h)

    1. __BEGIN_DECLS
    2. /* Structure returned by the `ftime' function. */
    3. struct timeb
    4. {
    5. time_t time; /* Seconds since epoch, as from `time'. */
    6. unsigned short int millitm; /* Additional milliseconds. */
    7. short int timezone; /* Minutes west of GMT. */
    8. short int dstflag; /* Nonzero if Daylight Savings Time used. */
    9. };
    10. /* Fill in TIMEBUF with information about the current time. */
    11. extern int ftime (struct timeb *__timebuf);
    12. __END_DECLS

    times函数原型(sys/times.h)

    clock_t times(struct tms *buf);

    参数:指向tms结构体变量的指针。

    返回值:clock_t类型等同于long类型,用于获得进程运行时的CPU时间。

    tms结构体(sys/times.h中)

    1. __BEGIN_DECLS
    2. /* Structure describing CPU time used by a process and its children. */
    3. struct tms
    4. {
    5. clock_t tms_utime; /* User CPU time. */
    6. clock_t tms_stime; /* System CPU time. */
    7. clock_t tms_cutime; /* User CPU time of dead children. */
    8. clock_t tms_cstime; /* System CPU time of dead children. */
    9. };
    10. /* Store the CPU time used by this process and all its
    11. dead children (and their dead children) in BUFFER.
    12. Return the elapsed real time, or (clock_t) -1 for errors.
    13. All times are in CLK_TCKths of a second. */
    14. extern clock_t times (struct tms *__buffer) __THROW;
    15. __END_DECLS

    示例:

    1. #include <stdio.h>
    2. #include <time.h>
    3. #include <sys/time.h>
    4. #include <sys/timeb.h>
    5. #include <sys/times.h>
    6. #include <unistd.h>
    7. int main(int argc, char* argv[])
    8. {
    9. int i = 0; ///< 循环参数
    10. long tck = 0; ///< 获取系统时钟
    11. long l_begin_time = 0; ///< 起始时间点
    12. long l_end_time = 0; ///< 结束时间点
    13. time_t curr;
    14. struct tm * t_tm;
    15. struct tms t_tms;
    16. struct timeb t_tmb;
    17. tzset(); ///< 对 UNIX 操作系统的兼容性
    18. // time函数获得秒数
    19. time(&curr);
    20. printf("current time is %ld seconds\n", curr);
    21. // localtime函数转换time_t
    22. t_tm = localtime(&curr);
    23. printf("%4d-%02d-%02d %02d:%02d:%02d\n", t_tm->tm_year + 1900, t_tm->tm_mon + 1, t_tm->tm_mday, t_tm->tm_hour, t_tm->tm_min, t_tm->tm_sec);
    24. // ftime函数获得时间包括毫秒
    25. ftime(&t_tmb);
    26. t_tm = localtime(&t_tmb.time);
    27. printf("%4d-%02d-%02d %02d:%02d:%02d :%3d\n", t_tm->tm_year + 1900, t_tm->tm_mon + 1, t_tm->tm_mday, t_tm->tm_hour, t_tm->tm_min, t_tm->tm_sec, t_tmb.millitm);
    28. // 用times函数计算以下循环运行花费的时间
    29. l_begin_time = times(&t_tms);
    30. printf("l_begin_time = %ld\n", l_begin_time);
    31. while (1)
    32. {
    33. i = i + 1;
    34. if (i == 0)
    35. {
    36. break;
    37. }
    38. }
    39. l_end_time = times(&t_tms);
    40. printf("l_end_time = %ld\n", l_end_time);
    41. printf("used CPU time: %ld\n", l_end_time - l_begin_time);
    42. tck = sysconf(_SC_CLK_TCK); ///< 获取系统时钟(1秒里有多少个)
    43. printf("transform minutes: %f\n", ((l_end_time - l_begin_time) / (double)tck));
    44. return 0;
    45. }

    执行结果:

     

  • 相关阅读:
    【MySql】MySQL概述及其语法概述
    【C++】顺序表 链表 栈练习题第三弹来啦(每日小细节008)
    【CKA考试笔记】十三、k8s中的网络
    面试官:你天天用 Lombok,说说它什么原理?我竟然答不上来…
    zabbix监控实战1
    阿里云服务器价格表,轻量和服务器最新活动价格表汇总
    02 Java虚拟机的结构
    多层高速PCB设计学习(一)初探基本知识(附单层设计补充)
    Linux —— OpenCv编译安装
    项目管理必备工具推荐:提高效率、协作与跟踪的实用工具盘点
  • 原文地址:https://blog.csdn.net/hello_wordmy/article/details/125469611