time_t tm timeval 和 時(shí)間字符串的轉(zhuǎn)換方法
1、常用的時(shí)間存儲(chǔ)方式
1)time_t類型,這本質(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)換為沒有經(jīng)過(guò)時(shí)區(qū)轉(zhuǎn)換的UTC時(shí)間,是一個(gè)struct tm結(jié)構(gòu)指針 struct tm *localtime(const time_t *timep); //和gmtime類似,但是它是經(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下沒有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都是類似的,所以,<strong>時(shí)間函數(shù)的 _r 版本都是線程安全的。</strong>
</span>
以上這篇time_t tm timeval 和 時(shí)間字符串的轉(zhuǎn)換方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
linux下RPM包安裝基于xinetd的服務(wù)的管理
大家好,本篇文章主要講的是linux下RPM包安裝基于xinetd的服務(wù)的管理,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Linux 系統(tǒng)優(yōu)化的一些建議(內(nèi)核優(yōu)化)
這篇文章主要介紹了Linux 系統(tǒng)優(yōu)化的一些建議,幫助大家更好的使用Linux,感興趣的朋友可以了解下2020-08-08
Linux下gdb調(diào)試之打斷點(diǎn)的實(shí)現(xiàn)方法
這篇文章主要介紹了Linux下gdb調(diào)試之打斷點(diǎn)的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

