time_t tm timeval 和 時(shí)間字符串的轉(zhuǎn)換方法
1、常用的時(shí)間存儲(chǔ)方式
1)time_t類(lèi)型,這本質(zhì)上是一個(gè)長(zhǎng)整數(shù),表示從1970-01-01 00:00:00到目前計(jì)時(shí)時(shí)間的秒數(shù),如果需要更精確一點(diǎn)的,可以使用timeval精確到毫秒。
2)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í)間間隔多少年,如果是手動(dòng)設(shè)置值的話,tm_isdst通常取值-1。
3)struct timeval結(jié)構(gòu)體在time.h中的定義為
struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ };
2、常用的時(shí)間函數(shù)
time_t time(time_t *t); //取得從1970年1月1日至今的秒數(shù) char *asctime(const struct tm *tm); //將結(jié)構(gòu)中的信息轉(zhuǎn)換為真實(shí)世界的時(shí)間,以字符串的形式顯示 char *ctime(const time_t *timep); //將timep轉(zhuǎn)換為真是世界的時(shí)間,以字符串顯示,它和asctime不同就在于傳入的參數(shù)形式不一樣 struct tm *gmtime(const time_t *timep); //將time_t表示的時(shí)間轉(zhuǎn)換為沒(méi)有經(jīng)過(guò)時(shí)區(qū)轉(zhuǎn)換的UTC時(shí)間,是一個(gè)struct tm結(jié)構(gòu)指針 struct tm *localtime(const time_t *timep); //和gmtime類(lèi)似,但是它是經(jīng)過(guò)時(shí)區(qū)轉(zhuǎn)換的時(shí)間。 time_t mktime(struct tm *tm); //將struct tm 結(jié)構(gòu)的時(shí)間轉(zhuǎn)換為從1970年至今的秒數(shù) int gettimeofday(struct timeval *tv, struct timezone *tz); //返回當(dāng)前距離1970年的秒數(shù)和微妙數(shù),后面的tz是時(shí)區(qū),一般不用 double difftime(time_t time1, time_t time2); //返回兩個(gè)時(shí)間相差的秒數(shù)
3、時(shí)間與字符串的轉(zhuǎn)換
需要包含的頭文件如下
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string.h>
1)unix/windows下時(shí)間轉(zhuǎn)字符串參考代碼
time_t t; //秒時(shí)間 tm* local; //本地時(shí)間 tm* gmt; //格林威治時(shí)間 char buf[128]= {0}; t = time(NULL); //或者time(&t);//獲取目前秒時(shí)間 local = localtime(&t); //轉(zhuǎn)為本地時(shí)間 strftime(buf, 64, "%Y-%m-%d %H:%M:%S", local); std::cout << buf << std::endl; gmt = gmtime(&t);//轉(zhuǎn)為格林威治時(shí)間 strftime(buf, 64, "%Y-%m-%d %H:%M:%S", gmt); std::cout << buf << std::endl;
2)unix字符串轉(zhuǎn)時(shí)間參考代碼
tm tm_; time_t t_; char buf[128]= {0}; strcpy(buf, "2012-01-01 14:00:00"); strptime(buf, "%Y-%m-%d %H:%M:%S", &tm_); //將字符串轉(zhuǎn)換為tm時(shí)間 tm_.tm_isdst = -1; t_ = mktime(&tm_); //將tm時(shí)間轉(zhuǎn)換為秒時(shí)間 t_ += 3600; //秒數(shù)加3600 tm_ = *localtime(&t_);//輸出時(shí)間 strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &tm_); std::cout << buf << std::endl;
3)由于windows下沒(méi)有strptime函數(shù),所以可以使用scanf來(lái)格式化
time_t StringToDatetime(char *str) { tm tm_; int year, month, day, hour, minute,second; sscanf(str,"%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second); tm_.tm_year = year-1900; tm_.tm_mon = month-1; tm_.tm_mday = day; tm_.tm_hour = hour; tm_.tm_min = minute; tm_.tm_sec = second; tm_.tm_isdst = 0; time_t t_ = mktime(&tm_); //已經(jīng)減了8個(gè)時(shí)區(qū) return t_; //秒時(shí)間 }
4)timeval獲取時(shí)間示例:
struct timeval start_time, over_time, consume_time; gettimeofday(&over_time, NULL);//get the current time start_time = over_time; do something..... gettimeofday(&over_time, NULL); timeval_subtract(&consume_time, &start_time, &over_time);//計(jì)算時(shí)間差104./** * 計(jì)算兩個(gè)時(shí)間的間隔,得到時(shí)間差 * @param struct timeval* resule 返回計(jì)算出來(lái)的時(shí)間 * @param struct timeval* x 需要計(jì)算的前一個(gè)時(shí)間 * @param struct timeval* y 需要計(jì)算的后一個(gè)時(shí)間 * return -1 failure ,0 success **/ timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* y) { if ( x->tv_sec>y->tv_sec ) return -1; if ( (x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec) ) return -1; result->tv_sec = ( y->tv_sec-x->tv_sec ); result->tv_usec = ( y->tv_usec-x->tv_usec ); if (result->tv_usec<0) { result->tv_sec--; result->tv_usec+=1000000; } return 0; }
4、關(guān)于localtime與localtime_r的區(qū)別
struct tm *localtime(const time_t *timep);
這個(gè)函數(shù)在返回的時(shí)候,返回的是一個(gè)指針,實(shí)際的內(nèi)存是localtime內(nèi)部通過(guò)static申請(qǐng)的靜態(tài)內(nèi)存,所以通過(guò)localtime調(diào)用后的返回值不及時(shí)使用的話,很有可能被其他線程localtime調(diào)用所覆蓋掉
struct tm *localtime_r(const time_t *timep, struct tm *result);
localtime_r則是由調(diào)用者在第二個(gè)參數(shù)傳入一個(gè)struct tm result指針,該函數(shù)會(huì)把結(jié)果填充到這個(gè)傳入的指針?biāo)竷?nèi)存里面;成功的返回值指針也就是struct tm result。
多線程應(yīng)用里面,應(yīng)該用localtime_r函數(shù)替代localtime函數(shù),因?yàn)閘ocaltime_r是線程安全的。
其他的時(shí)間函數(shù),如asctime,asctime_r;ctime,ctime_r;gmtime,gmtime_r都是類(lèi)似的,所以,<strong>時(shí)間函數(shù)的 _r 版本都是線程安全的。</strong>
</span>
以上這篇time_t tm timeval 和 時(shí)間字符串的轉(zhuǎn)換方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
crontab定時(shí)任務(wù)不執(zhí)行的原因分析與解決方法
這篇文章主要給大家介紹了關(guān)于crontab定時(shí)任務(wù)不執(zhí)行的原因分析與解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01Ubuntu Server 11.10安裝配置lamp(Apache+MySQL+PHP)
這篇文章主要介紹了Ubuntu Server 11.10安裝配置lamp(Apache+MySQL+PHP),需要的朋友可以參考下2016-10-10解決Linux中ifconfig和addr查看不到ip問(wèn)題
這篇文章主要介紹了解決Linux中ifconfig和addr查看不到ip問(wèn)題,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07linux后臺(tái)執(zhí)行命令&和nohup的具體使用方法
這篇文章主要介紹了linux后臺(tái)執(zhí)行命令&和nohup的具體使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Ubuntu 18.04通過(guò)命令禁用/開(kāi)啟觸控板
這篇文章主要介紹了Ubuntu 18.04通過(guò)命令禁用/開(kāi)啟觸控板,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11Linux系統(tǒng)設(shè)置tomcat開(kāi)機(jī)自啟介紹
大家好,本篇文章主要講的是Linux系統(tǒng)設(shè)置tomcat開(kāi)機(jī)自啟介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12rsync如何實(shí)現(xiàn)斷點(diǎn)續(xù)傳
rsync命令是一種高效的文件傳輸工具,相較于scp命令,它具有支持?jǐn)帱c(diǎn)續(xù)傳和僅拷貝修改過(guò)的文件等優(yōu)勢(shì),大大提高了文件傳輸?shù)男?特別適用于傳輸大文件和定期同步文件夾,通過(guò)參數(shù)配置,rsync能夠?qū)崿F(xiàn)多種高級(jí)功能2024-10-10Linux終端提示符(prompt)不如期生效的原因分析與解決
Linux命令行是系統(tǒng)管理員管理Linux的重要手段,我們管理Linux,首先要面對(duì)的就是Linux命令行提示符。下面這篇文章主要給大家介紹了Linux終端提示符(prompt)不如期生效的原因以及解決方法,需要的朋友可以參考下。2017-07-07