std::chrono::year_month_weekday_last

From cppreference.com
< cpp‎ | chrono
 
 
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)
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++17)

Elementary string conversions
(C++17)
(C++17)
 
Date and time utilities
(C++11)
(C++11)
Time of day
(C++20)



(C++20)(C++20)(C++20)(C++20)
Clocks
(C++20)
                                             
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Calendars
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
year_month_weekday_last
(C++20)
Time zones
(C++20)
(C++20)
(C++20)
(C++20)
C-style date and time
 
 
Defined in header <chrono>
class year_month_weekday_last;
(since C++20)

The class year_month_weekday_last represents the last weekday of a specific year and month. It is a field-based time point, with a resolution of std::chrono::days, except that it is limited to pointing to the last weekday of a year and 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_weekday_last is a TriviallyCopyable StandardLayoutType.

Member functions

constructs a year_month_weekday_last
(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

compares two year_month_weekday_last objects
(function)
adds or subtracts a year_month_weekday_last and some number of years or months
(function)
outputs a year_month_weekday_last into a stream
(function template)

Helper classes

specialization of std::formatter that formats a year_month_weekday_last according to the provided format
(class template specialization)

Example

#include <format>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <string>
#include <string_view>
 
void calendar(std::chrono::year_month_day ymd)
{
    auto show_content = [=](const std::string_view separator) {
        const auto ymdl{std::chrono::last / ymd.month() / ymd.year()};
        const int days_in_month = static_cast<unsigned>(ymdl.day());
        const int first_weekday = std::chrono::weekday{
            ymd.year() / ymd.month() / 1}.c_encoding();
 
        for (int d = 1 - first_weekday; d <= days_in_month;) {
            std::cout << separator;
            for (int wd = 0; wd != 7; ++wd, ++d) {
                std::cout << "│";
                if (d >= 1 && d <= days_in_month) {
                    const bool q{unsigned(d) == unsigned(ymd.day())};
                    std::cout << std::setfill('0')
                              << (q ? "<" : " ") << std::setw(2) << d
                              << (q ? ">" : " ");
                } else { std::cout << "    "; }
            }
            std::cout << "│\n";
        }
    };
 
    const auto yyyy_mm_dd = [=](int width) {
        return std::format("{0:^{1}}", std::format("{:4}/{:<02}/{:<02}",
            (int)ymd.year(), (unsigned)ymd.month(), (unsigned)ymd.day()), width);
    }(34);
 
    std::cout << "┌──────────────────────────────────┐\n"
                 "│"        << yyyy_mm_dd <<        "│\n"
                 "├────┬────┬────┬────┬────┬────┬────┤\n"
                 "│ Su │ Mo │ Tu │ We │ Th │ Fr │ Sa │\n";
    show_content("├────┼────┼────┼────┼────┼────┼────┤\n");
    std::cout << "└────┴────┴────┴────┴────┴────┴────┘\n";
}
 
int main()
{
    calendar(std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now()));
}

Possible output:

┌──────────────────────────────────┐
│            2020/09/04            │
├────┬────┬────┬────┬────┬────┬────┤
│ Su │ Mo │ Tu │ We │ Th │ Fr │ Sa │
├────┼────┼────┼────┼────┼────┼────┤
│    │    │ 01 │ 02 │ 03 │<04>│ 05 │
├────┼────┼────┼────┼────┼────┼────┤
│ 06 │ 07 │ 08 │ 09 │ 10 │ 11 │ 12 │
├────┼────┼────┼────┼────┼────┼────┤
│ 13 │ 14 │ 15 │ 16 │ 17 │ 18 │ 19 │
├────┼────┼────┼────┼────┼────┼────┤
│ 20 │ 21 │ 22 │ 23 │ 24 │ 25 │ 26 │
├────┼────┼────┼────┼────┼────┼────┤
│ 27 │ 28 │ 29 │ 30 │    │    │    │
└────┴────┴────┴────┴────┴────┴────┘