为什么周一周一上午3点更新日历周的数字,但不是在午夜
Why calendar week number updates on Monday at 3 a.m., but not at midnight?
我正在使用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:
这意味着tp1
和tp2
基于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点运行该程序是错误的?
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 将数字打印成文字
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 为什么周一周一上午3点更新日历周的数字,但不是在午夜