發(fā)現(xiàn)項(xiàng)目中經(jīng)常會(huì)用到時(shí)間相關(guān)的內(nèi)容,但是網(wǎng)上關(guān)于時(shí)間函數(shù)的講述很亂(主要代碼比較亂),這里一點(diǎn)那里一點(diǎn),于是我準(zhǔn)備把c++關(guān)于時(shí)間函數(shù)的用法做一個(gè)簡(jiǎn)單的匯總,方便以后的使用
使用了c庫(kù)函數(shù)版本的時(shí)間函數(shù),做了一些代碼示范,包括了時(shí)間格式和時(shí)間戳之間的相互轉(zhuǎn)換
include#include#include#includeusing namespace std;
time_t StringToDatetimeS(std::string str) //s級(jí)轉(zhuǎn)換
{tm tm_;
int year, month, day, hour, minute, second;
year = stoi((str.substr(0, 4)).c_str());
month = stoi((str.substr(5, 2)).c_str());
day = stoi((str.substr(8, 2)).c_str());
hour = stoi((str.substr(11, 2)).c_str());
minute = stoi((str.substr(14, 2)).c_str());
second = stoi((str.substr(17, 2)).c_str());
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_); // 將tm結(jié)構(gòu)體轉(zhuǎn)換成time_t格式。
return t_;
}
time_t StringToDatetimeMS(std::string str) //ms級(jí)轉(zhuǎn)換
{tm tm_;
int year, month, day, hour, minute, second, msecond;
year = stoi((str.substr(0, 4)).c_str());
month = stoi((str.substr(5, 2)).c_str());
day = stoi((str.substr(8, 2)).c_str());
hour = stoi((str.substr(11, 2)).c_str());
minute = stoi((str.substr(14, 2)).c_str());
second = stoi((str.substr(17, 2)).c_str());
msecond = stoi((str.substr(20, 3)).c_str());
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_)*1000+ msecond; // 將tm結(jié)構(gòu)體轉(zhuǎn)換成time_t格式。
return t_;
}
int main()
{//把當(dāng)前時(shí)間轉(zhuǎn)換為時(shí)間格式 當(dāng)然由于tm的格式限制,只能精確到s
time_t now = time(NULL);
struct tm tm_t;
localtime_s(&tm_t,&now);
char pTmpString[256] = {0 };
sprintf_s(pTmpString, "%04d-%02d-%02d %02d:%02d:%02d", tm_t.tm_year + 1900, tm_t.tm_mon + 1, tm_t.tm_mday, tm_t.tm_hour, tm_t.tm_min, tm_t.tm_sec);
string str = pTmpString;
cout<< pTmpString<< endl;
//若要精確到ms,可以調(diào)用系統(tǒng)的自帶的GetSystemTime,GetLocalTime
//GetSystemTime::獲取的是UTC時(shí)間,若要轉(zhuǎn)換為北京時(shí)間,需要hour+8
//GetLocalTime:獲取的北京時(shí)間
SYSTEMTIME tm_t2 = {0 };
GetSystemTime(&tm_t2);
char pTmpString2[256] = {0 };
sprintf_s(pTmpString2, "%04d-%02d-%02d %02d:%02d:%02d:%03d", tm_t2.wYear, tm_t2.wMonth, tm_t2.wDay, tm_t2.wHour+8, tm_t2.wMinute, tm_t2.wSecond, tm_t2.wMilliseconds);
string str2 = pTmpString2;
cout<< str2<< endl;
SYSTEMTIME tm_t3 = {0 };
GetLocalTime(&tm_t3);
char pTmpString3[256] = {0 };
sprintf_s(pTmpString3, "%04d-%02d-%02d %02d:%02d:%02d:%03d", tm_t3.wYear, tm_t3.wMonth, tm_t3.wDay, tm_t3.wHour, tm_t3.wMinute, tm_t3.wSecond, tm_t3.wMilliseconds);
string str3 = pTmpString3;
cout<< str3<< endl;
//下面是時(shí)間格式轉(zhuǎn)換為時(shí)間戳分別和上面對(duì)應(yīng) 13位的時(shí)間戳,其精度是毫秒(ms);10位的時(shí)間戳,其精度是秒(s);
//s級(jí)別轉(zhuǎn)換
if (StringToDatetimeS(pTmpString) == now)
{ cout<< "s級(jí)別轉(zhuǎn)換成功!?。?<< endl;
}
//ms轉(zhuǎn)換加上ms即可StringToDatetimeMS
}
2022-11-29 16:12:40
2022-11-29 16:12:40:317
2022-11-29 16:12:40:317
s級(jí)別轉(zhuǎn)換成功?。?!
二、使用c++11的庫(kù)函數(shù) std::chronoc++11為我們提供了三種時(shí)間類型,并且每一種都提供了一個(gè)函數(shù)now()來獲取當(dāng)前時(shí)間
并且這三個(gè)時(shí)間類都提供了rep(周期)、period(單位比率)、duration(成員類型)。
system_clock:系統(tǒng)時(shí)間。system_clock除了now()函數(shù)外,還提供了to_time_t()靜態(tài)成員函數(shù)。用于將系統(tǒng)時(shí)間轉(zhuǎn)換成熟悉的std::time_t類型,得到了std::time_t類型的值,就可以很方便地打印當(dāng)前時(shí)間了。
steady_clock:是單調(diào)的時(shí)鐘,相當(dāng)于教練手中的秒表;只會(huì)增長(zhǎng),適合用于記錄程序耗時(shí)。
high_resolution_clock:是當(dāng)前系統(tǒng)能夠提供的最高精度的時(shí)鐘;
獲取時(shí)間和做時(shí)間戳和時(shí)間格式的互換用system_clock
#include#include#include#includeusing namespace std;
time_t StringToDatetimeMS(std::string str) //ms級(jí)轉(zhuǎn)換
{tm tm_;
int year, month, day, hour, minute, second, msecond;
year = stoi((str.substr(0, 4)).c_str());
month = stoi((str.substr(5, 2)).c_str());
day = stoi((str.substr(8, 2)).c_str());
hour = stoi((str.substr(11, 2)).c_str());
minute = stoi((str.substr(14, 2)).c_str());
second = stoi((str.substr(17, 2)).c_str());
msecond = stoi((str.substr(20, 3)).c_str());
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_) * 1000 + msecond; // 將tm結(jié)構(gòu)體轉(zhuǎn)換成time_t格式。
return t_;
}
int main()
{//system_clock獲取當(dāng)前時(shí)間(可以獲取各種級(jí)別的時(shí)間戳)
auto tp = chrono::system_clock::now();
time_t nowtime = chrono::system_clock::to_time_t(tp);
chrono::milliseconds ms = chrono::duration_cast(tp.time_since_epoch());
//同理 微秒cs
chrono::microseconds cs = chrono::duration_cast(tp.time_since_epoch());
//納秒 us
chrono::nanoseconds us = chrono::duration_cast(tp.time_since_epoch());
//轉(zhuǎn)化為各種日期格式
struct tm tm_t;
localtime_s(&tm_t, &nowtime);
char buffer[128];
strftime(buffer, sizeof(buffer), "%F %T", &tm_t);
std::ostringstream ss;//用于拼接數(shù)字和字符串。
ss.fill('0');
cout<< buffer<< endl;
ss<< buffer<< ":"<< (ms % 1000).count(); //%1000表示除去s之后剩余那部分時(shí)間
string str1 = ss.str();
cout<< str1<< endl;
ss<< ":"<< ((cs % 1000000).count()) % 1000; //%1000000表示除去s之后剩余那部分時(shí)間,需要進(jìn)一步%1000才是剩余的微秒部分
string str2 = ss.str();
cout<< str2<< endl;
ss<< ":"<< (((us % 1000000000).count()) % 1000)%1000;//%1000000000表示除去s之后剩余那部分時(shí)間,需要進(jìn)一步%1000才是剩余的微秒部分,再%1000就是剩余us部分
string str3 = ss.str();
cout<< str3<< endl;
//把日期格式轉(zhuǎn)回時(shí)間戳,這里沒有直接的轉(zhuǎn)換函數(shù),用回上述的c版本轉(zhuǎn)換即可。
//即先轉(zhuǎn)成tm,再轉(zhuǎn)成以s為單位的時(shí)間戳,剩余部分換算相加即可。這里以
//StringToDatetimeMS為示范
if (StringToDatetimeMS(str1) == ms.count())
{cout<< "ms時(shí)間戳轉(zhuǎn)換成功"<< endl;
}
return 0;
}
2022-11-29 17:59:08
2022-11-29 17:59:08:210
2022-11-29 17:59:08:210:690
2022-11-29 17:59:08:210:690:500
ms時(shí)間戳轉(zhuǎn)換成功
//如果是計(jì)算程序的耗時(shí),用steady_clock,示例如下
#include#include//表示時(shí)間單位的庫(kù)
#include#include#includeusing namespace std;
using namespace std::chrono;
int main()
{auto begin = chrono::duration_cast(steady_clock::now().time_since_epoch()); //獲取系統(tǒng)啟動(dòng)后到現(xiàn)在的時(shí)間點(diǎn),這里強(qiáng)轉(zhuǎn)可能會(huì)有精度損失
for (int i = 0; i< 1; ++i)
{std::this_thread::sleep_for(seconds(1));
std::cout<< i<< std::endl;
}
auto end = chrono::duration_cast(steady_clock::now().time_since_epoch()); //獲取系統(tǒng)啟動(dòng)后到現(xiàn)在的時(shí)間點(diǎn),這里強(qiáng)轉(zhuǎn)可能會(huì)有精度損失
auto diff = (end - begin).count();
cout<< "diff="<< diff<< std::endl; //單位默認(rèn)是納秒
return 0;
}
0
diff=1000
這里主要參考下面這篇文章
https://blog.csdn.net/t46414704152abc/article/details/103531447?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166971236216800215045777%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=166971236216800215045777&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-103531447-null-null.142^v67^control,201^v3^add_ask,213^v2^t3_esquery_v2&utm_term=c%2B%2Bchrono%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4&spm=1018.2226.3001.4187
至于第三種high_resolution_clock可以看成steady_clock的升級(jí)版,其實(shí)用法沒什么區(qū)別,只是精度更高而已。
總結(jié)對(duì)一些常見的時(shí)間函數(shù)做了歸納和舉例,方便日后使用
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
文章名稱:C++時(shí)間函數(shù)歸納-創(chuàng)新互聯(lián)
地址分享:http://www.rwnh.cn/article24/cegdce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、微信小程序、網(wǎng)站維護(hù)、App設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)公司、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容