[转]linux时间相关结构体和函数整理
发布日期:2021-11-07 23:20:59 浏览次数:3 分类:技术文章

本文共 2703 字,大约阅读时间需要 9 分钟。

一、时间类型。Linux下常用的时间类型有4个:time_t,struct timeb, struct timeval,struct timespec,clock_t, struct tm.

(1) time_t是一个长整型,一般用来表示用1970年以来的秒数.

该类型定义在<sys/time.h>中.

一般通过 time_t time = time(NULL); 获取.

(2) struct timeb结构: 主要有两个成员, 一个是秒, 另一个是毫秒, 精确度为毫秒.

  1. struct timeb
  2. {
  3.     time_t time;
  4.     unsigned short millitm;
  5.     short timezone;
  6.     short dstflag;
  7. };

由函数int ftime(struct timeb *tp); 来获取timeb.

成功返回0, 失败返回-1.

(3) struct timeval有两个成员,一个是秒,一个是微妙.

  1. struct timeval 
  2. {
  3.     long tv_sec; /* seconds */
  4.     long tv_usec; /* microseconds */
  5. };
由int gettimeofday(struct timeval *tv, struct timezone *tz);获取.
struct timezone结构的定义为:
  1. struct timezone
  2. {
  3.    int tz_minuteswest; /* 和Greewich时间差了多少分钟*/
  4.    int tz_dsttime; /* 日光节约时间的状态 */
  5. };

(4) struct timespec有两个成员,一个是秒,一个是纳秒, 所以最高精确度是纳秒.
  1. struct timespec
  2. {
  3.     time_t tv_sec; /* seconds */
  4.     long tv_nsec; /* nanoseconds */
  5. };
一般由函数long clock_gettime (clockid_t which_clock, struct timespec *tp); 获取.
获取特定时钟的时间,时间通过tp结构传回,目前定义了6种时钟,分别是

   CLOCK_REALTIME                统当前时间,从1970年1.1日算起

   CLOCK_MONOTONIC               系统的启动时间,不能被设置

   CLOCK_PROCESS_CPUTIME_ID      进程运行时间

   CLOCK_THREAD_CPUTIME_ID       线程运行时间

   CLOCK_REALTIME_HR             CLOCK_REALTIME的高精度版本

   CLOCK_MONOTONIC_HR            CLOCK_MONOTONIC的高精度版本

   获取特定时钟的时间精度:

   long clock_getres(clockid_t );

   设置特定时钟的时间:

   long clock_settime(clockid_t ,struct timespec*);

   休眠time中指定的时间,如果遇到信号中断而提前返回,则由left_time返回剩余的时间:

   long clock_nanosleep(clockid_t ,int flag,timespec* time,timespec* left_time);

(5) clock_t类型, 由clock_t clock(); 返回获取.

表示进程占用的cpu时间. 精确到微秒.

(6) struct tm是直观意义上的时间表示方法:

  1. struct tm 
  2. {
  3.     int tm_sec; /* seconds */
  4.     int tm_min; /* minutes */
  5.     int tm_hour; /* hours */
  6.     int tm_mday; /* day of the month */
  7.     int tm_mon; /* month */
  8.     int tm_year; /* year */
  9.     int tm_wday; /* day of the week */
  10.     int tm_yday; /* day in the year */
  11.     int tm_isdst; /* daylight saving time */
  12. };
struct tm* gmtime(const time_t *timep);
struct tm* localtime(const time_t *timep);
time_t mktime(struct tm *tm);
gmtime和localtime的参数以及返回值类型相同,区别是前者返回的格林威治标准时间,后者是当地时间.
注意: 这边三个函数都是线程不安全的, 要使用线程安全的版本, 需要使用带_r的版本 -- gmtime_r, localtime_r, mktime_r.
二、 延迟函数
主要的延迟函数有:sleep(),usleep(),nanosleep(),select(),pselect().
  1. unsigned int sleep(unsigned int seconds);
  2. void usleep(unsigned long usec);
  3. int nanosleep(const struct timespec *req, struct timespec *rem);
  4. int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,struct timeval *timeout);
  5. int pselect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout, const sigset_t *sigmask);
alarm函数是信号方式的延迟,这种方式不直观,这里不说了。

仅通过函数原型中时间参数类型,可以猜测sleep可以精确到秒级,usleep/select可以精确到微妙级,nanosleep和pselect可以精确到纳秒级。

而实际实现中,linux上的nanosleep和alarm相同,都是基于内核时钟机制实现,受linux内核时钟实现的影响,并不能达到纳秒级的精度,man nanosleep也可以看到这个说明,man里给出的精度是:Linux/i386上是10 ms ,Linux/Alpha上是1ms。

转载地址:https://blog.csdn.net/zhangwu416826/article/details/7784766 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:关于指针比较(涉及的是对象同一性的问题)
下一篇:类成员指针和成员函数指针(深入理解)

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月26日 03时09分34秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【图解】拥塞控制 2021-06-30
线程上下文切换 2021-06-30
什么是服务熔断? 2021-06-30
服务器压力过大?CPU打满?我来帮你快速检查Linux服务器性能 2021-06-30
C++面经总结之《Effective C++》(一) 2021-06-30
C++面经总结之《Effective C++》(二) 2021-06-30
这是什么“虎狼之词”啊!!!程序员的健康问题,看一线老中医怎么说!!! 2021-06-30
打开我的收藏夹 -- Python数据分析杂谈 2021-06-30
上手Pandas,带你玩转数据(1)-- 实例详解pandas数据结构 2019-04-27
上手Pandas,带你玩转数据(2)-- 使用pandas从多种文件中读取数据 2019-04-27
上手Pandas,带你玩转数据(3)-- pandas数据存入文件 2019-04-27
爬虫遇上不让右击、不让F12的网站,该怎么办? 2019-04-27
上手Pandas,带你玩转数据(4)-- 数据清洗 2019-04-27
上手Pandas,带你玩转数据(5)-- 数据转换与数据定位 2019-04-27
上手Pandas,带你玩转数据(6)-- 摆脱对pandas可视化丑图的刻板印象吧 2019-04-27
从零开始,学会Python爬虫不再难!!! -- (1)开篇:初识爬虫,基础铺垫 丨蓄力计划 2019-04-27
从零开始,学会Python爬虫不再难!!! -- (2)承接:解析网页,抓取标签 丨蓄力计划 2019-04-27
AttributeError: module ‘urllib‘ has no attribute ‘quote‘的解决办法 2019-04-27
linux shell — 6.初识 EXT2 文件系统 2019-04-27
Java — String(字符串) 2019-04-27