From 16f02a72b8191d1659b8d6bb672a923963c11773 Mon Sep 17 00:00:00 2001 From: Zhuyuyangyy <193758752+Zhuyuyangyy@users.noreply.github.com> Date: Sat, 31 Oct 2026 17:37:09 +0800 Subject: [PATCH 1/2] fix: parse negative ten-digit timestamps as seconds --- src/date.ts | 2 +- test/date.test.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/date.ts b/src/date.ts index f3dae43..1edbcdb 100644 --- a/src/date.ts +++ b/src/date.ts @@ -166,7 +166,7 @@ export function timestamp(t?: number | string): number | Date { // Convert timestamp to Date // Timestamp(timestampValue) let v = typeof t === 'string' ? Number(t) : t; - if (String(v).length === 10) { + if (String(v).length === 10 || (v < 0 && String(-v).length === 10)) { v *= 1000; } return new Date(v); diff --git a/test/date.test.ts b/test/date.test.ts index c6f348b..bd69c23 100644 --- a/test/date.test.ts +++ b/test/date.test.ts @@ -250,6 +250,21 @@ describe('test/date.test.ts', () => { assert.equal((utility.timestamp('1385091596') as Date).getTime(), 1385091596000); assert.equal((utility.timestamp('1385091596000') as Date).getTime(), 1385091596000); }); + + it('should parse negative seconds while preserving existing unit inference', () => { + const cases: Array<[number | string, number]> = [ + [ -1385091596, -1385091596000 ], + [ '-1385091596', -1385091596000 ], + [ 1385091596000, 1385091596000 ], + [ -1385091596000, -1385091596000 ], + [ 1385091596, 1385091596000 ], + [ -138509159, -138509159000 ], + [ '-138509159', -138509159000 ], + ]; + for (const [ input, expected ] of cases) { + assert.equal((utility.timestamp(input) as Date).getTime(), expected, String(input)); + } + }); }); describe('dateToUnixTimestamp()', () => { From c4cc710bc03a2461663c0b661a145b65b22482a7 Mon Sep 17 00:00:00 2001 From: Zhuyuyangyy <193758752+Zhuyuyangyy@users.noreply.github.com> Date: Sat, 31 Oct 2026 17:44:17 +0800 Subject: [PATCH 2/2] fix: preserve fractional timestamp input behavior --- src/date.ts | 2 +- test/date.test.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/date.ts b/src/date.ts index 1edbcdb..74cd73c 100644 --- a/src/date.ts +++ b/src/date.ts @@ -166,7 +166,7 @@ export function timestamp(t?: number | string): number | Date { // Convert timestamp to Date // Timestamp(timestampValue) let v = typeof t === 'string' ? Number(t) : t; - if (String(v).length === 10 || (v < 0 && String(-v).length === 10)) { + if (String(v).length === 10 || (v < 0 && Number.isInteger(v) && String(-v).length === 10)) { v *= 1000; } return new Date(v); diff --git a/test/date.test.ts b/test/date.test.ts index bd69c23..cebd5be 100644 --- a/test/date.test.ts +++ b/test/date.test.ts @@ -260,6 +260,10 @@ describe('test/date.test.ts', () => { [ 1385091596, 1385091596000 ], [ -138509159, -138509159000 ], [ '-138509159', -138509159000 ], + [ -12345.6789, -12345 ], + [ '-12345.6789', -12345 ], + [ 12345.6789, 12345678 ], + [ '12345.6789', 12345678 ], ]; for (const [ input, expected ] of cases) { assert.equal((utility.timestamp(input) as Date).getTime(), expected, String(input));