Commit 8e34be2f authored by Marek Kurdej's avatar Marek Kurdej
Browse files

[libc++] [chrono] Fix year_month_weekday::ok() implementation.

Reviewers: ldionne, EricWF, mclow.lists

Reviewed By: mclow.lists

Subscribers: christof, dexonsmith, libcxx-commits

Tags: #libc

Differential Revision: https://reviews.llvm.org/D70282
parent 767eadd7
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -1858,6 +1858,7 @@ public:
         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 @@ public:
    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<sys_days>(__y / __m / 1)} +
            days{(__wdi.index() - 1) * 7 + 1};
        return static_cast<unsigned>(__nth_weekday_day.count()) <=
               static_cast<unsigned>((__y / __m / last).day());
    }

    static constexpr year_month_weekday __from_days(days __d) noexcept;
+23 −3
Original line number Diff line number Diff line
@@ -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 <chrono>
#include <type_traits>
@@ -28,7 +30,13 @@ int main(int, char**)
    using year_month_weekday = std::chrono::year_month_weekday;

    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<const year_month_weekday>().ok());
    ASSERT_SAME_TYPE(bool, decltype(std::declval<const year_month_weekday>().ok()));
@@ -45,7 +53,19 @@ int main(int, char**)
    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<unsigned>(-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)
    {