File tree Expand file tree Collapse file tree 2 files changed +8
-2
lines changed
tests/std/tests/P0355R7_calendars_and_time_zones_dates Expand file tree Collapse file tree 2 files changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -473,10 +473,12 @@ namespace chrono {
473
473
private:
474
474
unsigned char _Weekday;
475
475
476
- // courtesy of Howard Hinnant
476
+ // courtesy of Howard Hinnant (modified to avoid overflow)
477
477
// https://howardhinnant.github.io/date_algorithms.html#weekday_from_days
478
478
_NODISCARD static constexpr unsigned int _Weekday_from_days(int _Tp) noexcept {
479
- return static_cast<unsigned int>(_Tp >= -4 ? (_Tp + 4) % 7 : (_Tp + 5) % 7 + 6);
479
+ _STL_INTERNAL_STATIC_ASSERT(~0u % 7u == 3u); // offset for `_Tp < 0` needs to change
480
+ const auto _Before_modulo = static_cast<unsigned int>(_Tp) + (_Tp >= 0 ? 4u : 0u);
481
+ return _Before_modulo % 7u; // separate expression for MSVC codegen, see GH-5153
480
482
}
481
483
};
482
484
Original file line number Diff line number Diff line change @@ -281,6 +281,10 @@ constexpr void weekday_test() {
281
281
assert (Sunday - Monday == days{6 });
282
282
assert (Sunday - Tuesday == days{5 });
283
283
assert (Wednesday - Thursday == days{6 });
284
+
285
+ // GH-5153 "<chrono>: integer overflow in weekday::weekday(sys_days::max())"
286
+ assert (weekday{sys_days::max ()} == weekday{sys_days::max () - days{7 }});
287
+ assert (weekday{local_days::max ()} == weekday{local_days::max () - days{7 }});
284
288
}
285
289
286
290
constexpr void weekday_indexed_test () {
You can’t perform that action at this time.
0 commit comments