Skip to content

Commit af0c1b6

Browse files
fix: localTime to support 0 (number) as input
Before: it threw "cannot construct LocalTime of 0" After: correctly constructs LocalTime with 1970-01-01T00:00:00
1 parent 29ca2d5 commit af0c1b6

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/datetime/localTime.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ test('basic', () => {
126126
`"Cannot parse "undefined" into LocalTime"`,
127127
)
128128

129+
expect(localTime(0).toISODateTime()).toMatchInlineSnapshot(`"1970-01-01T00:00:00"`)
130+
129131
expect(localTime.getTimezone()).toBe('UTC')
130132
expect(localTime.isTimezoneValid('Europe/Stockholm')).toBe(true)
131133
expect(localTime.isTimezoneValid('Europe/Stockholm2')).toBe(false)

src/datetime/localTime.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,6 @@ class LocalTimeFactory {
714714
* Returns null if invalid
715715
*/
716716
parseOrNull(d: LocalTimeInputNullable): LocalTime | null {
717-
if (!d) return null
718717
if (d instanceof LocalTime) return d
719718

720719
let date
@@ -723,6 +722,9 @@ class LocalTimeFactory {
723722
date = d
724723
} else if (typeof d === 'number') {
725724
date = new Date(d * 1000)
725+
} else if (!d) {
726+
// This check is after checking the number, to support `0`
727+
return null
726728
} else if (typeof (d as any) !== 'string') {
727729
// unexpected type, e.g Function or something
728730
return null

0 commit comments

Comments
 (0)