linux c 時間函數


1、 time() 函數提供了 秒 級的精確度

time_t time(time_t * timer)
函數返回從UTC1970-1-1 0:0:0開始到現在的秒數

2、 struct timespec 提供了 ns  級的精確度

定義如下:

typedef long time_t;
#ifndef _TIMESPEC
#define _TIMESPEC
struct timespec {
  time_t tv_sec; // seconds
  long tv_nsec; // and nanoseconds
};
#endif

對應的操作函數為

int clock_gettime(clockid_t, struct timespec *)

其中 clockid_t 為使用的時鍾,主要有以下四種:

CLOCK_REALTIME    統當前時間,從1970年1.1日算起
CLOCK_MONOTONIC  系統的啟動時間,不能被設置
CLOCK_PROCESS_CPUTIME_ID 本進程運行時間
CLOCK_THREAD_CPUTIME_ID 本線程運行時間

3、struct timeval 提供了 us 級 的精確度

struct timeval {
  time_t tv_sec; // seconds
  long tv_usec; // microseconds
};

對應的操作函數為

int gettimeofday(struct timeval *tv, struct timezone *tz)

其中 timezone 為時區,定義如下:

struct timezone{
  int tz_minuteswest; //miniutes west of Greenwich
  int tz_dsttime; //type of DST correction
};

一般時區填寫為 NULL即可。

 


免責聲明!

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



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