gpt4 book ai didi

c++ - 将 UNIX 纪元时间转换为 SYSTEMTIME

转载 作者:行者123 更新时间:2023-11-28 02:20:45 30 4
gpt4 key购买 nike

在我的 C++ Windows 应用程序中,我将 SYSTEMTIME 转换为格式化字符串,如下所示:

    SYSTEMTIME systemTime;
GetSystemTime(&systemTime);
char pSystemTime[50];
sprintf_s(pSystemTime, 50, "%04d%02d%02d%02d%02d%02d%03u\0", systemTime.wYear, systemTime.wMonth,
systemTime.wDay, systemTime.wHour,
systemTime.wMinute, systemTime.wSecond,
systemTime.wMilliseconds);

现在我有一个自 UNIX 纪元以来的毫秒数。

long time = 1442524186;  //for example

如何将这么长的纪元时间转换为 SYSTEMTIME,以便我也可以将其格式化为字符串?

最佳答案

static UINT64 FileTimeToMillis(const FILETIME &ft)
{
ULARGE_INTEGER uli;
uli.LowPart = ft.dwLowDateTime; // could use memcpy here!
uli.HighPart = ft.dwHighDateTime;

return static_cast<UINT64>(uli.QuadPart/10000);
}

static void MillisToSystemTime(UINT64 millis, SYSTEMTIME *st)
{
UINT64 multiplier = 10000;
UINT64 t = multiplier * millis;

ULARGE_INTEGER li;
li.QuadPart = t;
// NOTE, DON'T have to do this any longer because we're putting
// in the 64bit UINT directly
//li.LowPart = static_cast<DWORD>(t & 0xFFFFFFFF);
//li.HighPart = static_cast<DWORD>(t >> 32);

FILETIME ft;
ft.dwLowDateTime = li.LowPart;
ft.dwHighDateTime = li.HighPart;

::FileTimeToSystemTime(&ft, st);
}

来源:https://stackoverflow.com/a/11123106/2385309

编辑:另见:https://stackoverflow.com/a/26486180/2385309您可能需要为 Epoch 时间毫秒添加/减去 11644473600000

关于c++ - 将 UNIX 纪元时间转换为 SYSTEMTIME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32636309/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com