From 4358ea7b6ee0a7791668c8f65c09d4c2ce83c89e Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Sat, 2 Jun 2018 22:55:35 -0400 Subject: [PATCH] [API BREAKING] Remove conversion from weekday to unsigned * There has been a great deal of anguish over the encoding of weekdays: whether [0, 6] maps to [Sunday, Saturday] or [1, 7] maps to [Monday, Sunday]. This commit attempts to address that issue, but will break a small amount of code at compile-time. See below on how to fix that. * The weekday constructor used to accept [0, 6] to represent [Sunday, Saturday]. It now accepts [0, 7] to represent [Sunday, Saturday] with both 0 and 7 mapping to Sunday. * The conversion from weekday to unsigned has been removed. * To convert a weekday to unsigned replace: auto u = unsigned{wd}; with: auto u = (wd - Sunday).count(); This maps [Sunday, Saturday] to [0, 6], which is the C/POSIX mapping. If you prefer the ISO mapping ([Monday, Sunday] -> [1, 7]), then do: auto u = (wd - Monday).count() + 1; --- date.html | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/date.html b/date.html index 11a9a40..78e58b2 100644 --- a/date.html +++ b/date.html @@ -3220,7 +3220,6 @@ public: constexpr weekday& operator+=(const days& d) noexcept; constexpr weekday& operator-=(const days& d) noexcept; - constexpr explicit operator unsigned() const noexcept; constexpr bool ok() const noexcept; constexpr weekday_indexed operator[](unsigned index) const noexcept; constexpr weekday_last operator[](last_spec) const noexcept; @@ -3249,27 +3248,27 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt, w std::basic_string<CharT, Traits, Alloc>* abbrev = nullptr, std::chrono::minutes* offset = nullptr); -constexpr weekday Sunday{0}; constexpr weekday Monday{1}; constexpr weekday Tuesday{2}; constexpr weekday Wednesday{3}; constexpr weekday Thursday{4}; constexpr weekday Friday{5}; constexpr weekday Saturday{6}; +constexpr weekday Sunday{7};

Overview

-weekday represents a day of the week in the Gregorian calendar. It should -only be representing values in the range 0 to 6, corresponding to Sunday thru Saturday. -However it may hold values outside this range. It can be constructed with any +weekday represents a day of the week in the Gregorian calendar. +It can be constructed with any unsigned value, which will be subsequently truncated to fit into -weekday's internal storage. weekday is equality comparable. +weekday's internal storage. The values [1, 6] map to Monday thru Saturday. +Both 0 and 7 map to Sunday. Other values in the range [8, 255] will be stored, and will +represent values of weekday that are !ok(). +weekday is equality comparable. weekday is not less-than comparable because there is no universal consensus -on which day is the first day of the week. This design chooses the encoding of 0 to 6 to -represent Sunday thru Saturday only because this is consistent with existing C and C++ -practice. However weekday's comparison and arithmetic operations treat the +on which day is the first day of the week. weekday's comparison and arithmetic operations treat the days of the week as a circular range, with no beginning and no end. One can stream out a weekday for debugging purposes. weekday has explicit conversions to and from unsigned. There are 7 weekday constants, one for each @@ -3301,9 +3300,14 @@ explicit constexpr weekday::weekday(unsigned wd) noexcept;

-Effects: Constructs an object of type weekday by constructing -wd_ with wd. The value held is unspecified if wd -is not in the range [0, 255]. +Effects: Constructs an object of type weekday +which represents a day of the week according to the ISO 8601 mapping +of integers to days of the week. Additionally, if wd == +0, Sunday is represented. [Note: Sunday can be +constructed from both 0 and 7 — end note]. If +wd is in the range [8, 255], wd_ is +initialized with wd. If wd is not in the range [0, 255], the value +held is unspecified.

@@ -3421,23 +3425,14 @@ constexpr weekday& weekday::operator-=(const days& d) noexcept;

-
-constexpr explicit weekday::operator unsigned() const noexcept;
-
- -
-

-Returns: wd_. -

-
-
 constexpr bool weekday::ok() const noexcept;
 

-Returns: wd_ <= 6. +Returns: true if *this holds a value in the range +Monday thru Sunday, else returns false.

@@ -3467,7 +3462,7 @@ constexpr bool operator==(const weekday& x, const weekday& y) noexcept;

-Returns: unsigned{x} == unsigned{y}. +Returns: x.wd_ == y.wd_.