C++實(shí)現(xiàn)時(shí)間轉(zhuǎn)換及格式化
寫了一些時(shí)間轉(zhuǎn)換及格式化相關(guān)的函數(shù),經(jīng)測試能夠跨平臺(tái)使用,記錄一下。
#include <cstdio> #include <chrono> #include <iostream> using namespace std; time_t GetTime() { chrono::system_clock::time_point now = chrono::system_clock::now(); return chrono::system_clock::to_time_t(now); } tm* GetUtcTm() { time_t t = GetTime(); return gmtime(&t); } tm* GetLocalTm() { time_t t = GetTime(); return localtime(&t); } string GetUtcDateTime() { auto t = GetTime(); auto localTm = gmtime(&t); char buff[32]; strftime(buff, 32, "%Y%m%d-%H:%M:%S", localTm); return string(buff); } string GetUtcDate() { auto t = GetTime(); auto localTm = gmtime(&t); char buff[32]; strftime(buff, 32, "%Y%m%d", localTm); return string(buff); } string GetUtcTime() { auto t = GetTime(); auto localTm = gmtime(&t); char buff[32]; strftime(buff, 32, "%H:%M:%S", localTm); return string(buff); } string GetUtcDateTimeWithMilliSecond() { auto now = chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now()); time_t t = chrono::system_clock::to_time_t(now); int milliSecond = now.time_since_epoch().count() % 1000; auto localTm = gmtime(&t); char buff[32]; int len = strftime(buff, 32, "%Y%m%d-%H:%M:%S", localTm); sprintf(buff + len, ".%03u", milliSecond); return string(buff); } string GetLocalDateTime() { auto t = GetTime(); auto localTm = localtime(&t); char buff[32]; strftime(buff, 32, "%Y%m%d-%H:%M:%S", localTm); return string(buff); } string GetLocalDate() { auto t = GetTime(); auto localTm = localtime(&t); char buff[32]; strftime(buff, 32, "%Y%m%d", localTm); return string(buff); } string GetLocalTime() { auto t = GetTime(); auto localTm = localtime(&t); char buff[32]; strftime(buff, 32, "%H:%M:%S", localTm); return string(buff); } string GetLocalDateTimeWithMilliSecond() { auto now = chrono::time_point_cast<chrono::milliseconds>(chrono::system_clock::now()); time_t t = chrono::system_clock::to_time_t(now); int milliSecond = now.time_since_epoch().count() % 1000; auto localTm = localtime(&t); char buff[32]; int len = strftime(buff, 32, "%Y%m%d-%H:%M:%S", localTm); sprintf(buff + len, ".%03u", milliSecond); return string(buff); } string GetLocalDateFromUnixTimeStamp(long long timeStamp) { time_t time = timeStamp / 1000000000LL; static char buff[16]; int len = strftime(buff, 16, "%Y%m%d", localtime(&time)); return string(buff); } string GetLocalTimeFromUnixTimeStamp(long long timeStamp) { time_t time = timeStamp / 1000000000LL; static char buff[16]; int len = strftime(buff, 16, "%H:%M:%S", localtime(&time)); return string(buff); } time_t GetTimeFromString(string dateTime, string format = "%04d%02d%02d-%02d:%02d:%02d") { tm t; int len = sscanf(dateTime.c_str(), format.c_str(), &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec); t.tm_year -= 1900; t.tm_mon -= 1; return mktime(&t); } string ToUtcDateTime(time_t* time) { char buff[32]; strftime(buff, 32, "%Y%m%d-%H:%M:%S", gmtime(time)); return string(buff); } string ToUtcDate(time_t* time) { char buff[32]; strftime(buff, 32, "%Y%m%d", gmtime(time)); return string(buff); } string ToUtcTime(time_t* time) { char buff[32]; strftime(buff, 32, "%H:%M:%S", gmtime(time)); return string(buff); } string ToLocalDateTime(time_t* time) { char buff[32]; strftime(buff, 32, "%Y%m%d-%H:%M:%S", localtime(time)); return string(buff); } string ToLocalDate(time_t* time) { char buff[32]; strftime(buff, 32, "%Y%m%d", localtime(time)); return string(buff); } string ToLocalTime(time_t* time) { char buff[32]; strftime(buff, 32, "%H:%M:%S", localtime(time)); return string(buff); } int main() { char buff[32]; strftime(buff, 32, "%Y%m%d %H:%M:%S", GetUtcTm()); cout << buff << endl; strftime(buff, 32, "%Y%m%d %H:%M:%S", GetLocalTm()); cout << buff << endl << endl; cout << GetUtcDateTime() << endl; cout << GetUtcDate() << endl; cout << GetUtcTime() << endl; cout << GetUtcDateTimeWithMilliSecond() << endl << endl; cout << GetLocalDateTime() << endl; cout << GetLocalDate() << endl; cout << GetLocalTime() << endl; cout << GetLocalDateTimeWithMilliSecond() << endl << endl; cout << GetLocalDateFromUnixTimeStamp(1635754321199409000L) << endl; cout << GetLocalTimeFromUnixTimeStamp(1635754321199409000L) << endl << endl; auto time = GetTimeFromString("20211101-08:12:01.224"); cout << ToUtcDateTime(&time) << endl; cout << ToUtcDate(&time) << endl; cout << ToUtcTime(&time) << endl; cout << ToLocalDateTime(&time) << endl; cout << ToLocalDate(&time) << endl; cout << ToLocalTime(&time) << endl << endl; return 0; }
輸出:
20211103 05:46:16
20211103 13:46:16
20211103-05:46:16
20211103
05:46:16
20211103-05:46:16.218
20211103-13:46:16
20211103
13:46:16
20211103-13:46:16.219
20211101
16:12:01
20211101-00:12:01
20211101
00:12:01
20211101-08:12:01
20211101
08:12:01
到此這篇關(guān)于C++實(shí)現(xiàn)時(shí)間轉(zhuǎn)換及格式化的文章就介紹到這了,更多相關(guān)C++時(shí)間轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++數(shù)據(jù)結(jié)構(gòu)二叉搜索樹的實(shí)現(xiàn)應(yīng)用與分析
從這篇博客開始,我就要和大家介紹有關(guān)二叉搜索樹的知識(shí),它還衍生出了兩棵樹——AVL樹和紅黑樹,在后面兩篇博客我都會(huì)介紹。今天先從二叉搜索樹開始引入2022-02-02Qt創(chuàng)建項(xiàng)目實(shí)戰(zhàn)之手把手創(chuàng)建第一個(gè)Qt項(xiàng)目
我們在進(jìn)行軟件開發(fā)學(xué)習(xí)時(shí),有時(shí)候需要qt軟件進(jìn)行代碼的敲寫,下面這篇文章主要給大家介紹了關(guān)于Qt創(chuàng)建項(xiàng)目實(shí)戰(zhàn)之手把手創(chuàng)建第一個(gè)Qt項(xiàng)目的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04c++中的volatile和variant關(guān)鍵字詳解
大家好,本篇文章主要講的是c++中的volatile和variant關(guān)鍵字詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01AVX2指令集優(yōu)化浮點(diǎn)數(shù)組求和算法
這篇文章主要為大家介紹了AVX2指令集優(yōu)化浮點(diǎn)數(shù)組求和算法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05