Jeffies值与linux系统时间——实践篇
阅读目录:
1、 时间相关的C库函数(man 3)及linux系统API(man 2)
2、 代码实践
1、 时间相关的C库函数及linux系统API简介:
1.1、常用的API及C库函数转换图:
1.2、简介:
(1)time系统调用返回当前时间距1970-01-01 00:00:00+0000(UTC)的总秒数。这个time内部就是用jiffies换算得到的秒数。其他函数基本都是围绕着time来工作的。
(2)gmtime和localtime会把time得到的秒数变成一个struct tm结构体表示的时间。区别是gmtime得到的是国际时间,而localtime得到的是本地时间(指的是你运行localtime函数的程序所在的计算机所设置的时区对应的本地时间)。与电脑自身设置有关的。
(3)、 mktime用来完成相反方向的转换(struct tm到time_t)
(4)asctime或者strftime依据struct tm得到字符串格式的时间
(5)、ctime 依据time_t得到字符串格式时间
(6)、gettimeofday返回的时间是由struct timeval和struct timezone这两个结构体来共同表示的,其中timeval表示时间,而timezone表示时区。精度可以达到微秒 settimeofday是用来设置当前的时间和时区的。
(7)总结:不管用哪个系统调用,最终得到的时间本质上都是一个时间(这个时间最终都是从kernel中记录的jiffies中计算得来的),只不过不同的函数返回的时间的格式不同,精度不同。
2、 代码实践:
2.1、time
man手册摘录:
#include <time.h>
time_t time(time_t *t);
time() returns the time as the number of seconds since the Epoch,
1970-01-01 00:00:00 +0000 (UTC).If t is non-NULL, the return value is also stored in the memory pointed to by t.
On success, the value of time in seconds since the Epoch is returned.
On <