std::chrono...onth_day::ok(3)
πΈ std::chrono...onth_day::ok(3) πΈ
okay bestie, welcome to the C++ Standard Libary manual page for std::chrono...onth_day::ok(3) β yes, that manual name is literally spelled C++ Standard Libary on the page, and we are keeping it, no cap π
. This is the ok() vibe check for std::chrono::year_month_day, and itβs lowkey iconic.
β¨ NAME β¨
std::chrono::year_month_day::ok - std::chrono::year_month_day::ok
π¦ Synopsis π¦
hereβs the signature, bestie β itβs giving constexpr, noexcept, since-C++20 energy:
constexpr bool ok() const noexcept; (since C++20)
and what does it do? checks if this year_month_day object represents a valid calendar date. periodt ποΈβ¨
π Return value π
trueif thisyear_month_dayobject represents a valid calendar date, that is, the stored year, month, and day values are all valid and the stored day value is within the number of days in the given year and month.falseotherwise.
π Possible implementation π
not me showing you a possible implementation like itβs a fit check π:
constexpr bool std::chrono::year_month_day::ok() const noexcept
{
return year().ok() && month().ok() && day().ok() &&
day() <= (year()/month()/std::chrono::last).day();
}
π Example π
this example is literally the whole plot: leap years, invalid dates, snap-to-last-day, overflow-to-next-month. // Run this code marker included, periodt:
// Run this code
#include <chrono>
int main()
{
constexpr auto ymd1 {std::chrono::day(1)/std::chrono::July/2020};
static_assert(ymd1.ok());
constexpr auto ymd2 {std::chrono::year(2020)/7/42};
static_assert(not ymd2.ok());
constexpr auto ymd3 {std::chrono::February/29/2020}; // ok, leap
year
static_assert(ymd3.ok());
constexpr auto ymd4 = ymd3 + std::chrono::years{1}; // bad, not a
leap year
static_assert(ymd4 == std::chrono::February/29/2021 and not
ymd4.ok());
// to fix the bad date we may want to snap to the last day of the
month:
if constexpr (!ymd4.ok())
{
constexpr auto ymd =
ymd4.year()/ymd4.month()/std::chrono::last;
static_assert(ymd == std::chrono::February/28/2021 and
ymd.ok());
}
// or we may want to overflow to the next month:
if constexpr (!ymd4.ok())
{
constexpr auto st =
std::chrono::sys_time<std::chrono::days>{ymd4};
constexpr auto ymd = std::chrono::year_month_day{st};
static_assert(ymd == std::chrono::March/1/2021 and ymd.ok());
}
}
k bye bestie π this cutie came from http://cppreference.com, dated 2024.06.10, with footer title std::chrono...onth_day::ok(3). Mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms