std::chrono...rono::year)(3)
π std::chrono::operator+, std::chrono::operator-(std::chrono::year) π
okay bestie, this is std::chrono...rono::year)(3), from the <pink>C++ Standard Libary</pink> (their spelling, not mine π), and it's the year-math operators page π lowkey iconic, since C++20.
πΈ NAME πΈ
std::chrono::operator+,std::chrono::operator-(std::chrono::year) β literally std::chrono::operator+ and std::chrono::operator- for std::chrono::year, fr fr β¨
π¦ Synopsis π¦
Here's the whole overload lineup, no cap:
constexpr std::chrono::year operator+( const std::chrono::year& y,
const std::chrono::years& ys ) noexcept;
(1) (since C++20)
constexpr std::chrono::year operator+( const std::chrono::years& ys,
const std::chrono::year& y ) noexcept;
(2) (since C++20)
constexpr std::chrono::year operator-( const std::chrono::year& y,
const std::chrono::years& ys ) noexcept;
(3) (since C++20)
constexpr std::chrono::years operator-( const std::chrono::year& y1,
const std::chrono::year& y2 ) noexcept;
(4) (since C++20)
What each one is giving:
1,2)β addsys.count()years toy. same energy, just swapped argument order π3)β subtractsys.count()years fromy.4)β returns the difference in years betweeny1andy2.
β¨ Return value β¨
1,2)βstd::chrono::year(int(y) + ys.count())3)βstd::chrono::year(int(y) - ys.count())4)βstd::chrono::years(int(y1) - int(y2))
π§ Notes π§
Important vibe check: if the resulting year value for (1-3) is outside the range [-32767, 32767], the actual value stored is unspecified π so don't act surprised.
The result of subtracting two year values is a duration of type std::chrono::years. This duration unit represents the length of the average Gregorian year, and the resulting duration bears no relationship to the number of days in the particular years represented by the operands. For example, the result of 2018y - 2017y is std::chrono::years(1), which represents 365.2425 days, not 365 days π
π Example π
// Run this code
#include <cassert>
#include <chrono>
int main()
{
std::chrono::year y{2020};
y = std::chrono::years(12) + y; // overload (2): duration + time point
assert(y == std::chrono::year(2032));
y = y - std::chrono::years(33); // overload (3): time point - duration
assert(y == std::chrono::year(1999));
// y = std::chrono::years(33) - y; // not supported: duration - time point
using namespace std::chrono;
constexpr std::chrono::years ys = 2025y - 2020y; // overload (4)
static_assert(ys == std::chrono::years(5));
}
see how overload (2) does duration + time point, (3) does time point - duration, and (4) is year minus year giving std::chrono::years? slay. also that commented-out line is not supported because duration - time point is a no-go π
π₯ See also π₯
operator++,operator++(int),operator--,operator--(int)β increments or decrements the month; public member function ofstd::chrono::monthoperator+=,operator-=β adds or subtracts a number of months; public member function ofstd::chrono::month
k that's the whole page bestie π«Ά source/version: http://cppreference.com; date: 2024.06.10; footer title: std::chrono...rono::year)(3). periodt π
π¦ 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