通过c++11的日期和时间库chrono
来获取当前时间戳,然后做转换。
#include <iostream> #include <chrono> #include <time.h> using namespace std; using namespace std::chrono; int main() { uint64_t timestamp(duration_cast<milliseconds>(chrono::system_clock::now().time_since_epoch()).count()); // 获取时间戳(毫秒) uint64_t milli = timestamp + 8 * 60 * 60 * 1000; // 转为东八区北京时间 auto mTime = milliseconds(milli); auto tp = time_point<system_clock, milliseconds>(mTime); auto tt = system_clock::to_time_t(tp); tm now; gmtime_s(&now, &tt); printf("%4d年%02d月%02d日 %02d:%02d:%02d.%03d\n", now.tm_year + 1900, now.tm_mon + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec, int(timestamp%1000)); cin.get(); return 0; }
Windows平台的方式
简单多了,通过SYSTEMTIME
即可。
#include <iostream> #include <wtypesbase.h> using namespace std; using namespace std::chrono; int main() { SYSTEMTIME sysTime; GetLocalTime(&sysTime); printf("%4d年%02d月%02d日 %02d:%02d:%02d.%03d\n", sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds); cin.get(); return 0; }