std::chrono...rono::month)(3)
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)β Addsms.count()months tom. The month value held in the result is computed by first evaluatingstatic_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)β Subtractsms.count()months frommand returns the result. Equivalent toreturn m + -ms;.4)β Ifm1.ok()andm2.ok()are both true, returns astd::chrono::monthsvaluemsuch thatm.count()is in the range[0, 11]andm2 + m == m1. Otherwise the returned value is unspecified.
π« Return value π«
1-3)β Astd::chrono::monthholding a month value calculated as described above.4)β Astd::chrono::monthsrepresenting the distance betweenm1andm2.
π§ 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 monthoperator--β(public member function)operator--(int)operator+=β adds or subtracts a number of monthsoperator-=β(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
- 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