[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
Fri Nov 15 04:08:56 PST 2019
curdeius updated this revision to Diff 229503.
curdeius added a comment.
- Add tests. Update Returns commment according to [time.cal.ymwd.members]/21.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D70282/new/
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
@@ -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>
@@ -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<const year_month_weekday>().ok());
ASSERT_SAME_TYPE(bool, decltype(std::declval<const year_month_weekday>().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<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)
{
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.229503.patch
Type: text/x-patch
Size: 4317 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20191115/9d5ec829/attachment.bin>
More information about the libcxx-commits
mailing list