c++ - 如何将 chrono::year_month_day 添加到 chrono::sys_seconds
问题描述
我持有年月日的持续时间。有没有一种简单的方法可以将 year_month_day 持续时间添加到像 sys_seconds 这样的 time_point?
sys_seconds date1 = {};
sys_seconds dat2 = {};
year_month_day calDuration;
date1 = date2 + calDuration; //error: no match for ‘operator+’
解决方案
This is chrono catching logic bugs for you at compile-time. Adding date2 + calDuration
is akin to adding tomorrow + today. It just doesn't make sense. And that's why it is a compile-time error.
What you may mean is that you have durations years
, months
and days
. This is not the same as the similarly named types year
, month
and day
. The plural forms are chrono::duration
s, just like minutes
and nanoseconds
. days
is 24h
. And years
and months
are the average length of those units in the civil calendar.
Conversely, the singular forms year
, month
and day
are the calendrical components that give a name to a day in the civil calendar, e.g. 2020y
, December
, and 18d
. And it is these singular forms that make up a year_month_day
, e.g. 2020y/December/18d
.
See this SO answer for a deep-dive on the difference between month
and months
.
There are multiple ways to add the units years
and months
to a time_point
. See this SO answer for a deep-dive on that topic.
推荐阅读
- javascript - 通过 ES6 导入在浏览器中使用 socket.io
- java - 使用 NavigationView 时出错/应用程序崩溃
- postgresql - TypeOrm Postgres 对 where 子句值区分大小写
- github - Github Actions - 任何运行步骤都没有这样的文件或目录
- java - Thymeleaf 返回 Java 异常消息而不是我的自定义错误消息。我怎样才能让它返回我想要的?
- linux - 为什么我的“回声”只显示 1 个字?(Linux-Bash)
- c - 如何将我的 printf 语句移动到我的主驱动程序?
- react-native - 反应本机视频播放器
- time - 同时输入php时间减法时得到错误的值
- android - TCP 客户端套接字未监听接收器线程