W3cubDocs

/C++

std::chrono::year_month_day_last

Defined in header <chrono>
class year_month_day_last;
(since C++20)

The class year_month_day_last represents the last day of a specific year and month. It is a field-based time point, with a resolution of std::chrono::days, subject to the limit that it can only represent the last day of a month.

std::chrono::years- and std::chrono::months-oriented arithmetic are supported directly. An implicit conversion to std::chrono::sys_days allows std::chrono::days-oriented arithmetic to be performed efficiently.

year_month_day_last is a TriviallyCopyable StandardLayoutType.

Member functions

constructs a year_month_day_last object
(public member function)
modifies the time point by some number of months or years
(public member function)
accesses the fields of this object
(public member function)
converts to a std::chrono::time_point
(public member function)
checks whether this object represents a valid date
(public member function)

Nonmember functions

(C++20)
compares two year_month_day_last values
(function)
(C++20)
adds or subtracts a year_month_day_last and some number of years or months
(function)
(C++20)
outputs a year_month_day_last into a stream
(function template)

Helper classes

(C++20)
formatting support for year_month_day_last
(class template specialization)

Example

#include <chrono>
#include <iostream>
 
// until online compiler supports streaming the year_month_day_last:
std::ostream& operator<<(std::ostream& os, std::chrono::year_month_day_last ymdl)
{
    return os << static_cast<int>(ymdl.year()) << '/'
              << static_cast<unsigned>(ymdl.month()) << '/'
              << static_cast<unsigned>(ymdl.day());
}
 
int main()
{
    const auto ymd = std::chrono::year_month_day
    {
        std::chrono::floor<std::chrono::days>( std::chrono::system_clock::now())
    };
 
    const std::chrono::year_month_day_last ymdl
    {
        ymd.year(), ymd.month() / std::chrono::last
    };
 
    std::cout << "The date of the last day of this month is: " << ymdl << '\n';
 
    // The 'last' object can be placed wherever it is legal to place a 'day':
    using namespace std::chrono;
    constexpr std::chrono::year_month_day_last
        ymdl1 = 2023y / February / last,
        ymdl2 = last / February / 2023y,
        ymdl3 = February / last / 2023y;
    static_assert( ymdl1 == ymdl2 and ymdl2 == ymdl3 );
}

Possible output:

The date of the last day of this month is: 2023/6/30

See also

(C++20)
represents a specific year, month, and day
(class)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/chrono/year_month_day_last