std::chrono..._month_day)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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 that ymd represents. the result has the same day() as ymd, and the same year() and month() as std::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 that ymd represents. the result is equivalent to std::chrono::year_month_day(ymd.year() + dy, ymd.month(), ymd.day().
  • 5) this one subtracts dm.count() months from the date ymd represents. equivalent to ymd + -dm. sneaky but iconic.
  • 6) and this subtracts dy.count() years from the date ymd represents. equivalent to ymd + -dy.
⚠️ important caveat bestie, don't skip this: for durations that are convertible to both std::chrono::years and std::chrono::months, the years overloads (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