std::chrono...chrono::day)(3)

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

✨ NAME ✨

Hey bestie πŸ’– welcome to the std::chrono...chrono::day)(3) page from the C++ Standard Libary manual β€” yes, that’s the manual name, spelling preserved, ily. This whole page is about std::chrono::operator+,std::chrono::operator-(std::chrono::day): the add/subtract ops for std::chrono::day. Literally day-math main character energy. πŸ‘‘

🌸 Synopsis 🌸

Defined in header <chrono> ✨

(1) (since C++20)

constexpr std::chrono::day operator+( const std::chrono::day&  d,
                                      const std::chrono::days& ds ) noexcept;

(2) (since C++20)

constexpr std::chrono::day operator+( const std::chrono::days& ds,
                                      const std::chrono::day&  d ) noexcept;

(3) (since C++20)

constexpr std::chrono::day operator-( const std::chrono::day&  d,
                                      const std::chrono::days& ds ) noexcept;

(4) (since C++20)

constexpr std::chrono::days operator-( const std::chrono::day& x,
                                       const std::chrono::day& y ) noexcept;

What they do, bestie:

  • 1,2) β€” Adds ds.count() days to d.
  • 3) β€” Subtracts ds.count() days from d.
  • 4) β€” Calculate the difference, in days, between two day x and y.

πŸ’– Return value πŸ’–

  • 1,2) β€” std::chrono::day(unsigned(d) + ds.count())
  • 3) β€” std::chrono::day(unsigned(d) - ds.count())
  • 4) β€” std::chrono::days(int(unsigned(x)) - int(unsigned(y)))

🧚 Notes 🧚

1-3) If the result would be outside the range [0, 255], the actual stored value is unspecified. ⚠️ Not β€œunspecified but cute.” It’s literally unspecified, so don’t act like it’s guaranteed, fr fr.

πŸ“ Example πŸ“

// Run this code β€” yes, literally the manpage’s little run label. Then:

#include <cassert>
#include <chrono>

int main()
{
    std::chrono::day d{15};

    d = d + std::chrono::days(2);
    assert(d == std::chrono::day(17));

    d = d - std::chrono::days(3);
    assert(d == std::chrono::day(14));

    constexpr std::chrono::days ds = std::chrono::day(16) -
       std::chrono::day(14);
    static_assert(ds == std::chrono::days(2));
}

πŸ”₯ See also πŸ”₯

  • operator++, operator++(int), operator--, operator--(int), operator+=, operator-= β€” these are public member functions; operator++ / operator-- increment or decrement the day, and operator+= / operator-= add or subtract a number of days. The whole member-function squad ate, no cap. πŸ’…

okay that’s the whole page, mwah 🫢🌈 β€” source: http://cppreference.com Β· date: 2024.06.10. Bye bestie, go do iconic calendar math. πŸ‘‘πŸ’œ

πŸ“¦ where this page comes from