std::chrono..._month_day)(3)
π std::chrono::operator+, std::chrono::operator-(std::chrono::year_month_day) π
okay so bestie, this is a page from the C++ Standard Library manual, and it's literally all about adding and subtracting months and years from a std::chrono::year_month_day date. yes, actual calendar math, and it slays β¨
β¨ NAME β¨
std::chrono::operator+, std::chrono::operator-(std::chrono::year_month_day) β the plus and minus operators that do date arithmetic on a std::chrono::year_month_day. that's the whole vibe π
πΈ SYNOPSIS πΈ
so this whole thing lives in the header <chrono>, no cap:
#include <chrono>
here's every single overload, and every one of them is noexcept (she's chill, she doesn't throw π
). each one is marked (since C++20) β that's the era she was born into, iykyk:
1) (since C++20)
constexpr std::chrono::year_month_day operator+( const std::chrono::year_month_day& ymd,
const std::chrono::months& dm ) noexcept;
2) (since C++20)
constexpr std::chrono::year_month_day operator+( const std::chrono::months& dm,
const std::chrono::year_month_day& ymd ) noexcept;
3) (since C++20)
constexpr std::chrono::year_month_day operator+( const std::chrono::year_month_day& ymd,
const std::chrono::years& dy ) noexcept;
4) (since C++20)
constexpr std::chrono::year_month_day operator+( const std::chrono::years& dy,
const std::chrono::year_month_day& ymd ) noexcept;
5) (since C++20)
constexpr std::chrono::year_month_day operator-( const std::chrono::year_month_day& ymd,
const std::chrono::months& dm ) noexcept;
6) (since C++20)
constexpr std::chrono::year_month_day operator-( const std::chrono::year_month_day& ymd,
const std::chrono::years& dy ) noexcept;
π¦ DESCRIPTION π¦
let me walk you through it, one overload at a time π«Ά
- 1, 2) these add
dm.count()months to the date thatymdrepresents. the result has the sameday()asymd, and the sameyear()andmonth()asstd::chrono::year_month(ymd.year(), ymd.month()) + dm. so the day-of-month is just... carried along for the ride, untouched π - 3, 4) these add
dy.count()years to the date thatymdrepresents. the result is equivalent tostd::chrono::year_month_day(ymd.year() + dy, ymd.month(), ymd.day(). - 5) this one subtracts
dm.count()months from the dateymdrepresents. equivalent toymd + -dm. sneaky but iconic. - 6) and this subtracts
dy.count()years from the dateymdrepresents. equivalent toymd + -dy.
β οΈ important caveat bestie, don't skip this: for durations that are convertible to bothstd::chrono::yearsandstd::chrono::months, theyearsoverloads (3, 4, 6) are preferred if the call would otherwise be ambiguous. so the years one wins the tie, periodt π
π NOTES π
listen. even if ymd.ok() is true, the resulting year_month_day may not represent a valid date if ymd.day() is 29, 30, or 31. like, the arithmetic is valid, the calendar is not. no cap, this is the trap π
π§ EXAMPLE π§
okay run this code, it's cute:
#include <chrono>
#include <iostream>
int main()
{
std::cout << std::boolalpha;
auto ymd{std::chrono::day(1)/std::chrono::July/2021};
ymd = ymd + std::chrono::months(4);
std::cout << (ymd.month() == std::chrono::November) << ' '
<< (ymd.year() == std::chrono::year(2021)) << ' ';
ymd = ymd - std::chrono::years(10);
std::cout << (ymd.month() == std::chrono::month(11)) << ' '
<< (ymd.year() == std::chrono::year(2011)) << '\n';
}
β Output β
true true true true
she literally said true four times in a row and honestly? same energy πβ¨
π SEE ALSO π
nope, nothing here β this page doesn't reference any other pages. move along bestie π¦
so that's the whole page! this manual page came to you courtesy of cppreference.com, dated 2024.06.10, and it lives under the manual name C++ Standard Library β section 3, if we're being fancy π
mwah, go write some constexpr calendar math, ily π«Άπ
π¦ 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