time_t tm timeval 和 時間字符串的轉(zhuǎn)換方法
1、常用的時間存儲方式
1)time_t類型,這本質(zhì)上是一個長整數(shù),表示從1970-01-01 00:00:00到目前計時時間的秒數(shù),如果需要更精確一點(diǎn)的,可以使用timeval精確到毫秒。
2)tm結(jié)構(gòu),這本質(zhì)上是一個結(jié)構(gòu)體,里面包含了各時間字段
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年到目前計時時間間隔多少年,如果是手動設(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ù)
time_t time(time_t *t); //取得從1970年1月1日至今的秒數(shù) char *asctime(const struct tm *tm); //將結(jié)構(gòu)中的信息轉(zhuǎn)換為真實世界的時間,以字符串的形式顯示 char *ctime(const time_t *timep); //將timep轉(zhuǎn)換為真是世界的時間,以字符串顯示,它和asctime不同就在于傳入的參數(shù)形式不一樣 struct tm *gmtime(const time_t *timep); //將time_t表示的時間轉(zhuǎn)換為沒有經(jīng)過時區(qū)轉(zhuǎn)換的UTC時間,是一個struct tm結(jié)構(gòu)指針 struct tm *localtime(const time_t *timep); //和gmtime類似,但是它是經(jīng)過時區(qū)轉(zhuǎn)換的時間。 time_t mktime(struct tm *tm); //將struct tm 結(jié)構(gòu)的時間轉(zhuǎn)換為從1970年至今的秒數(shù) int gettimeofday(struct timeval *tv, struct timezone *tz); //返回當(dāng)前距離1970年的秒數(shù)和微妙數(shù),后面的tz是時區(qū),一般不用 double difftime(time_t time1, time_t time2); //返回兩個時間相差的秒數(shù)
3、時間與字符串的轉(zhuǎn)換
需要包含的頭文件如下
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string.h>
1)unix/windows下時間轉(zhuǎn)字符串參考代碼
time_t t; //秒時間
tm* local; //本地時間
tm* gmt; //格林威治時間
char buf[128]= {0};
t = time(NULL); //或者time(&t);//獲取目前秒時間
local = localtime(&t); //轉(zhuǎn)為本地時間
strftime(buf, 64, "%Y-%m-%d %H:%M:%S", local);
std::cout << buf << std::endl;
gmt = gmtime(&t);//轉(zhuǎn)為格林威治時間
strftime(buf, 64, "%Y-%m-%d %H:%M:%S", gmt);
std::cout << buf << std::endl;
2)unix字符串轉(zhuǎn)時間參考代碼
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時間
tm_.tm_isdst = -1;
t_ = mktime(&tm_); //將tm時間轉(zhuǎn)換為秒時間
t_ += 3600; //秒數(shù)加3600
tm_ = *localtime(&t_);//輸出時間
strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &tm_);
std::cout << buf << std::endl;
3)由于windows下沒有strptime函數(shù),所以可以使用scanf來格式化
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個時區(qū)
return t_; //秒時間
}
4)timeval獲取時間示例:
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);//計算時間差104./**
* 計算兩個時間的間隔,得到時間差
* @param struct timeval* resule 返回計算出來的時間
* @param struct timeval* x 需要計算的前一個時間
* @param struct timeval* y 需要計算的后一個時間
* 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);
這個函數(shù)在返回的時候,返回的是一個指針,實際的內(nèi)存是localtime內(nèi)部通過static申請的靜態(tài)內(nèi)存,所以通過localtime調(diào)用后的返回值不及時使用的話,很有可能被其他線程localtime調(diào)用所覆蓋掉
struct tm *localtime_r(const time_t *timep, struct tm *result);
localtime_r則是由調(diào)用者在第二個參數(shù)傳入一個struct tm result指針,該函數(shù)會把結(jié)果填充到這個傳入的指針?biāo)竷?nèi)存里面;成功的返回值指針也就是struct tm result。
多線程應(yīng)用里面,應(yīng)該用localtime_r函數(shù)替代localtime函數(shù),因為localtime_r是線程安全的。
其他的時間函數(shù),如asctime,asctime_r;ctime,ctime_r;gmtime,gmtime_r都是類似的,所以,<strong>時間函數(shù)的 _r 版本都是線程安全的。</strong>
</span>
以上這篇time_t tm timeval 和 時間字符串的轉(zhuǎn)換方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
linux下RPM包安裝基于xinetd的服務(wù)的管理
大家好,本篇文章主要講的是linux下RPM包安裝基于xinetd的服務(wù)的管理,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽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)的實現(xiàn)方法
這篇文章主要介紹了Linux下gdb調(diào)試之打斷點(diǎn)的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09

