linux高精度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 }

 1 #include <time.h>
 2 
 3 // 返回自系統開機以來的毫秒數(tick)
 4 unsigned long GetTickCount()
 5 {
 6     struct timespec ts;
 7 
 8     clock_gettime(CLOCK_MONOTONIC, &ts);
 9 
10     return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
11 }
12 
13 
14 int main()
15 {
16     struct timespec time1 = { 0, 0 };
17 
18     clock_gettime(CLOCK_REALTIME, &time1);
19     printf("CLOCK_REALTIME: %d, %d\n", time1.tv_sec, time1.tv_nsec);
20 
21     clock_gettime(CLOCK_MONOTONIC, &time1);
22     printf("CLOCK_MONOTONIC: %d, %d\n", time1.tv_sec, time1.tv_nsec);
23 
24     clock_gettime(CLOCK_MONOTONIC_RAW, &time1);
25     printf("CLOCK_MONOTONIC_RAW: %d, %d\n", time1.tv_sec, time1.tv_nsec);
26 
27     clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
28     printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d\n", time1.tv_sec,
29            time1.tv_nsec);
30 
31     clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time1);
32     printf("CLOCK_THREAD_CPUTIME_ID: %d, %d\n", time1.tv_sec,
33            time1.tv_nsec);
34 
35     printf("\n%d\n", time(NULL));
36 
37     printf("tick count in ms: %ul\n", GetTickCount());
38 
39     return 0;
40 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM