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

标签 c++

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

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 1970
The current local time is: Sat Jan 17 10:11:02 1970
1970-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/

相关文章:

c++ - 构造函数的多重定义

C++ FSM 设计和所有权

C++ 将同类包装类型的元组转换为原始类型的元组

python - 在 python 中加载自定义 dll 时出现依赖性错误,使用 Visual Studio 2017 和 boost 构建

c++ - 用于处理带有数字输出的 C++ 单元测试的框架/工具

c++ - 将二维矩阵解析为链表

c# - Mapnik.NET 图层数据源路径

c++ - 在 std::u8string 和 std::string 之间转换

c++ - 具有余弦距离的 mlpack 最近邻?

python - 从 C++ 运行 python 脚本时内存泄漏