C++ 自定义时间日期结构到 utc 纪元

标签 c++ c++11 timestamp c++-chrono time-t

我使用的库使用以下结构来定义开始时间戳,如下所示。

    struct SYSTEMTIME {
    /** year */
    WORD year;

    /** month */
    WORD month;

    /** day of week */
    WORD dayOfWeek;

    /** day */
    WORD day;

    /** hour */
    WORD hour;

    /** minute */
    WORD minute;

    /** second */
    WORD second;

    /** milliseconds */
    WORD milliseconds;
};

对于此时间之后的每个日志条目,以纳秒为单位指定与第一个时间戳的差异。

假设是 UTC 2017-12-19 14:44:00 此后的第一个日志条目是 397000ns。

如何从第一个 SYSTEMTIME 结构中创建 chronos、time_t 或 unix 时间,然后向其中添加纳秒。

打印输出应该是第一个条目 2017-12-19 14:44:00.000397

致以诚挚的问候

最佳答案

已更新

我稍微修改了下面的代码以在 SYSTEMTIME 之间进行转换和date::sys_time<std::chrono::milliseconds> ,而不是 date::sys_time<std::chrono::nanoseconds> .

理由:这样 to_SYSTEMTIME 中就没有隐式精度损失。 。 to_SYSTEMTIME的客户可以按照他们想要的任何方式显式截断精度( floorroundceil 等)。并且未能截断精度(如果需要)不会是无提示的运行时错误。

客户端代码(在 main 中)不受此更改的影响。


您可以使用Howard Hinnant's free, open-source, header-only date/time library为此:

#include "date/date.h"
#include <iostream>

using WORD = int;

struct SYSTEMTIME {
    /** year */
    WORD year;

    /** month */
    WORD month;

    /** day of week */
    WORD dayOfWeek;

    /** day */
    WORD day;

    /** hour */
    WORD hour;

    /** minute */
    WORD minute;

    /** second */
    WORD second;

    /** milliseconds */
    WORD milliseconds;
};

date::sys_time<std::chrono::milliseconds>
to_sys_time(SYSTEMTIME const& t)
{
    using namespace std::chrono;
    using namespace date;
    return sys_days{year{t.year}/t.month/t.day} + hours{t.hour} +
           minutes{t.minute} + seconds{t.second} + milliseconds{t.milliseconds};
}

int
main()
{
    using namespace std::chrono;
    using namespace date;
    SYSTEMTIME st{2017, 12, 2, 19, 14, 44, 0, 0};
    auto t = to_sys_time(st) + 397000ns;
    std::cout << floor<microseconds>(t) << '\n';
}

输出:

2017-12-19 14:44:00.000397

这将转换 SYSTEMTIMEstd::chrono::time_point<system_clock, milliseconds> (其类型别名名为 date::sys_time<milliseconds> )通过收集 SYSTEMTIME 中的不同部分。然后它简单地添加 nanoseconds到那个time_point ,将其截断为所需精度 microseconds ,并将其流式传输出去。

如果有帮助的话,以下是如何使用相同的库进行相反的转换:

SYSTEMTIME
to_SYSTEMTIME(date::sys_time<std::chrono::milliseconds> const& t)
{
    using namespace std::chrono;
    using namespace date;
    auto sd = floor<days>(t);
    year_month_day ymd = sd;
    auto tod = make_time(t - sd);
    SYSTEMTIME x;
    x.year = int{ymd.year()};
    x.month = unsigned{ymd.month()};
    x.dayOfWeek = weekday{sd}.c_encoding();
    x.day = unsigned{ymd.day()};
    x.hour = tod.hours().count();
    x.minute = tod.minutes().count();
    x.second = tod.seconds().count();
    x.milliseconds = tod.subseconds().count();
    return x;
}

关于C++ 自定义时间日期结构到 utc 纪元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47888459/

相关文章:

c++ - 一种将 std::string 复制到 vector<char> 中的更优雅的方法

c++ - 构造函数符号的双重发射

c++ - 什么是右值、左值、x值、glvalues和prvalues?

c++ - C++11 中的延迟初始化顺序

php - 从日期减去一定数量的小时、天、月或年

c# - XML根据时间戳删除节点C#

c++ - 我需要帮助实现我的事件管理器和引擎系统 (SDL && C++)

c++ - 将 char[] 附加到具有特定长度的 std::string

c++ - 无法从函数参数的默认参数中推断出模板参数

MYSQL:使用字母数字 Id 在特定值之后选择接下来的 100 行