std::chrono...torsys_days,(3)

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

πŸ’… NAME

std::chrono::year_month_day_last::operator sys_days, std::chrono::year_month_day_last::operator local_days β€” bestie, this is the C++ Standard Library's glow-up conversion moment ✨ (the manpage header calls it "std::chrono...torsys_days,(3)", iconic truncation honestly)


✨ SYNOPSIS

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)


πŸ¦‹ DESCRIPTION

okay so picture this: you've got a year_month_day_last object, and you want it to become a std::chrono::time_point that represents the same date. That's literally the whole job. It converts *this into a std::chrono::time_point representing the same date as this year_month_day_last. πŸ’«

The way it does it? It's equivalent to composing a year_month_day out of year(), month() and day(), and then converting that year_month_day to the destination type. Chef's kiss. No cap. πŸŽ€

  1. (1) Equivalent to std::chrono::sys_days(year()/month()/day()). πŸ’–
  2. (2) Equivalent to std::chrono::local_days(year()/month()/day()). πŸ¦‹
ngl the second one is explicit β€” that's not a vibe check you can skip, you gotta be upfront about it. Both are constexpr and noexcept tho, so she's fast and she behaves. πŸ‘‘

πŸŽ€ EXAMPLE

// Run this code

#include <array>
#include <chrono>
#include <iostream>
#include <string_view>
using namespace std::chrono;
using namespace std::literals;

int main()
{
    constexpr std::chrono::year y{2023y};
    constexpr std::array quarters{"1st"sv, "2nd"sv, "3rd"sv, "4th"sv};
    constexpr auto mq{12 / 4}; // months per quarter

    std::cout << "In year " << static_cast<int>(y) << '\n';
    for (auto q = 1; q < 5; ++q)
    {
        const auto ls = y / std::chrono::month(q * mq) / Sunday[last];
        const auto ld = y / std::chrono::month(q * mq) / last;
        // subtract last Sunday from last day for day of week
        const auto index = (sys_days(ld) - sys_days(ls)).count();
        std::cout << "The " << quarters[q - 1] << " quarter ends on a "
                  << std::chrono::weekday(index) << '\n';
    }
}

🌈 OUTPUT

In year 2023
The 1st quarter ends on a Fri
The 2nd quarter ends on a Fri
The 3rd quarter ends on a Sat
The 4th quarter ends on a Sun

literally the entire year laid out, quarter by quarter. ate and left no crumbs. πŸ’…πŸ”₯


🫢 SEE ALSO

  • operator sys_days β€” converts to a std::chrono::time_point
  • operator local_days β€” public member function of std::chrono::year_month_day

<div>🌷 mwah πŸ’‹ this page came straight from cppreference.com, dated 2024.06.10, and yes the source line really does call it std::chrono...torsys_days,(3). go be great with your time_points bestie 🫢✨</div>

πŸ“¦ where this page comes from