我是靠谱客的博主 鳗鱼豆芽,这篇文章主要介绍对CLOCK_MONOTONIC的理解,现在分享给大家,希望可以做个参考。

CLOCK_MONOTONIC在timerfd_create以及clock_gettime中都有使用,具体函数如下:

复制代码
1
2
3
4
int timerfd_create(int clockid, int flags); //创建timerfd描述符 //clockid可以填CLOCK_REALTIME,CLOCK_MONOTONIC //flags可以填0,O_CLOEXEC,O_NONBLOCK
复制代码
1
2
3
4
5
6
7
8
9
int clock_gettime(clockid_t clk_id, struct timespec *tp); //得到当前的时间 //clk_id 设置时间的类型 //CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时, //中间时刻如果系统时间被用户改成其他,则对应的时间相应改变 //CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响 //CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间 //CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间

那CLOCK_MONOTONIC真的就是从系统启动时刻的时间吗?并且还有一个gettimeofday函数,之间是哪些关系

我写了一个程序测试:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h> #include <dirent.h> #include <iostream> #include <sys/time.h> #include <sys/types.h> int kMicroSecondsPerSecond = 1000 * 1000; int64_t NMicroSecondsPerSecond = 1000 * 1000 * 1000; int64_t now1() { struct timeval tv; gettimeofday(&tv, NULL); int64_t seconds = tv.tv_sec; printf("%ldn",seconds); return seconds * kMicroSecondsPerSecond + tv.tv_usec; //返回一个Timestamp结构体,相当于创建一个当前时间的Timestamp结构体 } int64_t now2() { struct timespec tv; clock_gettime(CLOCK_MONOTONIC, &tv); int64_t seconds = tv.tv_sec; printf("%ldn",seconds); return seconds * kMicroSecondsPerSecond + tv.tv_nsec/1000; //返回一个Timestamp结构体,相当于创建一个当前时间的Timestamp结构体 } void translate(int64_t microSecondsSinceEpoch_) { char buf[32] = {0}; time_t seconds = static_cast<time_t>(microSecondsSinceEpoch_ / kMicroSecondsPerSecond); int microseconds = static_cast<int>(microSecondsSinceEpoch_ % kMicroSecondsPerSecond); struct tm tm_time; gmtime_r(&seconds, &tm_time);//将总秒数转换成————年-月-日-小时-分-秒为单位,并且还会自动加上1970年1月1日时间 snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d", tm_time.tm_year+1900, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, microseconds);//总秒数加上1900年1月1日然后转换成固定格式 printf("%sn",buf); } int main() { int64_t microSecondsSinceEpoch1_ = now1(); translate(microSecondsSinceEpoch1_); int64_t microSecondsSinceEpoch2_ = now2(); translate(microSecondsSinceEpoch2_); translate(0); return 0; }

结果如下:

当前时间:

得到结果:

所以clock_gettime(CLOCK_MONOTONIC, &tv);就是开机到现在的时间,而gettimeofday则是从1970年开始,到现在的时间,但是这个时间是零时区的事件,也就是评论所说的格林尼治时间。

c++ 时间类型详解 time_t | 菜鸟教程可以参考这个网页

注意:其实在timerfd这套函数中,CLOCK_MONOTONIC具体代表什么含义并不重要,因为在设置timerfd的到期时间时,使用的函数如下:

复制代码
1
2
3
4
int timerfd_settime(int fd, int flags, const struct itimerspec *new_value, struct itimerspec *old_value);

其中flags填写1(TFD_TIMER_ABSTIME)代表绝对时间,0代表相对时间

如果timerfd_settime第二个参数设置为0,new_value.it_value设置为1
如果timerfd_settime第二个参数设置为TFD_TIMER_ABSTIME,new_value.it_value设置为now.tv_sec + 1。

并且timerfd_settime的当前时间可以使用函数clock_gettime来获取,填写的参数要和创建timerfd时的参数一样,也就是clk_id和clockid一样

最后

以上就是鳗鱼豆芽最近收集整理的关于对CLOCK_MONOTONIC的理解的全部内容,更多相关对CLOCK_MONOTONIC内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部