我是靠谱客的博主 机智羊,这篇文章主要介绍Linux tm time_t timeval timespec以及与时间相关函数用法,现在分享给大家,希望可以做个参考。

一、时间类型

linux中编程通常需要用到时间变量,和相关的时间操作函数。常用的时间类型有:

time_t 、struct  timeval、struct   timespec、struct   tm

在用到相关的类型和函数时,需要加上头文件:#include <time.h>

  • time_t: 存储从1970年到现在经过了多少。格式为long int。UTC时间。
  • struct  timeval:提供秒和微秒单位,最高精度是微秒
复制代码
1
2
3
4
5
6
7
8
/* A time value that is accurate to the nearest microsecond but also has a range of years. */ struct timeval { __time_t tv_sec; /* Seconds. */ __suseconds_t tv_usec; /* Microseconds. */ }; # endif /* struct timeval */
  • struct   timespec:提供秒和纳秒单位,最高精度是纳秒
复制代码
1
2
3
4
5
struct timespec { __time_t tv_sec; /* Seconds. */ __syscall_slong_t tv_nsec; /* Nanoseconds. */ };
  • struct   tm:详细时间的结构体
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct tm { int tm_sec; /* Seconds. [0-60] (1 leap second) */ int tm_min; /* Minutes. [0-59] */ int tm_hour; /* Hours. [0-23] */ int tm_mday; /* Day. [1-31] */ int tm_mon; /* Month. [0-11] */ int tm_year; /* Year - 1900. */ int tm_wday; /* Day of week. [0-6] */ int tm_yday; /* Days in year.[0-365] */ int tm_isdst; /* DST. [-1/0/1]*/ # ifdef __USE_MISC long int tm_gmtoff; /* Seconds east of UTC. */ const char *tm_zone; /* Timezone abbreviation. */ # else long int __tm_gmtoff; /* Seconds east of UTC. */ const char *__tm_zone; /* Timezone abbreviation. */ # endif };

二、时间函数

复制代码
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
char *asctime(const struct tm* timeptr); //将结构中的信息转换为真实世界的UTC时间,以字符串的形式显示 char *ctime(const time_t *timep); //将timep转换为真是世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不同 double difftime(time_t time1, time_t time2); //返回两个时间相差的秒数 int gettimeofday(struct timeval *tv, struct timezone *tz); //返回当前距离1970年的秒数和微秒数,后面的tz是时区,一般不用 struct tm* gmtime(const time_t *timep); //将time_t表示的时间转换为没有经过时区转换的UTC时间,是一个struct tm结构指针 stuct tm* localtime(const time_t *timep); //和gmtime类似,但是它是经过时区转换的时间。 time_t mktime(struct tm* timeptr); //将struct tm 结构的时间转换为从1970年至今的秒数 time_t time(time_t *t); //取得从1970年1月1日至今的秒数 //eg,totalsec = time(NULL) or time(&totalsec); int clock_gettime(clockid_t clk_id, struct timespec* tp); /*可以根据需要,获取不同要求的精确时间 参数 clk_id : 检索和设置的clk_id指定的时钟时间。 CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时, 中间时刻如果系统时间被用户改成其他,则对应的时间相应改变 CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响 CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间 CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间*/ //eg. 获得从开机到现在的时间:clock_gettime(CLOCK_MONOTONIC,&tspec);

三、示例

1、设置系统时间:

复制代码
1
2
3
4
5
6
7
8
struct timeval tv; tv.tv_sec = (time_t)gps_utctime_sec; tv.tv_usec = 0; if(settimeofday (&tv, NULL) < 0){ MSG("[ GPS ]:Set system datatime error,may need root permissions!n"); } else{ MSG("[ GPS ]:GPS valid,update system time!n");

2、读取当前时间tick

复制代码
1
2
3
4
5
6
7
/*! get the current system time tick, second unit */ static uint32_t getCurrSysTick(void) { time_t timep; time (&timep); return timep;//seconds from 1970-1-1:0:0:0 }

3、读取当前时间字符串:Fri Dec 20 14:44:18 2019

复制代码
1
2
3
4
5
6
static char* getCurrSysTime(void) { time_t timep; time (&timep); return asctime(localtime(&timep)); }

4、设置延时

复制代码
1
2
3
4
5
6
7
static void delay_ms(unsigned int dms) { struct timespec sleeper, dummy ; sleeper.tv_sec = (time_t)(dms / 1000) ; sleeper.tv_nsec = (long)(dms % 1000) * 1000000 ; nanosleep (&sleeper, &dummy);//delay 1ms }

5、获取当前时间tm

复制代码
1
2
3
4
5
6
7
8
9
10
struct tm* t_tm; t_tm = localtime((time_t *)&meas_gps_utctime.tv_sec); /* test gps */ MSG("today is %4d-%02d-%02d %02d:%02d:%02dn", 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);

 

Linux时间转换图:

 

 

参考:

  1. c++ 时间类型详解 time_t
  2. linux应用time和timezone
  3. linux下的clock_gettime()获取时间函数

最后

以上就是机智羊最近收集整理的关于Linux tm time_t timeval timespec以及与时间相关函数用法的全部内容,更多相关Linux内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部