From bf7f44c584d973ba40e67addaec1306ff7bec1c0 Mon Sep 17 00:00:00 2001 From: dualfroz Date: Fri, 4 Sep 2026 09:43:57 +0200 Subject: [PATCH] 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). --- package-lock.json | 39 --------------------------------------- src/methods/set/set.js | 3 ++- test/set.test.js | 13 +++++++++++++ 3 files changed, 15 insertions(+), 40 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6b0077b..e83a1ff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -688,9 +688,6 @@ "arm" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -705,9 +702,6 @@ "arm" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -722,9 +716,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -739,9 +730,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -756,9 +744,6 @@ "loong64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -773,9 +758,6 @@ "loong64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -790,9 +772,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -807,9 +786,6 @@ "ppc64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -824,9 +800,6 @@ "riscv64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -841,9 +814,6 @@ "riscv64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -858,9 +828,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -875,9 +842,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -892,9 +856,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ diff --git a/src/methods/set/set.js b/src/methods/set/set.js index 1c7d087..b6c83c6 100644 --- a/src/methods/set/set.js +++ b/src/methods/set/set.js @@ -67,8 +67,9 @@ const minutes = function (s, n, goFwd) { const hours = function (s, n, goFwd) { n = validate(n) + // Clamp to 0-23: hour 24 is rejected by walkTo(), which nulls the epoch. if (n >= 24) { - n = 24 + n = 23 } else if (n < 0) { n = 0 } diff --git a/test/set.test.js b/test/set.test.js index 9efdcee..d09a3c1 100644 --- a/test/set.test.js +++ b/test/set.test.js @@ -238,3 +238,16 @@ 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 23, not null the 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() +})