std::chrono:: operator+, std::chrono:: operator- (std::chrono::year_month_day_last)
From cppreference.net
<
cpp
|
chrono
|
year month day last
1,2)
将
dm.
count
(
)
个月添加到
ymdl
表示的日期。结果具有与
std::
chrono
::
year_month
(
ymdl.
year
(
)
, ymdl.
month
(
)
)
+
dm
相同的
year()
和
month()
。
3,4)
将
dy.
count
(
)
年数添加到
ymdl
表示的日期。该操作等效于
std::
chrono
::
year_month_day_last
(
ymdl.
year
(
)
+
dy, ymdl.
month_day_last
(
)
)
。
5)
从
ymdl
表示的日期中减去
dm.
count
(
)
个月。等价于
ymdl
+
-
dm
。
6)
从
ymdl
表示的日期中减去
dy.
count
(
)
年。等价于
ymdl
+
-
dy
。
对于可同时转换为
std::chrono::years
和
std::chrono::months
的时间段,若调用存在歧义,则优先选择
years
重载
(3,4,6)
。
示例
运行此代码
#include <cassert> #include <chrono> #include <iostream> int main() { auto ymdl{11/std::chrono::last/2020}; std::cout << ymdl << '\n'; ymdl = std::chrono::years(10) + ymdl; std::cout << ymdl << '\n'; assert(ymdl == std::chrono::day(30)/ std::chrono::November/ std::chrono::year(2030)); ymdl = ymdl - std::chrono::months(6); std::cout << ymdl << '\n'; assert(ymdl == std::chrono::day(31)/ std::chrono::May/ std::chrono::year(2030)); }
输出:
2020/Nov/last 2030/Nov/last 2030/May/last