c++ - chrono::year 对象是如何构造的?
问题描述
我刚刚注意到c++20将具有chrono::year
. 它的构造函数int
在以下范围内:[-32767, 32767],但是我不清楚这个参数代表什么。
- 这与
tm_year
1900 年的起源一致吗? - 或者也许
time_t
是 1970 年的起源? - 或者它可能是在Anno Domini中,起源为 0?
编辑:
这是理解is_leap
功能chrono::year
提供的含义的关键。没有来源,不清楚这里代表的是哪一年。
解决方案
In 25.8.1 [time.cal.general]:
The types in 25.8 describe the civil (Gregorian) calendar and its relationship to
sys_days
andlocal_days
.
The wording on this was (is) challenging as the intent is to model the Gregorian calendar (as does C++ currently via the C API) without offending those who follow other calendars.
I also am just now noting that the word "proleptic" is missing from the spec, and should probably be added in a strategic spot.
To directly answer the question, the integral associated with std::chrono::year
is the Anno Domini reference, as defined by Pope Gregory in 1582, but running both backwards and forwards in time. As I write this, the year is 2018y
.
And (answering Jonathan Mee's comment below), this program:
#include <chrono>
#include <iostream>
int
main()
{
using namespace std;
using namespace std::chrono;
const auto foo = 2018y;
cout << int{foo} << '\n';
}
Outputs:
2018
Live demo that you can experiment with with the proviso that the "date.h" example implementation puts things in namespace date
instead of namespace std::chrono
.
I should also note that this software allows for user-written calendars to interoperate with the std::chrono
system. Here is an example of the Julian calendar. There are a couple more examples here.
Finally, a brief note on the rationale as to why the current year is represented as year{2018}
(Anno Domini), as opposed to year{48}
(time_t
's 1970 origin), or year{118}
(tm_year
's 1900 origin):
This philosophy is hysterical when used in movies. But gets tiresome when used in software design. This library tries to do the expected.
推荐阅读
- java - 如何找出 System.out.print() 的类型
- scala - Gatling 如何创建列表并将其传递给会话?
- ffmpeg - 是否可以在 SDP 上调整 ffmpeg 的超时?
- python-3.x - 我怎样才能将所有这些听写合二为一?
- ios - SceneKit 着色器修改器未修改几何体的位置
- python - 从大型 dfs2 文件中提取 dfs0
- linux - 如何修复无法为虚拟机 kali-linux-2020.3-vbox-amd34 打开会话
- java - 错误 | 发生异常:org.apache.wicket.core.request.handler.ListenerInvocationNotAllowedException:行为拒绝接口调用
- cdn - 我无法导入 js 库脚本 p5.js CDN 错误
- google-chrome - NET::ERR_CERT_VALIDITY_TOO_LONG - 服务器证书的有效期过长