From 19c7eec68482e54a2014eb67f7c778358348ac7c Mon Sep 17 00:00:00 2001 From: adriangohjw Date: Fri, 27 Dec 2024 22:32:56 +0800 Subject: [PATCH 1/3] getParsedDate - add 8h to account for timezone --- .../src/utils/__tests__/getParsedData.test.ts | 6 +++--- packages/components/src/utils/getParsedDate.ts | 12 ++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/components/src/utils/__tests__/getParsedData.test.ts b/packages/components/src/utils/__tests__/getParsedData.test.ts index b806507521..a8cd7569d4 100644 --- a/packages/components/src/utils/__tests__/getParsedData.test.ts +++ b/packages/components/src/utils/__tests__/getParsedData.test.ts @@ -33,9 +33,9 @@ describe("getParsedDate", () => { expect(result).toStrictEqual(new Date(2023, 5, 15)) }) - it("parses ISO 8601 format correctly", () => { - const result = getParsedDate("2023-07-20T14:30:45.123Z") - expect(result).toStrictEqual(new Date(2023, 6, 20, 14, 30, 45, 123)) + it("parses ISO 8601 format correctly in Singapore timezone", () => { + const result = getParsedDate("2023-07-20T16:00:00.000Z") + expect(result).toStrictEqual(new Date(2023, 6, 21, 0, 0, 0, 0)) }) it("returns current date for unsupported format", () => { diff --git a/packages/components/src/utils/getParsedDate.ts b/packages/components/src/utils/getParsedDate.ts index f7e916d9f7..f70a61b525 100644 --- a/packages/components/src/utils/getParsedDate.ts +++ b/packages/components/src/utils/getParsedDate.ts @@ -1,5 +1,6 @@ import { isMatch, parse } from "date-fns" +const TIMEZONE_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" const SUPPORTED_DATE_FORMATS = [ "dd/MM/yyyy", "d MMM yyyy", @@ -7,7 +8,7 @@ const SUPPORTED_DATE_FORMATS = [ "dd MMM yyyy", "dd MMMM yyyy", "yyyy-MM-dd", - "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", + TIMEZONE_DATE_FORMAT, ] export const getParsedDate = (dateString: string) => { @@ -20,7 +21,14 @@ export const getParsedDate = (dateString: string) => { try { if (isMatch(dateString, format)) { - return parse(dateString, format, new Date()) + let offsetDate = dateString + if (format === TIMEZONE_DATE_FORMAT) { + offsetDate = new Date( + // Add 8 hours to account for the Singapore timezone offset + new Date(dateString).getTime() + 8 * 60 * 60 * 1000, + ).toISOString() + } + return parse(offsetDate, format, new Date()) } } catch (e) { return new Date() From 3c4c605bb68401f7ccacd7cfbdcaaa4cb31f6d83 Mon Sep 17 00:00:00 2001 From: adriangohjw Date: Fri, 3 Jan 2025 14:10:11 +0800 Subject: [PATCH 2/3] use locale machine timezone offset --- packages/components/src/utils/getParsedDate.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/components/src/utils/getParsedDate.ts b/packages/components/src/utils/getParsedDate.ts index f70a61b525..6b25fb711d 100644 --- a/packages/components/src/utils/getParsedDate.ts +++ b/packages/components/src/utils/getParsedDate.ts @@ -23,9 +23,11 @@ export const getParsedDate = (dateString: string) => { if (isMatch(dateString, format)) { let offsetDate = dateString if (format === TIMEZONE_DATE_FORMAT) { + const localTimezoneOffsetInSeconds = + new Date().getTimezoneOffset() * 60 * 1000 + offsetDate = new Date( - // Add 8 hours to account for the Singapore timezone offset - new Date(dateString).getTime() + 8 * 60 * 60 * 1000, + new Date(dateString).getTime() - localTimezoneOffsetInSeconds, ).toISOString() } return parse(offsetDate, format, new Date()) From c7177d5621cfbfd826d1f71dad460d1cca087ab5 Mon Sep 17 00:00:00 2001 From: adriangohjw Date: Fri, 3 Jan 2025 14:58:39 +0800 Subject: [PATCH 3/3] use SG timezone for github actions --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 822768fca9..f28322d376 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -90,11 +90,15 @@ jobs: env: # Required to allow Datadog to trace Vitest tests NODE_OPTIONS: -r ${{ env.DD_TRACE_PACKAGE }} --import ${{ env.DD_TRACE_ESM_IMPORT }} + # Set timezone to Singapore to ensure that the tests are run in the correct timezone + TZ: Asia/Singapore - name: Test Components run: turbo test-ci:unit --filter=@opengovsg/isomer-components --env-mode=loose env: # Required to allow Datadog to trace Vitest tests NODE_OPTIONS: -r ${{ env.DD_TRACE_PACKAGE }} --import ${{ env.DD_TRACE_ESM_IMPORT }} + # Set timezone to Singapore to ensure that the tests are run in the correct timezone + TZ: Asia/Singapore end-to-end-tests: name: End-to-end tests