为什么周一周一上午3点更新日历周的数字,但不是在午夜

Why calendar week number updates on Monday at 3 a.m., but not at midnight?

本文关键字:数字 午夜 更新 一周 周一周 一上午 为什么 3点 日历      更新时间:2023-10-16

我正在使用howardhinnant.github.io/iso_week.html的 iso_week.h来计算给定日期的周数。但是,看起来它更新了周一凌晨3点,而不是午夜。

示例代码:

#include <iostream>
#include "iso_week.h"
int main() {
    using namespace iso_week;
    using namespace std::chrono;
    /* Assume we have two time points:
     * tp1 corresponds: Monday July 15 02:50:00 2019
     * tp2 corresponds: Monday July 15 03:00:00 2019
     */
    // Floor time points to convert to the sys_days:
    auto tp1_sys = floor<days>(tp1);
    auto tp2_sys = floor<days>(tp2);
    // Convert from sys_days to iso_week::year_weeknum_weekday format
    auto yww1 = year_weeknum_weekday{tp1_sys};
    auto yww2 = year_weeknum_weekday{tp2_sys};
    // Print corresponding week number of the year
    std::cout << "Week of yww1 is: " << yww1.weeknum() << std::endl;
    std::cout << "Week of yww2 is: " << yww2.weeknum() << std::endl;
}

输出为:

Week of yww1 is: W28
Week of yww2 is: W29

为什么要这样做?

起初我没有注意到您的评论:

// Floor time points to convert to the sys_days:

这意味着tp1tp2基于system_clock。和system_clock型号UTC。

您可以使用TZ.H标头(TZ.CPP源(获取当前时区,将UTC时间点转换为本地时间点,然后将其馈送到year_weeknum_weekday。这将定义一天的开始是您当地的午夜而不是午夜UTC。

这看起来像:

#include "date/iso_week.h"
#include "date/tz.h"
#include <iostream>
int
main()
{
    using namespace iso_week;
    using namespace date;
    using namespace std::chrono;
    auto tp1 = floor<seconds>(system_clock::now());
    zoned_seconds zt{current_zone(), tp1};
    auto tp1_local = floor<days>(zt.get_local_time());
    auto yww1 = year_weeknum_weekday{tp1_local};
    std::cout << "Week of yww1 is: " << yww1.weeknum() << std::endl;
}

current_zone()查询您的计算机目前的本地时区设置。如果您喜欢其他时区,则可以用时区名称替换current_zone()

zoned_seconds zt{"Europe/Athens", tp1};

如果您想以比秒的精确工作更精确地工作,则zoned_seconds只是zoned_time<seconds>的类型别名。因此,请使用您需要的任何精确度(例如zoned_time<milliseconds>(。

使用TZ.H的使用确实需要安装。它不仅是标题。

这与您的时区有关系吗?我知道很多企业都位于东海岸," iso_week.h"可能基于那个时候,这意味着它可能在午夜运行,它只是告诉您它在凌晨3点运行。如果不是这种情况,只在晚上9点运行该程序是错误的?