fix(set): clamp hour() to 23 instead of the invalid 24

hour(n) with n >= 24 clamped n to 24, which walkTo() rejects as an invalid
hour (valid range 0-23), nulling the epoch and invalidating the object.
Clamp to 23 to match the other overflow-clamping setters (date, month).
This commit is contained in:
dualfroz committed 2026-09-04 09:43:57 +02:00
1 parent cdeb1866ab
commit d48b259893
3 files changed
+96 -1

No files matched your search

+14
View File
@@ -238,3 +238,17 @@ test('month()/date() setters clamp the day to the month length', (t) => {
t.equal(iso(spacetime('2021-02-10').date(31)), '2021-02-28', 'date(31) in a common february clamps to the 28th')
t.end()
})
test('hour() setter clamps out-of-range hours to a valid hour', (t) => {
const s = spacetime('2021-03-14 10:00:00', 'UTC')
// an out-of-range hour must clamp to the last valid hour (23), not produce
// an invalid spacetime with a null epoch
t.equal(s.hour(24).hour(), 23, 'hour(24) clamps to 23')
t.equal(s.hour(25).hour(), 23, 'hour(25) clamps to 23')
t.equal(s.hour(100).hour(), 23, 'hour(100) clamps to 23')
t.equal(s.hour(24).isValid(), true, 'hour(24) stays valid')
t.equal(s.hour(24).format('iso-short'), '2021-03-14', 'hour(24) keeps the same day')
// negative hours still clamp to 0
t.equal(s.hour(-1).hour(), 0, 'hour(-1) clamps to 0')
t.end()
})