Namespaces
Variants

std::chrono:: operator+, std::chrono:: operator- (std::chrono::year_month_day_last)

From cppreference.net
定义于头文件 <chrono>
(C++20 起)
(C++20 起)
(C++20 起)
(C++20 起)
(C++20 起)
(C++20 起)
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