[libcxx-commits] [PATCH] D70282: [libc++] [chrono] Fix year_month_weekday::ok() implementation.

Marek Kurdej via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 14 16:27:01 PST 2019


curdeius created this revision.
curdeius added reviewers: ldionne, EricWF, mclow.lists.
Herald added subscribers: libcxx-commits, dexonsmith, christof.
Herald added a project: libc++.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D70282

Files:
  libcxx/include/chrono
  libcxx/test/std/utilities/time/time.cal/time.cal.ymwd/time.cal.ymwd.members/ok.pass.cpp


Index: libcxx/test/std/utilities/time/time.cal/time.cal.ymwd/time.cal.ymwd.members/ok.pass.cpp
===================================================================
--- libcxx/test/std/utilities/time/time.cal/time.cal.ymwd/time.cal.ymwd.members/ok.pass.cpp
+++ libcxx/test/std/utilities/time/time.cal/time.cal.ymwd/time.cal.ymwd.members/ok.pass.cpp
@@ -27,8 +27,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<const year_month_weekday>().ok());
     ASSERT_SAME_TYPE(bool, decltype(std::declval<const year_month_weekday>().ok()));
@@ -46,6 +52,15 @@
     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, 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)
     {
Index: libcxx/include/chrono
===================================================================
--- libcxx/include/chrono
+++ 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<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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70282.229423.patch
Type: text/x-patch
Size: 3546 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20191115/8ea4bde3/attachment.bin>


More information about the libcxx-commits mailing list