diff --git a/libcxx/include/chrono b/libcxx/include/chrono --- a/libcxx/include/chrono +++ b/libcxx/include/chrono @@ -1858,6 +1858,7 @@ constexpr weekday_indexed operator[](unsigned __index) const noexcept; constexpr weekday_last operator[](last_spec) const noexcept; + // TODO: Make private? static constexpr unsigned char __weekday_from_days(int __days) noexcept; }; @@ -2568,8 +2569,13 @@ inline constexpr bool ok() const noexcept { if (!__y.ok() || !__m.ok() || !__wdi.ok()) return false; - // TODO: make sure it's a valid date - return true; + if (__wdi.index() <= 4) return true; + auto __nth_weekday_day = + __wdi.weekday() - + chrono::weekday{static_cast(__y / __m / 1)} + + days{(__wdi.index() - 1) * 7 + 1}; + return static_cast(__nth_weekday_day.count()) <= + static_cast((__y / __m / last).day()); } static constexpr year_month_weekday __from_days(days __d) noexcept; diff --git a/libcxx/test/std/utilities/time/time.cal/time.cal.ymwd/time.cal.ymwd.members/ok.pass.cpp b/libcxx/test/std/utilities/time/time.cal/time.cal.ymwd/time.cal.ymwd.members/ok.pass.cpp --- a/libcxx/test/std/utilities/time/time.cal/time.cal.ymwd/time.cal.ymwd.members/ok.pass.cpp +++ b/libcxx/test/std/utilities/time/time.cal/time.cal.ymwd/time.cal.ymwd.members/ok.pass.cpp @@ -11,7 +11,9 @@ // class year_month_weekday; // constexpr bool ok() const noexcept; -// Returns: m_.ok() && y_.ok(). +// Returns: If any of y_­.ok(), m_­.ok(), or wdi_­.ok() is false, returns false. +// Otherwise, if *this represents a valid date, returns true. +// Otherwise, returns false. #include #include @@ -27,8 +29,14 @@ using weekday_indexed = std::chrono::weekday_indexed; using year_month_weekday = std::chrono::year_month_weekday; - constexpr month January = std::chrono::January; - constexpr weekday Tuesday = std::chrono::Tuesday; + constexpr month January = std::chrono::January; + constexpr weekday Monday = std::chrono::Monday; + constexpr weekday Tuesday = std::chrono::Tuesday; + constexpr weekday Wednesday = std::chrono::Wednesday; + constexpr weekday Thursday = std::chrono::Thursday; + constexpr weekday Friday = std::chrono::Friday; + constexpr weekday Saturday = std::chrono::Saturday; + constexpr weekday Sunday = std::chrono::Sunday; ASSERT_NOEXCEPT( std::declval().ok()); ASSERT_SAME_TYPE(bool, decltype(std::declval().ok())); @@ -45,7 +53,19 @@ static_assert(!year_month_weekday{year{2019}, month{}, weekday_indexed{} }.ok(), ""); // Bad month & day static_assert(!year_month_weekday{year{-32768}, January, weekday_indexed{} }.ok(), ""); // Bad year & day + static_assert(!year_month_weekday{year{2019}, January, weekday_indexed{Tuesday, static_cast(-1)}}.ok(), ""); // Bad index. + static_assert(!year_month_weekday{year{2019}, January, weekday_indexed{Wednesday, 0}}.ok(), ""); // Bad index. + static_assert( year_month_weekday{year{2019}, January, weekday_indexed{Tuesday, 1}}.ok(), ""); // All OK + static_assert( year_month_weekday{year{2019}, January, weekday_indexed{Tuesday, 4}}.ok(), ""); // All OK + + static_assert(!year_month_weekday{year{2019}, January, weekday_indexed{Monday, 5}}.ok(), ""); // Bad index + static_assert( year_month_weekday{year{2019}, January, weekday_indexed{Tuesday, 5}}.ok(), ""); // All OK + static_assert( year_month_weekday{year{2019}, January, weekday_indexed{Wednesday, 5}}.ok(), ""); // All OK + static_assert( year_month_weekday{year{2019}, January, weekday_indexed{Thursday, 5}}.ok(), ""); // All OK + static_assert(!year_month_weekday{year{2019}, January, weekday_indexed{Friday, 5}}.ok(), ""); // Bad index + static_assert(!year_month_weekday{year{2019}, January, weekday_indexed{Saturday, 5}}.ok(), ""); // Bad index + static_assert(!year_month_weekday{year{2019}, January, weekday_indexed{Sunday, 5}}.ok(), ""); // Bad index for (unsigned i = 0; i <= 50; ++i) {