std::chrono...th_weekday)(3)
β¨ NAME β¨
Hey bestie, welcome to the std::chrono...th_weekday)(3) page from the C++ Standard Libary man-page collection π. This one is all about std::chrono::year_month_weekday math, and the name is, and I quote:
std::chrono::operator+,std::chrono::operator-(std::chrono::year_month_weekday)
And yeah, the page echoes the exact same name right after a dash, because man pages love a dramatic repeat π . Itβs giving βtwo operators, one extremely long type name,β and weβre gonna make it make sense.
πΈ Synopsis πΈ
Every overload here has (since C++20) energy β¨. Theyβre all constexpr and noexcept, so the compiler is literally invited to vibe at compile time and promised no exceptions.
(1) (since C++20)
constexpr std::chrono::year_month_weekday
operator+( const std::chrono::year_month_weekday& ymwd,
const std::chrono::months& dm ) noexcept;
(2) (since C++20)
constexpr std::chrono::year_month_weekday
operator+( const std::chrono::months& dm,
const std::chrono::year_month_weekday& ymwd ) noexcept;
(3) (since C++20)
constexpr std::chrono::year_month_weekday
operator+( const std::chrono::year_month_weekday& ymwd,
const std::chrono::years& dy ) noexcept;
(4) (since C++20)
constexpr std::chrono::year_month_weekday
operator+( const std::chrono::years& dy,
const std::chrono::year_month_weekday& ymwd ) noexcept;
(5) (since C++20)
constexpr std::chrono::year_month_weekday
operator-( const std::chrono::year_month_weekday& ymwd,
const std::chrono::months& dm ) noexcept;
(6) (since C++20)
constexpr std::chrono::year_month_weekday
operator-( const std::chrono::year_month_weekday& ymwd,
const std::chrono::years& dy ) noexcept;
Okay so what do they actually do? Hereβs the tea π«:
1,2)β Addsdm.count()months to the date represented byymwd. The result has the sameyear()andmonth()asstd::chrono::year_month(ymwd.year(), ymwd.month()) + dm, and the sameweekday()andindex()asymwd. The weekday vibe stays untouched, periodt.3,4)β Addsdy.count()years to the date represented byymwd. The result is equivalent tostd::chrono::year_month_weekday(ymwd.year() + dy, ymwd.month(), ymwd.weekday_indexed()).5)β Subtractsdm.count()months from the date represented byymwd. Equivalent toymwd + -dm.6)β Subtractsdy.count()years from the date represented byymwd. Equivalent toymwd + -dy.
And a lilβ extra rule, no cap: 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 π
π¦ Notes π¦
π¨ Okay bestie, this part matters: even ifymwd.ok()is true, the resultingyear_month_weekdaymay not represent a valid date ifymwd.index()is 5. So donβt let theok()pass fool you into thinking itβs automatically a valid date β thatindex()of 5 is the plot twist π.
π Example π
Run this code, bestie:
#include <cassert>
#include <chrono>
#include <iostream>
int main()
{
auto ymwdi{1/std::chrono::Wednesday[1]/2021};
std::cout << ymwdi << '\n';
ymwdi = std::chrono::years(5) + ymwdi;
// First Wednesday in January, 2026
std::cout << ymwdi << '\n';
assert(static_cast<std::chrono::year_month_day>(ymwdi) ==
std::chrono::January/7/2026);
ymwdi = ymwdi - std::chrono::months(6);
// First Wednesday in July, 2025
std::cout << ymwdi << '\n';
assert(static_cast<std::chrono::year_month_day>(ymwdi) ==
std::chrono::July/2/2025);
}
Output: π»
2021/Jan/Wed[1]
2026/Jan/Wed[1]
2025/Jul/Wed[1]
Source/version/date cuteness, exactly as the page signs off: http://cppreference.com, 2024.06.10, and the page label std::chrono...th_weekday)(3). 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