Namespaces
Variants

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

From cppreference.net
constexpr std:: chrono :: year operator + ( const std:: chrono :: year & y,
const std:: chrono :: years & ys ) noexcept ;
(1) (自 C++20 起)
constexpr std:: chrono :: year operator + ( const std:: chrono :: years & ys,
const std:: chrono :: year & y ) noexcept ;
(2) (自 C++20 起)
constexpr std:: chrono :: year operator - ( const std:: chrono :: year & y,
const std:: chrono :: years & ys ) noexcept ;
(3) (自 C++20 起)
constexpr std:: chrono :: years operator - ( const std:: chrono :: year & y1,
const std:: chrono :: year & y2 ) noexcept ;
(4) (自 C++20 起)
1,2) ys. count ( ) 年数累加到 y
3) y 中减去 ys. count ( ) 年。
4) 返回 y1 y2 之间的年份差。

目录

返回值

1,2) std:: chrono :: year ( int ( y ) + ys. count ( ) )
3) std:: chrono :: year ( int ( y ) - ys. count ( ) )
4) std:: chrono :: years ( int ( y1 ) - int ( y2 ) )

注释

如果 (1-3) 的年份结果值超出范围 [ - 32767 , 32767 ] ,实际存储的值是未指定的。

两个 year 值相减的结果是类型为 std::chrono::years 的时间长度。该时长单位表示格里历年的平均长度,所得结果时长与操作数所代表的具体年份中的天数无关。例如, 2018y - 2017y 的结果是 std:: chrono :: years ( 1 ) ,表示365.2425天而非365天。

示例

#include <cassert>
#include <chrono>
int main()
{
    std::chrono::year y{2020};
    y = std::chrono::years(12) + y; // 重载 (2):时长 + 时间点
    assert(y == std::chrono::year(2032));
    y = y - std::chrono::years(33); // 重载 (3):时间点 - 时长
    assert(y == std::chrono::year(1999));
    // y = std::chrono::years(33) - y; // 不支持:时长 - 时间点
    using namespace std::chrono;
    constexpr std::chrono::years ys = 2025y - 2020y; // 重载 (4)
    static_assert(ys == std::chrono::years(5));
}

参见

递增或递减月份
( std::chrono::month 的公开成员函数)
增加或减去指定月数
( std::chrono::month 的公开成员函数)