幾個(gè)關(guān)于時(shí)間的概念
Coordinated Universal Time(UTC)
協(xié)調(diào)世界時(shí),又稱(chēng)為世界標(biāo)準(zhǔn)時(shí)間,通常也指的是格林威治標(biāo)準(zhǔn)時(shí)間(Greenwich Mean Time,GMT)。地球被分為24個(gè)時(shí)區(qū),相鄰時(shí)區(qū)相差一個(gè)小時(shí)。比如中國(guó)上海是+8區(qū),意思就是,假如格林威治現(xiàn)在是27日1點(diǎn)0分0秒,而中國(guó)上海就是27日早上9點(diǎn)0分0秒。中國(guó)內(nèi)地的時(shí)間與UTC的時(shí)差為+8,也就是UTC+8,美國(guó)則是UTC-5。
Calendar Time
日歷時(shí)間,是用“從一個(gè)標(biāo)準(zhǔn)時(shí)間點(diǎn)到此時(shí)的時(shí)間經(jīng)過(guò)的秒數(shù)”來(lái)表示的時(shí)間。
linux 時(shí)間函數(shù)
-
time / time_t:秒 -
gettimeofday / struct timeval:微妙 -
clock_gettime / struct timespec:納秒 -
tdstc:納秒
time_t 和 time 函數(shù)
#include <time.h>
typedef long time_t; // 一般用來(lái)記錄從1970年1月1日0時(shí)0分0秒到現(xiàn)在時(shí)刻的秒數(shù)
time_t time(time_t *calptr)
函數(shù)返回值:時(shí)間戳(秒數(shù))
如果calptr非空,那么時(shí)間值也會(huì)寫(xiě)入到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時(shí)0分0秒的時(shí)間,將其存放于timeval(微秒)結(jié)構(gòu)中。gettimeofday 在x86-64上已經(jīng)不是系統(tǒng)調(diào)用,相比于clock_gettime調(diào)用開(kāi)銷(xiāo)小,而相比于time精度高,適合推薦。
timespec 和 clock_gettime 函數(shù)
#include <time.h>
struct timespec {
time_t tv_sec; // 秒
long int tv_nsec // 納秒
}
運(yùn)用
#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;
}
時(shí)間格式化
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)的指針;若出錯(cuò),返回 NULL
gmtime 將時(shí)間戳轉(zhuǎn)換協(xié)調(diào)統(tǒng)一時(shí)間的tm結(jié)構(gòu)的指針
struct tm* localtime(const time_t *calptr);
函數(shù)返回值:指向分解的tm結(jié)構(gòu)的指針;若出錯(cuò),返回 NULL
localtime 將時(shí)間戳轉(zhuǎn)換成本地時(shí)間的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個(gè)函數(shù)的返回值:若有空間,返回存入數(shù)組的字符數(shù)。否則,返回0
常用格式:
| 格式 | 說(shuō)明 | 實(shí)例 |
|---|---|---|
| %F | YYYY-MM-DD | 2017-08-29 |
| %T | %H:%M:%S | 21:24:52 |
| %Y | 年 | 2018 |
| %m | 月 | 08 |
| %b | 縮寫(xiě)的月名 | Jan |
| %d | 月日 | 30 |
| %a | 縮寫(xiě)的周日名 | 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)境高級(jí)編程》[美] W. Richard Stevens Stephen A. Rago
2、