【问题标题】:c++ adding years and days using date.hc ++使用date.h添加年份和日期
【发布时间】:2023-01-16 22:06:31
【问题描述】:

使用 date.h 和 std::chrono 处理日历持续时间算术,但得到意外结果。

示例代码是:

#include "date.h"

#include <string>
#include <chrono>
#include <iostream>

int main() {
    date::sys_seconds calendarDate = {};
    calendarDate = std::chrono::years(30) + date::sys_seconds(std::chrono::days(10));
    std::string stringDate = date::format("%Y-%m-%d %H:%M:%S", calendarDate);
    std::cout << "{} + 30 years + 10 days = " << stringDate << "\n";
    return 0;
}

实际输出: {} + 30 年 + 10 天 = 2000-01-11 06:36:00

预期输出: {} + 30 年 + 10 天 = 2000-01-11 00:00:00

使用 Ubuntu 22.04; g++ 11.3.0

编译:gcc -g -std=c++20 main.cpp -lstdc++

在此处使用 date.h fromm:https://raw.githubusercontent.com/HowardHinnant/date/master/include/date/date.h

是否了解在额外的 6 小时 36 分钟中增加了什么?

【问题讨论】:

  • 只是猜测它使用的是平均年份。

标签: c++ c++-chrono


【解决方案1】:

Linux 将日期存储为 UTC。我的猜测是您的机器位于中央时区 (CT),距 UTC 时间 +6 小时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多