Time's Data Structures
The original Representation
typedef long time_t;
And Now. Microsecond Precision
#include <sys/time.h>
struct timeval{
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
Even Better: Nanosecond Precision
#include <time.h>
struct timespec{
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds*/
};
Breaking Down Time
#include <time.h>
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* the day of the month */
int tm_mon; /* the month */
int tm_year; /* the year */
int tm_wday; /* the day of the week */
int tm_yday; /* the day in the year */
int tm_isdst; /* daylight savings time? */
#ifdef _BSD_SOURCE
long tm_gmtoff; /* time zone's offset from GMT */
const char *tm_zone; /* time zone abbreviation */
#endif /* _BSD_SOURCE */
};
#include <stdio.h>
#include <time.h>
#define BST (+1)
#define CCT (+8)
int main ()
{
time_t rawtime;
struct tm *info;
time(&rawtime);
/* 獲取 GMT 時(shí)間 */
info = gmtime(&rawtime );
printf("當(dāng)前的世界時(shí)鐘:\n");
printf("倫敦:%2d:%02d\n", (info->tm_hour+BST)%24, info->tm_min);
printf("中國(guó):%2d:%02d\n", (info->tm_hour+CCT)%24, info->tm_min);
return(0);
}
A Type for Process Time
類型clock_t表示時(shí)鐘滴答繁调。它是一個(gè)整數(shù)類型醉锅,通常是long疙赠。根據(jù)接口的不同,clock_t表示的滴答是系統(tǒng)的實(shí)際計(jì)時(shí)器頻率(HZ)或Clock_PER_SEC
POSIX Clocks
clockid_t 有如下的參數(shù)選擇:
- CLOCK_REALTIME
全系統(tǒng)實(shí)時(shí)(wall rime)時(shí)鐘.設(shè)置這個(gè)時(shí)鐘需要特殊的特權(quán)。 - CLOCK_MONOTONIC
一種單調(diào)增加的時(shí)鐘遥皂,任何進(jìn)程都不能設(shè)置指攒。它表示自某些未指定的起始點(diǎn)(如系統(tǒng)引導(dǎo))以來(lái)所花費(fèi)的時(shí)間。 - CLOCK_MONOTONIC_RAW
與CLOCK_MONOTONIC相似,但時(shí)鐘不適合旋轉(zhuǎn)(時(shí)鐘偏斜的校正)芹枷。也就是說(shuō)衅疙,如果硬件時(shí)鐘比墻壁時(shí)間運(yùn)行得更快或更慢,當(dāng)讀這個(gè)時(shí)鐘時(shí)不會(huì)對(duì)其進(jìn)行調(diào)整鸳慈。這個(gè)時(shí)鐘是linux專用的饱溢。 - CLOCK_PROCESS_CPUTIME_ID
處理器提供的一種高分辨率的進(jìn)程時(shí)鐘.例如,在x86架構(gòu)上走芋,這個(gè)時(shí)鐘使用時(shí)間戳計(jì)數(shù)器(TSC)寄存器绩郎。 - CLOCK_THREAD_CPUTIME_ID
類似于每個(gè)進(jìn)程的時(shí)鐘,但對(duì)進(jìn)程中的每個(gè)線程都是唯一的.
Time Source Resolution
POSIX定義了用于獲取給定時(shí)間源的分辨率的函數(shù)clock_getres():
#include <time.h>
int clock_getres(clockid_t clock_id, struct timespec *res);
成功返回0翁逞, 失敗返回-1并設(shè)置errno肋杖。
int main()
{
clockid_t clocks[] = {
CLOCK_REALTIME,
CLOCK_MONOTONIC,
CLOCK_PROCESS_CPUTIME_ID,
CLOCK_THREAD_CPUTIME_ID,
CLOCK_MONOTONIC_RAW,
(clockid_t)-1
};
int i;
for(i = 0; clocks[i]!=(clockid_t)-1; ++i){
struct timespec res;
int ret;
ret = clock_getres(clocks[i], &res);
if(ret)
perror("clock_getres");
else
printf("clock=%d sec=%ld nsec=%ld\n",
clocks[i], res.tv_sec, res.tv_nsec);
}
return 0;
}
測(cè)試結(jié)果of樹莓派
Getting the Current Time of Day
#include <time.h>
time_t time(time_t *t);
//example
time_t t;
printf("current time: %ld\n", (long)time(&t));
printf("the same vale: %ld\n", (long)t);
result
A Better Interface
#include <sys/time.h>
int gettimeofday (struct timeval *tv, struct timezone *tz);
timezone已經(jīng)過(guò)時(shí)了,不要用 傳NULL給tz
struct timeval tv;
int ret;
ret = gettimeofday(&tv, NULL);
if(ret)
perror("gettimeofday");
else
printf("second=%ld useconds=%ld\n",
(long)tv.tv_sec, (long)tv.tv_usec);
result
An Advanced Interface
#include <time.h>
int clock_gettime (clockid_t clock_id, struct timespec *ts);
成功調(diào)用返回0挖函, 失敗返回-1并設(shè)置errno
clockid_t clocks[] = {
CLOCK_REALTIME,
CLOCK_MONOTONIC,
CLOCK_PROCESS_CPUTIME_ID,
CLOCK_THREAD_CPUTIME_ID,
CLOCK_MONOTONIC_RAW,
(clockid_t)-1
};
int i;
for(i = 0; clocks[i]!=(clockid_t)-1; ++i){
struct timespec ts;
int ret;
ret = clock_gettime(clocks[i], &ts);
if(ret)
perror("clock_getres");
else
printf("clock=%d sec=%ld nsec=%ld\n",
clocks[i], ts.tv_sec, ts.tv_nsec);
}
測(cè)試結(jié)果
Getting the Process Time
#include <sys/times.h>
struct tms {
clock_t tms_utime; /* user time consumed */
clock_t tms_stime; /* system time consumed */
clock_t tms_cutime; /* user time consumed by children */
clock_t tms_cstime; /* system time consumed by children */
};
clock_t times (struct tms *buf);
失敗返回-1状植,并設(shè)置errno。