c++ 获取时间戳、13位时间戳转为时间格式

1.获取时间戳

   //获取日期
    SYSTEMTIME st;
    CString StrTime;
	GetLocalTime(&st); //获取日期、时间
    StrTime.Format(_T("%.4d-%.2d-%.2d %.2d:%.2d:%.2d.%.3d"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); //2022-07-23 09:49:04.358

2. 13位时间戳转为时间格式

        int64_t TimeUp = 1681436983403;	//13位时间戳
        auto milli = TimeUp + (long long)8 * 60 * 60 * 1000; 
        auto mTime = std::chrono::milliseconds(milli);
        auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(mTime);
        auto tt = std::chrono::system_clock::to_time_t(tp);
        std::tm* now = gmtime(&tt);
        CString ImgTime; 
        ImgTime.Format(L"%.4d-%.2d-%.2d %.2d:%.2d:%.2d.%.3d\n", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec, TimeUp % 1000);
        Printf(ImgTime );

执行结果:

2023-04-14 09:49:43.403

3.获取13位时间戳、减几毫秒,并打印出时间格式

    CString CStr;
    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::milliseconds(150));    
    auto noread_ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()) - ms;
    Printfs("当前时间[%lld]-[%d]=减完后时间[%lld]\r\n", std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()), ms, noread_ms);
    auto mTime = std::chrono::milliseconds(noread_ms);
    auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(mTime);
    auto tt = std::chrono::system_clock::to_time_t(tp);
    std::tm* tn = gmtime(&tt);
    CStr.Format(L"时间:%.4d-%.2d-%.2d %.2d:%.2d:%.2d.%.3d", tn->tm_year + 1900, tn->tm_mon + 1, tn->tm_mday, tn->tm_hour, tn->tm_min, tn->tm_sec, int((noread_ms.count()) % 1000));
    Printf(CStr);

运行结果:
当前时间[1699446071139]-[150]=减完后时间[1699446070989]
时间:2023-11-08 12:21:10.989