Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions stl/inc/chrono
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,12 @@ namespace chrono {
private:
unsigned char _Weekday;

// courtesy of Howard Hinnant
// courtesy of Howard Hinnant (modified to avoid overflow)
// https://howardhinnant.github.io/date_algorithms.html#weekday_from_days
_NODISCARD static constexpr unsigned int _Weekday_from_days(int _Tp) noexcept {
return static_cast<unsigned int>(_Tp >= -4 ? (_Tp + 4) % 7 : (_Tp + 5) % 7 + 6);
_STL_INTERNAL_STATIC_ASSERT(~0u % 7u == 3u); // offset for `_Tp < 0` needs to change
const auto _Before_modulo = static_cast<unsigned int>(_Tp) + (_Tp >= 0 ? 4u : 0u);
Comment thread
CaseyCarter marked this conversation as resolved.
return _Before_modulo % 7u; // separate expression for MSVC codegen, see GH-5153
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ constexpr void weekday_test() {
assert(Sunday - Monday == days{6});
assert(Sunday - Tuesday == days{5});
assert(Wednesday - Thursday == days{6});

// GH-5153 "<chrono>: integer overflow in weekday::weekday(sys_days::max())"
assert(weekday{sys_days::max()} == weekday{sys_days::max() - days{7}});
assert(weekday{local_days::max()} == weekday{local_days::max() - days{7}});
}

constexpr void weekday_indexed_test() {
Expand Down