Namespaces
Variants
Views
Actions

std::chrono::year_month_day::operator sys_days, std::chrono::year_month_day::operator local_days

From cppreference.com
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
 
 
constexpr operator std::chrono::sys_days() const noexcept;
(1) (since C++20)
explicit constexpr operator std::chrono::local_days() const noexcept;
(2) (since C++20)

Converts *this to a std::chrono::time_point representing the same date as this year_month_day.

1) If ok() is true, the return value holds a count of days from the std::chrono::system_clock epoch (1970-01-01) to *this. The result is negative if *this represent a date prior to it.
Otherwise, if the stored year and month are valid (year().ok() && month().ok() is true), then the returned value is sys_days(year()/month()/1d) + (day() - 1d).
Otherwise (if year().ok() && month().ok() is false), the return value is unspecified.
A std::chrono::sys_days in the range [std::chrono::days{-12687428}, std::chrono::days{11248737}], when converted to year_month_day and back, yields the same value.
2) Same as (1) but returns local_days instead. Equivalent to return local_days(sys_days(*this).time_since_epoch());.

[edit] 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);

[edit] Example

#include <iostream>
#include <iomanip>
#include <chrono>
 
void print(std::chrono::year_month_day const ymd) {
//  std::cout << ymd; // when C++20 stream operator<< ready, or:
    std::cout << static_cast<int>(ymd.year()) << '/'
              << std::setw(2) << std::setfill('0')
              << static_cast<unsigned>(ymd.month()) << '/'
              << std::setw(2) << static_cast<unsigned>(ymd.day());
}
 
int main()
{
    using namespace std::chrono;
    const auto today = sys_days{floor<days>(system_clock::now())};
    for (const year_month_day ymd : { {November/15/2020}, {November/15/2120}, today }) {
        print(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 290 day(s) ago
2120/11/15 is 36234 day(s) from now
2021/09/01 is today!