OpenTime做最稱心的C++開發(fā)時間庫
跨平臺多線程設(shè)計!
程序開發(fā)頻繁涉及時間處理,有一個好用的時間庫可以大幅提高工作效率。
OpenTime是最簡單易用的C++處理時間工具。
OpenLinyou項目設(shè)計跨平臺服務器框架,在VS或者XCode上寫代碼,無需任何改動就可以編譯運行在Linux上,甚至是安卓和iOS.
OpenLinyou:https://github.com/openlinyou
跨平臺支持
Windows、linux、Mac、iOS、Android等跨平臺設(shè)計
編譯和執(zhí)行
請安裝cmake工具,用cmake可以構(gòu)建出VS或者XCode工程,就可以在vs或者xcode上編譯運行。
源代碼:https://github.com/openlinyou/opentime
git clone https://github.com/openlinyou/opentime
cd ./opentime
mkdir build
cd build
cmake ..
#如果是win32,在該目錄出現(xiàn)opentime.sln,點擊它就可以啟動vs寫代碼調(diào)試
make
./test
全部源文件
- src/opentime.h
- src/opentime.cpp
1.時間戳轉(zhuǎn)換成字符串
#include <assert.h>
#include <iostream>
#include "opentime.h"
int main()
{
OpenTime openTime(OpenTime::Unixtime());
std::cout << "Current Unixtime:" << openTime.unixtime() << std::endl;
std::cout << "Current Date:" << openTime.toString() << std::endl;
openTime = 1676704738;
assert(openTime.unixtime() == 1676704738);
assert(openTime.wday() == 6);
assert(openTime.toString() == "2023-02-18 15:18:58");
assert(openTime.toGMT() == "Mon, 18 Feb 2023 15:18:58 GMT");
//2023-02-18 13:37:55
openTime = 1676698675;
assert(openTime.toString("") == "2023-02-18 13:37:55");
assert(openTime.toString("_") == "2023_02_18_13_37_55");
//1991-03-18 13:37:55
openTime = 669274675;
assert(openTime.toString("%Y_%M_%D") == "1991_03_18");
//2010-12-18 03:37:55
openTime = 1292614675;
assert(openTime.toString("%Y_%M_%D_%h_%m_%s") == "2010_12_18_03_37_55");
//1972-06-18 13:37:55
openTime = 77693875;
assert(openTime.toString("date:%h:%m:%s %Y/%M/%D%d") == "date:13:37:55 1972/06/18%d");
return 0;
}
2.字符串轉(zhuǎn)換成時間戳
#include <assert.h>
#include <iostream>
#include "opentime.h"
int main()
{
OpenTime openTime;
openTime = "2023-02-18 15:18:58";
int64_t thatTime = 1676704738;
assert(openTime.unixtime() == thatTime);
openTime += 3600 * 24 * 2;
assert(openTime.toString() == "2023-02-20 15:18:58");
openTime.fromGMT("Sat, 18 Feb 2023 15:18:58 GMT");
assert(openTime.unixtime() == thatTime);
//2023-02-18 13:37:55
std::string strTime = "2023-02-18 13:37:55";
openTime = strTime;
assert(openTime.unixtime() == 1676698675);
//1991-03-18 00:00:00
strTime = "1991_03_18";
openTime.fromString(strTime, "%Y_%M_%D");
assert(openTime.unixtime() == 669225600);
//2010-12-18 03:37:55
strTime = "2010_12_18_03_37_55";
openTime.fromString(strTime, "%Y_%M_%D_%h_%m_%s");
assert(openTime.unixtime() == 1292614675);
//1972-06-18 13:37:55
strTime = "date:13:37:55 1972/06/18%d";
openTime.fromString(strTime, "date:%h:%m:%s %Y/%M/%D%d");
assert(openTime.unixtime() == 77693875);
int64_t milliTimeStamp = OpenTime::MilliUnixtime();
std::cout << "Current milliTimeStamp:" << OpenTime(milliTimeStamp / 1000).toString(milliTimeStamp % 1000) << std::endl;
return 0;
}
3.GMT時間與時間戳轉(zhuǎn)換
#include <assert.h>
#include <iostream>
#include "opentime.h"
int main()
{
OpenTime openTime;
openTime = "2023-02-18 15:18:58";
openTime += 3600 * 24 * 2;
assert(openTime.toString() == "2023-02-20 15:18:58");
int64_t thatTime = openTime.unixtime();
assert(openTime.toGMT() == "Mon, 20 Feb 2023 15:18:58 GMT");
openTime.fromGMT("Mon, 20 Feb 2023 15:18:58 GMT");
assert(openTime.unixtime() == thatTime);
// sleep 1 second
OpenTime::Sleep(1000);
return 0;
}
4.整數(shù)時間與時間戳轉(zhuǎn)換
#include <assert.h>
#include <iostream>
#include "opentime.h"
int main()
{
OpenTime openTime;
int64_t inttime = 20230218133755;
openTime.fromIntTime(inttime);
assert(openTime.toString() == "2023-02-18 13:37:55");
assert(openTime.toIntTime() == 20230218133755);
return 0;
}
5.時間戳按日對齊
#include <assert.h>
#include "opentime.h"
int main()
{
OpenTime openTime;
//2023-02-18 00:00:00
openTime = "2023-02-18 15:18:58";
int64_t alignTime = openTime.alignDay();
assert(OpenTime::ToString(alignTime) == "2023-02-18 00:00:00");
alignTime = OpenTime::AlignDay(1642490337);
assert(OpenTime::ToString(alignTime) == "2022-01-18 00:00:00");
return 0;
}