struct timespec 和 struct timeval

本文介绍了如何使用C语言中的time.h和sys/time.h库来获取不同精度的时间戳,包括秒级、毫秒级、微秒级和纳秒级的时间,并通过示例代码展示了如何使用struct timespec和struct timeval来获取当前时间。

time()提供了秒级的精确度 .

1、头文件 <time.h> 
2、函数原型 
time_t time(time_t * timer) 
函数返回从TC1970-1-1 0:0:0开始到现在的秒数 

用time()函数结合其他函数(如:localtime、gmtime、asctime、ctime)可以获得当前系统时间或是标准时间。

如果需要更高的时间精确度,就需要struct timespec 和 struct timeval来处理:

一、struct timespec 定义:

typedef long time_t;
#ifndef _TIMESPEC
#define _TIMESPEC
struct timespec {
time_t tv_sec; // seconds 
long tv_nsec; // and nanoseconds 
};
#endif
struct timespec有两个成员,一个是秒,一个是纳秒, 所以最高精确度是纳秒。
一般由函数int clock_gettime(clockid_t, struct timespec *)获取特定时钟的时间,常用如下4种时钟:
CLOCK_REALTIME 统当前时间,从1970年1.1日算起
CLOCK_MONOTONIC 系统的启动时间,不能被设置
CLOCK_PROCESS_CPUTIME_ID 本进程运行时间
CLOCK_THREAD_CPUTIME_ID 本线程运行时间
struct tm *localtime(const time_t *clock);  //线程不安全
struct tm* localtime_r( const time_t* timer, struct tm* result );//线程安全
size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );

 

二、struct timeval 定义:

struct timeval {
time_t tv_sec; // seconds 
long tv_usec; // microseconds 
};
struct timezone{ 
int tz_minuteswest; //miniutes west of Greenwich 
int tz_dsttime; //type of DST correction 
};

struct timeval有两个成员,一个是秒,一个是微秒, 所以最高精确度是微秒。
一般由函数int gettimeofday(struct timeval *tv, struct timezone *tz)获取系统的时间 

三、 对照样例:

复制代码
 1 #include<stdio.h>
 2 #include<time.h>
 3 #include<sys/time.h>
 4 
 5 void nowtime_ns()
 6 {
 7     printf("---------------------------struct timespec---------------------------------------\n"); 
 8     printf("[time(NULL)]     :     %ld\n", time(NULL)); 
 9     struct timespec ts;
10     clock_gettime(CLOCK_REALTIME, &ts);
11     printf("clock_gettime : tv_sec=%ld, tv_nsec=%ld\n", ts.tv_sec, ts.tv_nsec);
12     
13     struct tm t;
14     char date_time[64];
15     strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&ts.tv_sec, &t));
16     printf("clock_gettime : date_time=%s, tv_nsec=%ld\n", date_time, ts.tv_nsec);
17 }
18 void nowtime_us()
19 {
20     printf("---------------------------struct timeval----------------------------------------\n"); 
21     printf("[time(NULL)]    :    %ld\n", time(NULL)); 
22     struct timeval us;
23     gettimeofday(&us,NULL);
24     printf("gettimeofday: tv_sec=%ld, tv_usec=%ld\n", us.tv_sec, us.tv_usec);
25     
26     struct tm t;
27     char date_time[64];
28     strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&us.tv_sec, &t));
29     printf("gettimeofday: date_time=%s, tv_usec=%ld\n", date_time, us.tv_usec);
30 }
31 
32 int main(int argc, char* argv[])
33 {
34     nowtime_ns();
35     printf("\n");
36     nowtime_us();
37     printf("\n");
38     return 0;
39 }
复制代码
nowtime.cpp

执行结果:

$tt
---------------------------struct timespec---------------------------------------
[time(NULL)] : 1400233995
clock_gettime : tv_sec=1400233995, tv_nsec=828222000
clock_gettime : date_time=2014-05-16 17:53:15, tv_nsec=828222000

---------------------------struct timeval----------------------------------------
[time(NULL)] : 1400233995
gettimeofday: tv_sec=1400233995, tv_usec=828342
gettimeofday: date_time=2014-05-16 17:53:15, tv_usec=828342

 

over

./include/linux/time32.h:66:58: error: unknown type name 'time_t' 66 | extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec); | ^~~~~~ ./include/linux/time32.h:16:1: note: 'time_t' is defined in header '<time.h>'; this is probably fixable by adding '#include <time.h>' 15 | #include <vdso/time32.h> +++ |+#include <time.h> 16 | ./include/linux/time32.h:71:60: error: parameter 1 ('lhs') has incomplete type 71 | static inline struct timespec timespec_sub(struct timespec lhs, | ~~~~~~~~~~~~~~~~^~~ ./include/linux/time32.h:72:65: error: parameter 2 ('rhs') has incomplete type 72 | struct timespec rhs) | ~~~~~~~~~~~~~~~~^~~ ./include/linux/time32.h:71:31: error: return type is an incomplete type 71 | static inline struct timespec timespec_sub(struct timespec lhs, | ^~~~~~~~~~~~ ./include/linux/time32.h:71:31: error: function declaration isn't a prototype [-Werror=strict-prototypes] ./include/linux/time32.h: In function 'timespec_sub': ./include/linux/time32.h:74:25: error: storage size of 'ts_delta' isn't known 74 | struct timespec ts_delta; | ^~~~~~~~ ./include/linux/time32.h:75:9: error: implicit declaration of function 'set_normalized_timespec'; did you mean 'set_normalized_timespec64'? [-Wimplicit-function-declaration] 75 | set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec, | ^~~~~~~~~~~~~~~~~~~~~~~ | set_normalized_timespec64 ./include/linux/time32.h:77:16: error: 'return' with a value, in function returning void [-Wreturn-mismatch] 77 | return ts_delta; | ^~~~~~~~ ./include/linux/time32.h:71:31: note: declared here 71 | static inline struct timespec timespec_sub(struct timespec lhs, | ^~~~~~~~~~~~ ./include/linux/time32.h:74:25: error: unused variable 'ts_delta' [-Werror=unused-variable] 74 | struct timespec ts_delta; | ^~~~~~~~ In file included from ./include/linux/ktime.h:230: ./include/linux/timekeeping.h: At top level: ./include/linux/timekeeping.h:48:36: error: 'struct timeval' declared inside parameter list will not be visible outside of this definition or declaration [-Werror] 48 | extern void do_gettimeofday(struct timeval *tv); | ^~~~~~~ cc1: all warnings being treated as errors xtern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec); /* * sub = lhs - rhs, in normalized form */ static inline struct timespec timespec_sub(struct timespec lhs, struct timespec rhs) { struct timespec ts_delta; set_normalized_timespec(&ts_delta, lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec); return ts_delta; }
最新发布
12-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值