std::chrono...ay::operator(3)
π std::chrono...ay::operator(3) π
Bestie, welcome to this manual page from the C++ Standard Libary β yes, thatβs the manual-name vibe right there β and itβs all about the conversion operators on std::chrono::year_month_day: the sys_days one and the local_days one. Calendar math, but make it main-character energy. ποΈβ¨
π NAME π
Officially, the NAME block is std::chrono::year_month_day::operatorsys_days,std::chrono::year_month_day::operator β std::chrono::year_month_day::operatorsys_days,std::chrono::year_month_day::operator. Yep, it literally repeats the exact same thing; thatβs the bit. π
β¨ Synopsis β¨
Two overloads, both constexpr, both noexcept, both (since C++20). Slay. π
constexpr operator std::chrono::sys_days() const noexcept;
(1) (since C++20)
constexpr explicit operator std::chrono::local_days() const noexcept;
(2) (since C++20)
Okay so both of these convert *this to a std::chrono::time_point representing the same date as this year_month_day. Obsessed. π
- If
ok()istrue, the return value holds a count of days from thestd::chrono::system_clockepoch (1970-01-01) to*this. The result is negative if*thisrepresent a date prior to it. Otherwise, if the stored year and month are valid (year().ok() && month().ok()istrue), then the returned value issys_days(year()/month()/1d) + (day() - 1d). Otherwise (ifyear().ok() && month().ok()isfalse), the return value is unspecified. Astd::chrono::sys_daysin the range[std::chrono::days{-12687428}, std::chrono::days{11248737}], when converted toyear_month_dayand back, yields the same value. π₯ - Same as (1) but returns
local_daysinstead. Equivalent toreturn local_days(sys_days(*this).time_since_epoch());. π¦
π§ Notes π§
Converting to std::chrono::sys_days and back can be used to normalize a year_month_day that contains an invalid day but a valid year and month:
using namespace std::chrono;
auto ymd = 2017y/January/0;
ymd = sys_days{ymd};
// ymd is now 2016y/December/31
Normalizing the year and month can be done by adding (or subtracting) zero std::chrono::months:
using namespace std::chrono;
constexpr year_month_day normalize(year_month_day ymd)
{
ymd += months{0}; // normalizes year and month
return sys_days{ymd}; // normalizes day
}
static_assert(normalize(2017y/33/59) == 2019y/10/29);
That static_assert ate and left no crumbs, ngl. ππ
π Example π
// Run this code β like, literally run it, bestie:
#include <chrono>
#include <iostream>
int main()
{
using namespace std::chrono;
const auto today =
sys_days{std::chrono::floor<days>(system_clock::now())};
for (const year_month_day ymd : {{November/15/2020},
{November/15/2120}, today})
{
std::cout << ymd;
const auto delta = (sys_days{ymd} - today).count();
(delta < 0) ? std::cout << " was " << -delta << " day(s) ago\n"
: (delta > 0) ? std::cout << " is " << delta << " day(s) from now\n"
: std::cout << " is today!\n";
}
}
Possible output:
2020-11-15 was 1014 day(s) ago
2120-11-15 is 35510 day(s) from now
2023-08-26 is today!
The way this prints dates and does day-math is honestly iconic. ππΈ
Later, bestie! π Source/version/date sign-off vibes: http://cppreference.com, 2024.06.10, std::chrono...ay::operator(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