Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix getParsedDate - add 8h to account for timezone #963

Merged
merged 5 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
14 changes: 12 additions & 2 deletions packages/components/src/utils/getParsedDate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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",
"d MMMM yyyy",
"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) => {
Expand All @@ -20,7 +21,16 @@ export const getParsedDate = (dateString: string) => {

try {
if (isMatch(dateString, format)) {
return parse(dateString, format, new Date())
let offsetDate = dateString
if (format === TIMEZONE_DATE_FORMAT) {
const localTimezoneOffsetInSeconds =
new Date().getTimezoneOffset() * 60 * 1000

offsetDate = new Date(
new Date(dateString).getTime() - localTimezoneOffsetInSeconds,
).toISOString()
}
return parse(offsetDate, format, new Date())
}
} catch (e) {
return new Date()
Expand Down
Loading