Skip to content

Commit

Permalink
fix(date): account for atlantic/newfoundland timezones better
Browse files Browse the repository at this point in the history
  • Loading branch information
Forceh91 committed Dec 11, 2024
1 parent 42ae1b6 commit c0d8bae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/__tests__/ecccDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ describe("Helpers for ECCC dates", () => {
expect(ecccDateStringToTSDate(ecccDateString)).toStrictEqual(expectedDate);
});

it("parses to a newfoundland date object correctly", () => {
it("parses to custom timezone (ADT/AST/NDT/NST) objects correctly", () => {
const ecccDateString = "Sunday August 13, 2023 at 12:00";
expect(ecccDateStringToTSDate(`${ecccDateString} AST`)).toStrictEqual(new Date(2023, 7, 13, 11, 0, 0));
expect(ecccDateStringToTSDate(`${ecccDateString} ADT`)).toStrictEqual(new Date(2023, 7, 13, 11, 0, 0));
expect(ecccDateStringToTSDate(`${ecccDateString} AST`)).toStrictEqual(new Date(2023, 7, 13, 12, 0, 0));
expect(ecccDateStringToTSDate(`${ecccDateString} NDT`)).toStrictEqual(new Date(2023, 7, 13, 10, 30, 0));
expect(ecccDateStringToTSDate(`${ecccDateString} NST`)).toStrictEqual(new Date(2023, 7, 13, 11, 30, 0));
});

it("gets the correct months for the current season", () => {
Expand Down
9 changes: 7 additions & 2 deletions src/lib/date/ecccDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import { format } from "date-fns";
import { getIsWinterSeason } from "./season";

export function ecccDateStringToTSDate(date: string) {
// JS doesn't see ATS/NDT (newfoundland time) as a valid date for some reason
const fixedTimezone = date.replace("AST", "GMT-0300").replace("NDT", "GMT-0230").replace("NST", "GMT-0330");
// JS doesn't see atlantic and newfoundland standard/daylight time as a valid time for some reason
const fixedTimezone = date
.replace("ADT", "GMT-0300")
.replace("AST", "GMT-0400")
.replace("NDT", "GMT-0230")
.replace("NST", "GMT-0330");

return new Date(fixedTimezone.replace(" at", "").replace(",", ""));
}

Expand Down

0 comments on commit c0d8bae

Please sign in to comment.