From 1f4790c941953db668bae9ce1e6db19a4417c192 Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Thu, 16 Jan 2025 13:19:25 -0300 Subject: [PATCH] feat(timestamp): Parse number-like dates plus offset (#33647) --- lib/util/timestamp.spec.ts | 2 ++ lib/util/timestamp.ts | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/util/timestamp.spec.ts b/lib/util/timestamp.spec.ts index c084c062f3025e..1926ea478aed71 100644 --- a/lib/util/timestamp.spec.ts +++ b/lib/util/timestamp.spec.ts @@ -17,6 +17,8 @@ describe('util/timestamp', () => { ${'2021-01-01'} | ${'2021-01-01T00:00:00.000Z'} ${'20210101000000'} | ${'2021-01-01T00:00:00.000Z'} ${'20211231235959'} | ${'2021-12-31T23:59:59.000Z'} + ${'20210101000000+0000'} | ${'2021-01-01T00:00:00.000Z'} + ${'20211231235959+0000'} | ${'2021-12-31T23:59:59.000Z'} ${'Jan 1, 2021'} | ${'2021-01-01T00:00:00.000Z'} ${'2021/01/01'} | ${'2021-01-01T00:00:00.000Z'} ${'2021-01-02T00:00:00+05:30'} | ${'2021-01-01T18:30:00.000Z'} diff --git a/lib/util/timestamp.ts b/lib/util/timestamp.ts index b40fd93ead2d2a..5c0e3d32b9c9c1 100644 --- a/lib/util/timestamp.ts +++ b/lib/util/timestamp.ts @@ -64,6 +64,15 @@ export function asTimestamp(input: unknown): Timestamp | null { return numberLikeDate.toISO() as Timestamp; } + const numberLikeOffsetDate = DateTime.fromFormat( + input, + 'yyyyMMddHHmmssZZZ', + { zone: 'UTC' }, + ); + if (isValid(numberLikeOffsetDate)) { + return numberLikeOffsetDate.toISO() as Timestamp; + } + const fallbackDate = DateTime.fromMillis( Date.parse(input) - timezoneOffset, { zone: 'UTC' },