std::chrono...no::weekday)(3)

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

πŸŽ€ NAME πŸŽ€

This is the page for std::chrono::operator+,std::chrono::operator-(std::chrono::weekday) β€” literally both the plus and minus operators for std::chrono::weekday, bestie. It's from the C++ Standard Libary manual (yes, spelled like that in the header 😭), and it's giving chrono main character energy fr fr.

✨ Synopsis ✨

Defined in header <chrono>.

(1) (since C++20)

constexpr std::chrono::weekday operator+( const std::chrono::weekday& wd,
                                          const std::chrono::days& d ) noexcept;

(2) (since C++20)

constexpr std::chrono::weekday operator+( const std::chrono::days& d,
                                          const std::chrono::weekday& wd ) noexcept;

(3) (since C++20)

constexpr std::chrono::weekday operator-( const std::chrono::weekday& wd,
                                          const std::chrono::days& d ) noexcept;

(4) (since C++20)

constexpr std::chrono::days operator-( const std::chrono::weekday& wd1,
                                       const std::chrono::weekday& wd2 ) noexcept;
  • 1, 2) β€” Adds d.count() days to wd. The weekday value held in the result is computed by first evaluating static_cast<long long>(wd.c_encoding()) + d.count() and reducing it modulo 7 to an integer in the range [0, 6].
  • 3) β€” Subtracts d.count() days from wd. Equivalent to return wd + -d;.
  • 4) β€” If wd1.ok() and wd2.ok() are both true, returns a std::chrono::days value d such that d.count() is in the range [0, 6] and wd2 + d == wd1. Otherwise the returned value is unspecified.

πŸ’– Return value πŸ’–

  • 1-3) A std::chrono::weekday holding a weekday value calculated as described above.
  • 4) A std::chrono::days representing the distance between wd1 and wd2.

🧠 Notes 🧠

As long as the computation doesn't overflow, (1-3) always return a valid weekday even if wd.ok() is false. That's the whole caveat, bestie β€” don't let it overflow and you're slaying πŸ’….

🌸 Example 🌸

// Run this code

#include <chrono>
#include <iostream>

int main()
{
    std::cout << std::boolalpha;

    std::chrono::weekday wd{4};
    wd = wd + std::chrono::days(2);
    std::cout << (wd == std::chrono::weekday(6)) << ' '
              << (wd == std::chrono::Saturday) << ' ';

    wd = wd - std::chrono::days(3);
    std::cout << (wd == std::chrono::weekday(3)) << ' '
              << (wd == std::chrono::Wednesday) << ' ';

    wd = std::chrono::Tuesday;
    wd = wd + std::chrono::days{8}; // (((2 + 8) == 10) % 7) == 3;
    std::cout << (wd == std::chrono::Wednesday) << ' ';

    wd = wd + (std::chrono::Sunday - std::chrono::Thursday); // (3 + 3) == 6
    std::cout << (wd == std::chrono::Saturday) << '\n';
}

πŸ“ Output πŸ“

true true true true true true

🌈 See also 🌈

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

Sign-off πŸ’Œ: source http://cppreference.com, version/date 2024.06.10, footer title std::chrono...no::weekday)(3). That's the footer, bestie β€” mwah πŸ’‹.

πŸ“¦ where this page comes from