std::chrono...rono::month)(3)

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

okay so bestie, this page is from the C++ Standard Libary β€” the title is std::chrono...rono::month)(3) (the ellipsis is literally the main character) β€” and it’s all about doing math with std::chrono::month values. πŸ—“οΈβœ¨ No cap, if you’ve ever wanted to add months to a month or subtract one month from another, this is your moment. πŸ’…

πŸ’– NAME πŸ’–

It’s std::chrono::operator+ and std::chrono::operator- for std::chrono::month β€” like, the plus and minus operators specifically for month objects. The full name is std::chrono::operator+,std::chrono::operator-(std::chrono::month) and then it says std::chrono::operator+,std::chrono::operator-(std::chrono::month) again. Yes, she repeats herself, she’s extra. 🫢

✨ Synopsis ✨

Defined in header <chrono>. Here are the overloads, all constexpr and all noexcept:

(1) (since C++20)

constexpr std::chrono::month operator+( const std::chrono::month& m,
                                        const std::chrono::months& ms ) noexcept;

(2) (since C++20)

constexpr std::chrono::month operator+( const std::chrono::months& ms,
                                        const std::chrono::month& m ) noexcept;

(3) (since C++20)

constexpr std::chrono::month operator-( const std::chrono::month& m,
                                        const std::chrono::months& ms ) noexcept;

(4) (since C++20)

constexpr std::chrono::months operator-( const std::chrono::month& m1,
                                         const std::chrono::month& m2 ) noexcept;

So what are they actually doing? 🧚

  • 1,2) β€” Adds ms.count() months to m. The month value held in the result is computed by first evaluating static_cast<long long>(unsigned(m)) + (ms.count() - 1), reducing it modulo 12 to an integer in the range [0, 11], and then adding 1.
  • 3) β€” Subtracts ms.count() months from m and returns the result. Equivalent to return m + -ms;.
  • 4) β€” If m1.ok() and m2.ok() are both true, returns a std::chrono::months value m such that m.count() is in the range [0, 11] and m2 + m == m1. Otherwise the returned value is unspecified.

πŸ’« Return value πŸ’«

  • 1-3) β€” A std::chrono::month holding a month value calculated as described above.
  • 4) β€” A std::chrono::months representing the distance between m1 and m2.

🧠 Notes 🧠

As long as the computation doesn’t overflow, (1-3) always return a valid month even if m.ok() is false. 🫢

The result of subtracting two month values is a duration of type std::chrono::months. That duration unit represents the length of the average Gregorian month, and the resulting duration bears no relationship to the number of days in the particular months represented by the operands. For example, std::chrono::seconds(std::chrono::April - std::chrono::March) is not the number of seconds in March (2678400s), but 2629746s (30.436875 days). 🀯

πŸŽ€ Example πŸŽ€

The page literally says // Run this code β€” so go off, bestie:

#include <cassert>
#include <chrono>

int main()
{
    std::chrono::month m{6};

    m = m + std::chrono::months(2);
    assert(m == std::chrono::month(8));

    m = m - std::chrono::months(3);
    assert(m == std::chrono::month(5));

    constexpr std::chrono::months ms = std::chrono::month(8) -
        std::chrono::month(6);
    static_assert(ms == std::chrono::months(2));
}

πŸ‘€ See also πŸ‘€

  • operator++
  • operator++(int) β€” increments or decrements the month
  • operator-- β€” (public member function)
  • operator--(int)
  • operator+= β€” adds or subtracts a number of months
  • operator-= β€” (public member function)

okay that’s the whole page, bestie. source/version: http://cppreference.com, date: 2024.06.10, and the page title is std::chrono...rono::month)(3). go forth and do month math responsibly. πŸ’–βœ¨

πŸ“¦ where this page comes from