gpt4 book ai didi

c++ - 如何将无符号值转换为 struct tm?

转载 作者:行者123 更新时间:2023-11-30 01:51:32 25 4
gpt4 key购买 nike

我得到的绝对时间值如下,

1413399540000
1411047780000
1411574340000

如何在 C++ 中将它们转换为“struct tm”?

嗨,约阿希姆,

这是访问列表时间范围的 GUI 输入,我尝试了以下操作,

typedef unsigned long long uint64_t;

uint64_t mytime;
struct tm *tm;
time_t realtime;

mytime = 1447862580005ULL;
realtime = (time_t) (mytime / 1000000);
printf ("The current local time is: %s", ctime(&realtime));
tm = localtime(&realtime);
printf ("The current local time is: %s", asctime(tm));

tm->tm_year +=1900;
tm->tm_mon +=1;

printf("%04d-%02d-%02d %02d:%02d:%02d\n",
tm->tm_year,tm->tm_mon, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);

对/对:

The current local time is: Sat Jan 17 10:11:02 1970The current local time is: Sat Jan 17 10:11:02 19701970-01-17 10:11:02

我看起来很困惑。不确定如何验证转换是否具有正确的值(value)。

最佳答案

1413399540000 看起来像是自 unix 纪元以来的毫秒数,即 15.10.2014 18:59:00

auto millisec = 1413399540000;
time_t time = millisec / 1000;

tm local;
localtime_r(&time, &local); // TODO: Check the errors.

tm utc;
gmtime_r(&time, &utc); // TODO: Check the errors.

关于c++ - 如何将无符号值转换为 struct tm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26017494/

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