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

constexpr std::chrono::year_month operator+(const std::chrono::year_month& ym,
                                            const std::chrono::years& dy) noexcept;
(1) (since C++20)
constexpr std::chrono::year_month operator+(const std::chrono::years& dy,
                                            const std::chrono::year_month& ym) noexcept;
(2) (since C++20)
constexpr std::chrono::year_month operator+(const std::chrono::year_month& ym,
                                            const std::chrono::months& dm) noexcept;
(3) (since C++20)
constexpr std::chrono::year_month operator+(const std::chrono::months& dm,
                                            const std::chrono::year_month& ym) noexcept;
(4) (since C++20)
constexpr std::chrono::year_month operator-(const std::chrono::year_month& ym,
                                            const std::chrono::years& dy) noexcept;
(5) (since C++20)
constexpr std::chrono::year_month operator-(const std::chrono::year_month& ym,
                                            const std::chrono::months& dm) noexcept;
(6) (since C++20)
constexpr std::chrono::months operator-(const std::chrono::year_month& ym1, 
                                        const std::chrono::year_month& ym2) noexcept;
(7) (since C++20)
1-2) Adds dy.count() years to ym.
3-4) Adds dm.count() months to ym.
5) Subtracts dy.count() years from ym.
6) Subtracts dm.count() months from ym.
7) Returns the difference in months between the two time points represented by ym1 and ym2.

Return value

1-2) std::chrono::year_month(ym.year() + dy, ym.month())
3-4) A year_month value z such that z - ym == dm and z.ok() == true.
5) ym + -dy
5) ym + -dm
7) ym1.year() - ym2.year() + std::chrono::months(int(unsigned(ym1.month())) - int(unsigned(ym2.month())))

Notes

The result of subtracting two year_month values is a duration of type std::chrono::months. This duration unit represents the length of the average Gregorian month (30.436875 days), and the resulting duration bears no relationship to the actual number of days in the time period at issue. For example, the result of 2017y/3 - 2017y/2 is std::chrono::months(1), even though February 2017 only contains 28 days.

Durations that are convertible to std::chrono::months, but not std::chrono::years, can be directly added to or subtracted from a year_month. Durations convertible to std::chrono::years cannot because such durations are also convertible to std::chrono::months, resulting in an ambiguity:

using namespace std::chrono;
 
using decades = duration<int, std::ratio_multiply<std::ratio<10>, years::period>>;
using kilomonths = duration<int, std::ratio_multiply<std::kilo, months::period>>;
 
auto ym = 2001y/April;
ym = ym + decades{1}; // error, ambiguous
ym = ym + kilomonths{1}; // OK

Example

See also

modifies the year_month by some number of months or years
(public member function)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/chrono/year_month/operator_arith_2