linux時(shí)間函數(shù)與時(shí)間格式與字符串之間的轉(zhuǎn)化方法
我們可以認(rèn)為格林威治時(shí)間就是時(shí)間協(xié)調(diào)時(shí)間(GMT=UTC)
GMT : 格林威治時(shí)間
UTC : 時(shí)間協(xié)調(diào)時(shí)間
1、time_t
time_t time(time_t *t);
取得從1970年1月1日至今的秒數(shù)。
time_t類型,這本質(zhì)上是一個(gè)長整數(shù)( long ),表示從1970-01-01 00:00:00到目前計(jì)時(shí)時(shí)間的秒數(shù),timeval則精確到毫秒
2、timeval
timeval類型,這是一個(gè)結(jié)構(gòu)體類型,struct timeval 頭文件為 time.h
struct timeval
{
time_t tv_sec; /* Seconds. */
//秒
suseconds_t tv_usec; /* Microseconds. */
//微秒
};使用:
struct timeval tv;
gettimeofday(&tv, NULL);
printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);3、timezone
struct timezone
{
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};4、struct tm
tm結(jié)構(gòu),這本質(zhì)上是一個(gè)結(jié)構(gòu)體,里面包含了各時(shí)間字段
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};其中tm_year表示從1900年到目前計(jì)時(shí)時(shí)間間隔多少年,如果是手動設(shè)置值的話,tm_isdst通常取值-1。
功能:
tm結(jié)構(gòu)體包含,年、月、日,時(shí)、分、秒
使用:
time_t t_time;
struct tm *tmp_ptr = NULL;
time(&t_time);
printf("t_time = %d.\n", t_time);
tmp_ptr = gmtime(&t_time);
printf("after gmtime, the time is: \
\n yday = %d \
\n wday = %d \
\n year = %d \
\n mon = %d \
\n mday = %d \
\n hour = %d \
\n min = %d \
\n sec = %d \
\n isdst =%d.\n",
tmp_ptr->tm_yday,
tmp_ptr->tm_wday,
tmp_ptr->tm_year,
tmp_ptr->tm_mon,
tmp_ptr->tm_mday,
tmp_ptr->tm_hour,
tmp_ptr->tm_min,
tmp_ptr->tm_sec,
tmp_ptr->tm_isdst
);打?。?/strong>
t_time = 1513841065. after gmtime, the time is: yday = 354 wday = 4 year = 117 mon = 11 mday = 21 hour = 7 min = 24 sec = 25 isdst =0.
5、ctime/asctime
char *ctime(const time_t *timep);
將timep轉(zhuǎn)換為真是世界的時(shí)間,以字符串顯示,它和asctime不同就在于傳入的參數(shù)形式不一樣。
char *asctime(const struct tm* timeptr);
將結(jié)構(gòu)中的信息轉(zhuǎn)換為真實(shí)世界的時(shí)間,以字符串的形式顯示。
char *ctime(const time_t *timer) 返回一個(gè)表示當(dāng)?shù)貢r(shí)間的字符串,當(dāng)?shù)貢r(shí)間是基于參數(shù) timer。
返回的字符串格式:Thu Dec 21 13:59:57 2017
使用:
time_t curtime;
struct tm *tm_ptr = NULL;
time(&curtime);
tm_ptr = localtime(&curtime);
printf("ctime轉(zhuǎn)換的當(dāng)前時(shí)間 = %s", ctime(&curtime));
printf("asctime轉(zhuǎn)換的當(dāng)前時(shí)間 = %s", asctime(tm_ptr));打?。?/strong>
ctime轉(zhuǎn)換的當(dāng)前時(shí)間 = Thu Dec 21 13:59:57 2017
asctime轉(zhuǎn)換的當(dāng)前時(shí)間 = Thu Dec 21 13:59:57 2017
6、gmtime/localtime
struct tm* gmtime(const time_t *timep);
將time_t表示的時(shí)間轉(zhuǎn)換為沒有經(jīng)過時(shí)區(qū)轉(zhuǎn)換的UTC時(shí)間,是一個(gè)struct tm結(jié)構(gòu)指針。
stuct tm* localtime(const time_t *timep);
和gmtime類似,但是它是經(jīng)過時(shí)區(qū)轉(zhuǎn)換的時(shí)間。
time_t curtime;
gmtime 函數(shù)將 curtime 轉(zhuǎn)換為struct tm結(jié)構(gòu)的格林威治時(shí)間,基本的意思是,gmtime轉(zhuǎn)出來的是0時(shí)區(qū)的標(biāo)準(zhǔn)時(shí)間
localtime 函數(shù)將 curtime 轉(zhuǎn)換為struct tm結(jié)構(gòu)的本地時(shí)間,localtime是將時(shí)區(qū)考慮在內(nèi)了,轉(zhuǎn)出的當(dāng)前時(shí)區(qū)的時(shí)間。
舉個(gè)例子:
time_t t_time;
struct tm *tmp_ptr = NULL;
time(&t_time);
printf("t_time = %d.\n", t_time);
tmp_ptr = gmtime(&t_time);
printf("after gmtime, the time is: \
\n yday = %d \
\n wday = %d \
\n year = %d \
\n mon = %d \
\n mday = %d \
\n hour = %d \
\n min = %d \
\n sec = %d \
\n isdst =%d.\n",
tmp_ptr->tm_yday,
tmp_ptr->tm_wday,
tmp_ptr->tm_year,
tmp_ptr->tm_mon,
tmp_ptr->tm_mday,
tmp_ptr->tm_hour,
tmp_ptr->tm_min,
tmp_ptr->tm_sec,
tmp_ptr->tm_isdst
);
tmp_ptr = localtime(&t_time);
printf("after localtime, the time is: \
\n yday = %d \
\n wday = %d \
\n year = %d \
\n mon = %d \
\n mday = %d \
\n hour = %d \
\n min = %d \
\n sec = %d \
\n isdst =%d.\n",
tmp_ptr->tm_yday,
tmp_ptr->tm_wday,
tmp_ptr->tm_year,
tmp_ptr->tm_mon,
tmp_ptr->tm_mday,
tmp_ptr->tm_hour,
tmp_ptr->tm_min,
tmp_ptr->tm_sec,
tmp_ptr->tm_isdst
);
return 0;
打?。?/strong>
t_time = 1513841065. after gmtime, the time is: yday = 354 wday = 4 year = 117 mon = 11 mday = 21 hour = 7 min = 24 sec = 25 isdst =0. after localtime, the time is: yday = 354 wday = 4 year = 117 mon = 11 mday = 21 hour = 15 min = 24 sec = 25 isdst =0.
7、mktime
time_t mktime(struct tm* timeptr);
將struct tm 結(jié)構(gòu)的時(shí)間轉(zhuǎn)換為從1970年至今的秒數(shù)。
mktime 與 gmtime/localtime 功能相反,gmtime/localtime 將time_t轉(zhuǎn)換為struct tm結(jié)構(gòu)體數(shù)據(jù),mktime將struct tm重新轉(zhuǎn)換為time_t類型的UTC時(shí)間
使用例子:
time_t t_time;
struct tm *tm_ptr = NULL;
time(&t_time);
printf("time_t first time value = %d.\n", t_time);
tm_ptr = gmtime(&t_time);
printf("time_t switch gmtime type, the time is: \
\n yday = %d \
\n wday = %d \
\n year = %d \
\n mon = %d \
\n mday = %d \
\n hour = %d \
\n min = %d \
\n sec = %d \
\n isdst =%d.\n",
tm_ptr->tm_yday,
tm_ptr->tm_wday,
tm_ptr->tm_year,
tm_ptr->tm_mon,
tm_ptr->tm_mday,
tm_ptr->tm_hour,
tm_ptr->tm_min,
tm_ptr->tm_sec,
tm_ptr->tm_isdst
);
t_time = mktime(tm_ptr); /*重新轉(zhuǎn)換為time_t類型的UTC時(shí)間,這里有一個(gè)時(shí)區(qū)的轉(zhuǎn)換, 時(shí)間為0區(qū)的時(shí)間, 所以一下使用的時(shí)間都為0區(qū)的時(shí)間*/
printf("gmtime type switch time_t second time = %d.\n", t_time);
打印:
time_t first time value = 1513842674. time_t switch gmtime type, the time is: yday = 354 wday = 4 year = 117 mon = 11 mday = 21 hour = 7 min = 51 sec = 14 isdst =0. gmtime type switch time_t second time = 1513813874.
8、gettimeofday
int gettimeofday(struct timeval *tv, struct timezone *tz);
返回當(dāng)前距離1970年的秒數(shù)和微妙數(shù),后面的tz是時(shí)區(qū),一般不用。
使用例子:
struct timeval time_val;
gettimeofday(&time_val, NULL); //gettimeofday(&start,&tz);結(jié)果一樣
printf("時(shí)間秒.tv_sec: %d.\n",time_val.tv_sec);
printf("時(shí)間微秒.tv_usec: %d.\n",time_val.tv_usec);
打?。?/strong>
時(shí)間秒.tv_sec:1513843633 時(shí)間微秒.tv_usec:689374
9、difftime
double difftime(time_t time1, time_t time2);
返回兩個(gè)時(shí)間相差的秒數(shù)
使用例子:
//獲得時(shí)間差
time_t t_start;
time_t t_end;
time(&t_start);
sleep(5);
time(&t_end);
printf("間隔時(shí)間= %f.\n", difftime(t_end, t_start));打?。?/strong>
間隔時(shí)間= 5.000000.
10、strftime
size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
使用strftime()函數(shù)將時(shí)間格式化為我們想要的格式。
( %a 星期幾的簡寫 %A 星期幾的全稱 %b 月分的簡寫 %B 月份的全稱 %c 標(biāo)準(zhǔn)的日期的時(shí)間串 %C 年份的后兩位數(shù)字 %d 十進(jìn)制表示的每月的第幾天 %D 月/天/年 %e 在兩字符域中,十進(jìn)制表示的每月的第幾天 %F 年-月-日 %g 年份的后兩位數(shù)字,使用基于周的年 %G 年分,使用基于周的年 %h 簡寫的月份名 %H 24小時(shí)制的小時(shí) %I 12小時(shí)制的小時(shí) %j 十進(jìn)制表示的每年的第幾天 %m 十進(jìn)制表示的月份 %M 十時(shí)制表示的分鐘數(shù) %n 新行符 %p 本地的AM或PM的等價(jià)顯示 %r 12小時(shí)的時(shí)間 %R 顯示小時(shí)和分鐘:hh:mm %S 十進(jìn)制的秒數(shù) %t 水平制表符 %T 顯示時(shí)分秒:hh:mm:ss %u 每周的第幾天,星期一為第一天 (值從0到6,星期一為0) %U 第年的第幾周,把星期日做為第一天(值從0到53) %V 每年的第幾周,使用基于周的年 %w 十進(jìn)制表示的星期幾(值從0到6,星期天為0) %W 每年的第幾周,把星期一做為第一天(值從0到53) %x 標(biāo)準(zhǔn)的日期串 %X 標(biāo)準(zhǔn)的時(shí)間串 %y 不帶世紀(jì)的十進(jìn)制年份(值從0到99) %Y 帶世紀(jì)部分的十進(jìn)制年份 %z,%Z 時(shí)區(qū)名稱,如果不能得到時(shí)區(qū)名稱則返回空字符。 %% 百分號 )
使用例子:
time_t t_time;
char buf[128];
struct tm* tm_ptr = NULL;
time(&t_time);
tm_ptr = localtime(&t_time);
//2017-12-21 18:53:58
strftime(buf, 64, "%Y-%m-%d %H:%M:%S", tm_ptr);
strftime(buf, 64, "%Y-%m-%d --- %H:%M:%S", tm_ptr);
printf("formatTimeString = %s.\n", buf);打?。?/strong>
formatTimeString = 2017-12-21 18:53:58. formatTimeString = 2017-12-21 --- 18:54:46.
11、strptime
功能和 strftime 功能相反, 將字符串格式化為一個(gè)tm結(jié)構(gòu)。
size_t strftime(char *s,size_t maxsize,char *format,const struct tm *timeptr);
使用例子:
char buf[] = "2017-12-21 --- 18:54:46";
struct tm tm_ptr;
//2017-12-21 18:53:58
strptime(buf, "%Y-%m-%d --- %H:%M:%S", &tm_ptr);
printf("----strptime-----, the time is: \
\n yday = %d \
\n wday = %d \
\n year = %d \
\n mon = %d \
\n mday = %d \
\n hour = %d \
\n min = %d \
\n sec = %d.\n",
tm_ptr.tm_yday,
tm_ptr.tm_wday,
tm_ptr.tm_year,
tm_ptr.tm_mon,
tm_ptr.tm_mday,
tm_ptr.tm_hour,
tm_ptr.tm_min,
tm_ptr.tm_sec
);
打?。?/strong>
----strptime-----, the time is: yday = 354 wday = 4 year = 117 mon = 11 mday = 21 hour = 18 min = 54 sec = 46.
以上這篇linux時(shí)間函數(shù)與時(shí)間格式與字符串之間的轉(zhuǎn)化方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信