最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實(shí)現(xiàn)時(shí)間轉(zhuǎn)換及格式化

 更新時(shí)間:2023年11月22日 09:21:49   作者:紫云星  
這篇文章主要為大家詳細(xì)介紹了C++中實(shí)現(xiàn)時(shí)間轉(zhuǎn)換及格式化的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

寫了一些時(shí)間轉(zhuǎn)換及格式化相關(guān)的函數(shù),經(jīng)測試能夠跨平臺使用,記錄一下。

#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++常用的#include頭文件總結(jié)

    C++常用的#include頭文件總結(jié)

    這篇文章主要介紹了C++常用的#include頭文件,對初學(xué)者理解C++程序設(shè)計(jì)大有好處的相關(guān)資料
    2014-07-07
  • C++ 命名空間詳解

    C++ 命名空間詳解

    這篇文章主要介紹了C++ 命名空間的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2021-11-11
  • C++實(shí)現(xiàn)簡單版通訊錄管理系統(tǒng)

    C++實(shí)現(xiàn)簡單版通訊錄管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡單版通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • c語言中使用BF-KMP算法實(shí)例

    c語言中使用BF-KMP算法實(shí)例

    這篇文章主要介紹了c語言中使用BF-KMP算法,大家參考使用
    2013-11-11
  • C++常見異常處理原理及代碼示例解析

    C++常見異常處理原理及代碼示例解析

    這篇文章主要介紹了C++常見異常處理原理及代碼示例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Cocos2d-x中獲取系統(tǒng)時(shí)間和隨機(jī)數(shù)實(shí)例

    Cocos2d-x中獲取系統(tǒng)時(shí)間和隨機(jī)數(shù)實(shí)例

    這篇文章主要介紹了Cocos2d-x中獲取系統(tǒng)時(shí)間和隨機(jī)數(shù)實(shí)例,本文代碼含有大量注釋來講解獲取系統(tǒng)時(shí)間和隨機(jī)數(shù)的方法,需要的朋友可以參考下
    2014-09-09
  • 深入解析C++中的虛函數(shù)與多態(tài)

    深入解析C++中的虛函數(shù)與多態(tài)

    對C++ 了解的人都應(yīng)該知道虛函數(shù)(Virtual Function)是通過一張?zhí)摵瘮?shù)表(Virtual Table)和一個(gè)指向虛函數(shù)表的指針(vptr)來實(shí)現(xiàn)的
    2013-09-09
  • C++中LibCurl庫的使用教程分享

    C++中LibCurl庫的使用教程分享

    LibCurl是一個(gè)開源的免費(fèi)的多協(xié)議數(shù)據(jù)傳輸開源庫,該框架具備跨平臺性,開源免費(fèi),這篇文章主要為大家介紹了如何在C++中使用LibCurl庫,需要的可以參考下
    2023-08-08
  • 純C++實(shí)現(xiàn)PP-OCRv5文字識別的全流程

    純C++實(shí)現(xiàn)PP-OCRv5文字識別的全流程

    你是不是也遇到過這種情況:想在C++項(xiàng)目里加個(gè)OCR功能,結(jié)果光裝OpenCV就折騰半天?今天教你零OpenCV依賴,用Paddle Inference + stb_image,純C++實(shí)現(xiàn)PP-OCRv5文字識別全流程(檢測+識別),代碼可直接跑,需要的朋友可以參考下
    2026-02-02
  • c++使用regex報(bào)錯(cuò)regex_error兩種解決方案

    c++使用regex報(bào)錯(cuò)regex_error兩種解決方案

    C++正則表達(dá)式是一個(gè)非常強(qiáng)大和實(shí)用的工具,但是使用它們時(shí)需要注意仔細(xì)檢查代碼是否符合語法規(guī)則,這篇文章主要給大家介紹了關(guān)于c++使用regex報(bào)錯(cuò)regex_error的兩種解決方案,需要的朋友可以參考下
    2024-03-03

最新評論

昭觉县| 吕梁市| 伊金霍洛旗| 岐山县| 青海省| 龙南县| 偏关县| 剑川县| 乐安县| 库车县| 五常市| 鹰潭市| 清远市| 西畴县| 巴林左旗| 湘乡市| 柞水县| 合川市| 黄大仙区| 海兴县| 松滋市| 金昌市| 兴仁县| 永登县| 遂川县| 天全县| 南京市| 永清县| 耒阳市| 姜堰市| 大姚县| 武强县| 武穴市| 娄底市| 阿克陶县| 贡觉县| 包头市| 平原县| 敖汉旗| 万安县| 兴业县|