C++時間與字串轉換

來源:互聯網
上載者:User

1、常用的時間儲存方式

1)time_t類型,這本質上是一個長整數,表示從1970-01-01 00:00:00到目前計時時間的秒數,如果需要更精確一點的,
可以使用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。

2、常用的時間函數

time_t time(time_t *t); //取得從1970年1月1日至今的秒數 char *asctime(const struct tm *tm); //將結構中的資訊轉換為真實世界的時間,以字串的形式顯示 char *ctime(const time_t *timep); //將timep轉換為真是世界的時間,以字串顯示,它和asctime不同就在於傳入的參數形式不一樣 struct tm *gmtime(const time_t *timep); //將time_t表示的時間轉換為沒有經過時區轉換的UTC時間,是一個struct tm結構指標  struct tm *localtime(const time_t *timep); //和gmtime類似,但是它是經過時區轉換的時間。 time_t mktime(struct tm *tm); //將struct tm 結構的時間轉換為從1970年至今的秒數 int gettimeofday(struct timeval *tv, struct timezone *tz); //返回當前距離1970年的秒數和微妙數,後面的tz是時區,一般不用 double difftime(time_t time1, time_t time2); //返回兩個時間相差的秒數

3、時間與字串的轉換

需要包含的標頭檔如下

#include <iostream> #include <time.h> #include <stdlib.h> #include <string.h>

1)unix/windows下時間轉字串參考代碼

void DatetimeToString() { time_t t; //秒時間 tm* local; //本地時間  tm* gmt; //格林威治時間 char buf[128]= {0}; t = time(NULL); //擷取目前秒時間 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字串轉時間參考代碼

void StringToDatetime() { 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; //秒數加3600  tm_ = *localtime(&t_);//輸出時間 strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &tm_); std::cout << buf << std::endl; }

3)由於windows下沒有strptime函數,所以可以使用scanf來格式化

time_t StringToDatetime(char *str) { tm tm_; int year, month, day, hour, minute,second; sscanf(str,"%d-%d-%d %d:%d:%d", &tm_.year, &tm_.month, &tm_.day, &tm_.hour, &tm_.minute, &tm_.second); tm_.tm_year = year-1900; tm_.tm_mon = month; 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_); return t_; //秒時間 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.