time_t tm timeval 和 時間字符串的轉換方法
1、常用的時間存儲方式
1)time_t類型,這本質上是一個長整數(shù),表示從1970-01-01 00:00:00到目前計時時間的秒數(shù),如果需要更精確一點的,可以使用timeval精確到毫秒。
2)tm結構,這本質上是一個結構體,里面包含了各時間字段
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年到目前計時時間間隔多少年,如果是手動設置值的話,tm_isdst通常取值-1。
3)struct timeval結構體在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); //將結構中的信息轉換為真實世界的時間,以字符串的形式顯示 char *ctime(const time_t *timep); //將timep轉換為真是世界的時間,以字符串顯示,它和asctime不同就在于傳入的參數(shù)形式不一樣 struct tm *gmtime(const time_t *timep); //將time_t表示的時間轉換為沒有經過時區(qū)轉換的UTC時間,是一個struct tm結構指針 struct tm *localtime(const time_t *timep); //和gmtime類似,但是它是經過時區(qū)轉換的時間。 time_t mktime(struct tm *tm); //將struct tm 結構的時間轉換為從1970年至今的秒數(shù) int gettimeofday(struct timeval *tv, struct timezone *tz); //返回當前距離1970年的秒數(shù)和微妙數(shù),后面的tz是時區(qū),一般不用 double difftime(time_t time1, time_t time2); //返回兩個時間相差的秒數(shù)
3、時間與字符串的轉換
需要包含的頭文件如下
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string.h>
1)unix/windows下時間轉字符串參考代碼
time_t t; //秒時間 tm* local; //本地時間 tm* gmt; //格林威治時間 char buf[128]= {0}; t = time(NULL); //或者time(&t);//獲取目前秒時間 local = localtime(&t); //轉為本地時間 strftime(buf, 64, "%Y-%m-%d %H:%M:%S", local); std::cout << buf << std::endl; gmt = gmtime(&t);//轉為格林威治時間 strftime(buf, 64, "%Y-%m-%d %H:%M:%S", gmt); std::cout << buf << std::endl;
2)unix字符串轉時間參考代碼
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_); //將字符串轉換為tm時間 tm_.tm_isdst = -1; t_ = mktime(&tm_); //將tm時間轉換為秒時間 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_); //已經減了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、關于localtime與localtime_r的區(qū)別
struct tm *localtime(const time_t *timep);
這個函數(shù)在返回的時候,返回的是一個指針,實際的內存是localtime內部通過static申請的靜態(tài)內存,所以通過localtime調用后的返回值不及時使用的話,很有可能被其他線程localtime調用所覆蓋掉
struct tm *localtime_r(const time_t *timep, struct tm *result);
localtime_r則是由調用者在第二個參數(shù)傳入一個struct tm result指針,該函數(shù)會把結果填充到這個傳入的指針所指內存里面;成功的返回值指針也就是struct tm result。
多線程應用里面,應該用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 和 時間字符串的轉換方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Ubuntu Server 11.10安裝配置lamp(Apache+MySQL+PHP)
這篇文章主要介紹了Ubuntu Server 11.10安裝配置lamp(Apache+MySQL+PHP),需要的朋友可以參考下2016-10-10linux后臺執(zhí)行命令&和nohup的具體使用方法
這篇文章主要介紹了linux后臺執(zhí)行命令&和nohup的具體使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09Linux終端提示符(prompt)不如期生效的原因分析與解決
Linux命令行是系統(tǒng)管理員管理Linux的重要手段,我們管理Linux,首先要面對的就是Linux命令行提示符。下面這篇文章主要給大家介紹了Linux終端提示符(prompt)不如期生效的原因以及解決方法,需要的朋友可以參考下。2017-07-07