幾個關(guān)于時間的概念
Coordinated Universal Time(UTC)
協(xié)調(diào)世界時,又稱為世界標(biāo)準(zhǔn)時間,通常也指的是格林威治標(biāo)準(zhǔn)時間(Greenwich Mean Time,GMT)。地球被分為24個時區(qū),相鄰時區(qū)相差一個小時。比如中國上海是+8區(qū),意思就是,假如格林威治現(xiàn)在是27日1點0分0秒,而中國上海就是27日早上9點0分0秒。中國內(nèi)地的時間與UTC的時差為+8,也就是UTC+8,美國則是UTC-5。
Calendar Time
日歷時間,是用“從一個標(biāo)準(zhǔn)時間點到此時的時間經(jīng)過的秒數(shù)”來表示的時間。
linux 時間函數(shù)
-
time / time_t
:秒 -
gettimeofday / struct timeval
:微妙 -
clock_gettime / struct timespec
:納秒 -
tdstc
:納秒
time_t 和 time 函數(shù)
#include <time.h>
typedef long time_t; // 一般用來記錄從1970年1月1日0時0分0秒到現(xiàn)在時刻的秒數(shù)
time_t time(time_t *calptr)
函數(shù)返回值:時間戳(秒數(shù))
如果calptr非空,那么時間值也會寫入到calptr所指的內(nèi)存中
timeval 和 gettimeofday 函數(shù)
#include <time.h>
struct timeval {
time_t tv_sec; // 秒
suseconds_t tv_usec // 微妙
}
#include <sys/time.h>
int gettimeofday(struct timeval *restrict tp, void *restrict tzp);
函數(shù)返回值:總是返回0
tzp唯一合法參數(shù)是NULL
gettimeofday
返回距1970年1月1日0時0分0秒的時間,將其存放于timeval
(微秒)結(jié)構(gòu)中。gettimeofday 在x86-64上已經(jīng)不是系統(tǒng)調(diào)用,相比于clock_gettime
調(diào)用開銷小,而相比于time
精度高,適合推薦。
timespec 和 clock_gettime 函數(shù)
#include <time.h>
struct timespec {
time_t tv_sec; // 秒
long int tv_nsec // 納秒
}
運用
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
time_t get_now_time_sec()
{
time_t t;
time(&t);
return t;
}
unsigned long long get_now_time_usec()
{
struct timeval val;
val.tv_sec = 0;
val.tv_usec = 0;
gettimeofday(&val, NULL);
return val.tv_sec * 1000000 + val.tv_usec;
}
unsigned long long get_now_time_nsec()
{
struct timespec spec;
spec.tv_sec = 0;
spec.tv_nsec = 0;
clock_gettime(CLOCK_REALTIME, &spec);
return spec.tv_sec * 1000000000 + spec.tv_nsec;
}
時間格式化
struct tm
#include <time.h>
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday; // day of month [1 -~ 31]
int tm_mon; // month since January [0 ~ 11]
int tm_year; // years since 1900
int tm_wday; // days since Sunday [0 ~ 6]
int tm_yday; // days since 1月1日 [0 ~ 265]
int tm_isdst;
};
gmtime和localtime函數(shù)
#include <time.h>
struct tm* gmtime(const time_t *calptr);
函數(shù)返回值:指向分解的tm結(jié)構(gòu)的指針;若出錯,返回 NULL
gmtime 將時間戳轉(zhuǎn)換協(xié)調(diào)統(tǒng)一時間的tm結(jié)構(gòu)的指針
struct tm* localtime(const time_t *calptr);
函數(shù)返回值:指向分解的tm結(jié)構(gòu)的指針;若出錯,返回 NULL
localtime 將時間戳轉(zhuǎn)換成本地時間的tm結(jié)構(gòu)的指針
strftime和strftim_l函數(shù)
#include <time.h>
size_t strftime(char *restrict buf, size_t maxsize,
const char* restrict format, const struct tm* restrict tmptr);
size_t strfime_l(char *restrict buf, size_t maxsize,
const char* restrict format, const struct tm *restrict tmptr,
locale_t locale);
2個函數(shù)的返回值:若有空間,返回存入數(shù)組的字符數(shù)。否則,返回0
常用格式:
格式 | 說明 | 實例 |
---|---|---|
%F | YYYY-MM-DD | 2017-08-29 |
%T | %H:%M:%S | 21:24:52 |
%Y | 年 | 2018 |
%m | 月 | 08 |
%b | 縮寫的月名 | Jan |
%d | 月日 | 30 |
%a | 縮寫的周日名 | Thu |
代碼示例:
#include <time.h>
#include <stdio.h>
int main()
{
time_t t;
struct tm *tmp,*tmp2;
char buf[64], buf2[64];
time(&t);
tmp = localtime(&t);
strftime(buf, 64, "time and date: %F %T", tmp);
printf("%s\n", buf);
tmp2 = gmtime(&t);
strftime(buf2, 64, "time and date: %F %T", tmp2);
printf("%s\n", buf2);
}
輸出:
time and date: 2018-04-28 22:05:40
time and date: 2018-04-28 14:05:40
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
int main(int argc, char**argv)
{
struct timeval ti;
struct tm *tmp;
char buf[64];
gettimeofday(&ti, NULL);
tmp = localtime(&(ti.tv_sec));
printf("%ld, %ld\n", ti.tv_sec, ti.tv_usec);
strftime(buf, 64, "time: %F %T", tmp);
printf("time is now: %s.%ld\n", buf, ti.tv_usec);
}
轉(zhuǎn)換關(guān)系
轉(zhuǎn)換關(guān)系.jpg
參考資料
1、《UNIX環(huán)境高級編程》[美] W. Richard Stevens Stephen A. Rago
2、