std::chrono...nth_day_last(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::chrono...nth_day_last(3) πŸ’–

okay so bestie, this is a page from the C++ Standard Library manual, and we're talking about a constructor. specifically std::chrono::year_month_day_last::year_month_day_last β€” yes the name is a whole mouthful, she's giving "my parents gave me four middle names" energy, but stay with me 🫢


✨ NAME ✨

std::chrono::year_month_day_last::year_month_day_last β€” literally the constructor for std::chrono::year_month_day_last. that's it, that's the whole name, no notes πŸ’…


🌸 SYNOPSIS 🌸

constexpr year_month_day_last( const std::chrono::year& y,
                               const std::chrono::month_day_last& mdl
) noexcept;

(since C++20) β€” she's a modern girl, she did not exist before C++20, periodt ⭐


πŸ¦‹ NOTES πŸ¦‹

so what does this cutie actually do? she constructs a year_month_day_last object that stores the year y and the month mdl.month(). and the object she builds represents the last day of that year and month. that's the whole vibe β€” she's the end-of-month icon, the final boss of the calendar πŸŽ€

also! you can make a year_month_day_last a totally different way: by combining a std::chrono::year_month with std::chrono::last using operator/. two paths, same slay ✨


πŸ“ EXAMPLE πŸ“

// Run this code πŸ‘‡

#include <chrono>
#include <iostream>

int main()
{
    std::cout << "All last days of months of a given year:\n";

    constexpr std::chrono::year year{2021};

    for (int i{1}; i <= 12; ++i)
    {
        const std::chrono::month_day_last mdl{std::chrono::month(i) / std::chrono::last};
        const std::chrono::year_month_day_last ymdl{year, mdl};
        std::cout << ymdl << " day: " << ymdl.day() << '\n';
    }
}

πŸ’œ Possible output

All last days of months of a given year:
2021/Jan/last day: 31
2021/Feb/last day: 28
2021/Mar/last day: 31
2021/Apr/last day: 30
2021/May/last day: 31
2021/Jun/last day: 30
2021/Jul/last day: 31
2021/Aug/last day: 31
2021/Sep/last day: 30
2021/Oct/last day: 31
2021/Nov/last day: 30
2021/Dec/last day: 31

look at February doing her little 28 days thing 😭 she's doing her best okay


🌈 SEE ALSO 🌈

  • operator/ β€” the conventional syntax for Gregorian calendar date creation, (C++20) (function). yes she's the other way to build these, go say hi πŸ‘‘

ngl the double-colon name is lowkey terrifying but she's just a constructor, she doesn't bite 🫢

and that's the page!! brought to you by cppreference.com, dated 2024.06.10, filed under std::chrono...nth_day_last(3) in the C++ Standard Library section β€” mwah, go build some dates bestie πŸ’…βœ¨

πŸ“¦ where this page comes from