std::chrono...year_month)(3)
π NAME π
bestie, welcome to the C++ Standard Libary manual page for std::chrono...year_month)(3) π«Ά it's std::chrono::operator+,std::chrono::operator-(std::chrono::year_month) β literally the std::chrono::operator+ and std::chrono::operator- overloads for std::chrono::year_month, and yes the vibes are immaculate.
β¨ Synopsis β¨
okay so here's the whole overload lineup, all constexpr, all noexcept, all (since C++20) π
(1) (since C++20)
constexpr std::chrono::year_month operator+( const std::chrono::year_month& ym,
const std::chrono::years& dy ) noexcept;
(2) (since C++20)
constexpr std::chrono::year_month operator+( const std::chrono::years& dy,
const std::chrono::year_month& ym ) noexcept;
(3) (since C++20)
constexpr std::chrono::year_month operator+( const std::chrono::year_month& ym,
const std::chrono::months& dm ) noexcept;
(4) (since C++20)
constexpr std::chrono::year_month operator+( const std::chrono::months& dm,
const std::chrono::year_month& ym ) noexcept;
(5) (since C++20)
constexpr std::chrono::year_month operator-( const std::chrono::year_month& ym,
const std::chrono::years& dy ) noexcept;
(6) (since C++20)
constexpr std::chrono::year_month operator-( const std::chrono::year_month& ym,
const std::chrono::months& dm ) noexcept;
(7) (since C++20)
constexpr std::chrono::months operator-( const std::chrono::year_month& ym1,
const std::chrono::year_month& ym2 ) noexcept;
Here's what they actually do, no cap:
1,2)β Addsdy.count()years toym.3,4)β Addsdm.count()months toym.5)β Subtractsdy.count()years fromym.6)β Subtractsdm.count()months fromym.7)β Returns the difference in months between the two time points represented byym1andym2.
For durations that are convertible to both std::chrono::years and std::chrono::months, the years overloads (1,2,5) are preferred if the call would otherwise be ambiguous.
π₯ Return value π₯
1,2)βstd::chrono::year_month(ym.year() + dy, ym.month())3,4)β Ayear_monthvaluezsuch thatz - ym == dmandz.ok() == true.5)βym + -dy6)βym + -dm7)β
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.
lowkey important: that std::chrono::months math is average-month math, not "count the actual days on the calendar" math. π
π Example π
// Run this code
#include <cassert>
#include <chrono>
int main()
{
auto ym{std::chrono::year(2021)/std::chrono::July};
ym = ym + std::chrono::months(14);
assert(ym.month() == std::chrono::September);
assert(ym.year() == std::chrono::year(2022));
ym = ym - std::chrono::years(3);
assert(ym.month() == std::chrono::month(9));
assert(ym.year() == std::chrono::year(2019));
ym = ym + (std::chrono::September - std::chrono::month(2));
assert(ym.month() == std::chrono::April);
assert(ym.year() == std::chrono::year(2020));
}
π¦ See also π¦
operator+=modifies theyear_monthby some number of months or yearsoperator-=(public member function)
π that's the whole page, bestie! source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::chrono...year_month)(3) β¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms