std::chrono...no::weekday)(3)
π 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 towd. The weekday value held in the result is computed by first evaluatingstatic_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 fromwd. Equivalent toreturn wd + -d;. - 4) β If
wd1.ok()andwd2.ok()are both true, returns astd::chrono::daysvaluedsuch thatd.count()is in the range[0, 6]andwd2 + d == wd1. Otherwise the returned value is unspecified.
π Return value π
- 1-3) A
std::chrono::weekdayholding a weekday value calculated as described above. - 4) A
std::chrono::daysrepresenting the distance betweenwd1andwd2.
π§ 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
- 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