Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhiXin18 committed Nov 2, 2024
2 parents 5b79503 + 2c593c9 commit 1424abd
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 493 deletions.
3 changes: 2 additions & 1 deletion backend/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

import { describe, expect, test } from "@jest/globals";
import { formatSGT } from "./date";
import { TZDate } from "@date-fns/tz";

describe("CongestionSvc", () => {
test("formatSGT()", async () => {
// month is zero indexed: 3 -> April
expect(formatSGT(new Date("2024-04-01T00:00:00.000+0000"))).toStrictEqual(
expect(formatSGT(new TZDate("2024-04-01T00:00:00.000+0000"))).toStrictEqual(
"2024-04-01T08:00:00.000+0800",
);
});
Expand Down
37 changes: 31 additions & 6 deletions backend/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/

import { TZDate } from "@date-fns/tz";
import { formatDate } from "date-fns";
import { eachDayOfInterval, eachHourOfInterval, formatDate } from "date-fns";

export type DateInterval = "day" | "hour";

/**
* Formats a given date to Singapore Standard Time (SGT) in ISO 8601 format.
Expand All @@ -14,18 +16,41 @@ import { formatDate } from "date-fns";
* then adjusts it to Singapore Standard Time (SGT) and formats the date
* to the iso 8601 format with millisecond precision.
*
* @param {Date} date - The date to format in SGT.
* @returns {string} The formatted date string in SGT, ISO 8601 format.
* @param date - The date to format in SGT.
* @returns The formatted date string in SGT, ISO 8601 format.
*
* @example
* const date = new Date('2023-10-26T12:00:00Z');
* console.log(formatSGT(date));
* // Output: "2023-10-26T20:00:00.000000+08:00" (Example output in SGT)
*/
export function formatSGT(date: Date): string {
const tzDate = new TZDate(date, "UTC");
export function formatSGT(date: TZDate): string {
return formatDate(
tzDate.withTimeZone("Asia/Singapore"),
date.withTimeZone("Asia/Singapore"),
"yyyy-MM-dd'T'HH:mm:ss.SSSXX",
);
}

/**
* Generates a range of dates based on a specified interval.
*
* @param begin - The start date of the range, inclusive.
* @param end - The end date of the range, exclusive.
* @param interval - The interval for generating dates. Options are:
* - "day": Generates an array of dates at daily intervals.
* - "hour": Generates an array of dates at hourly intervals.
*
* @returns An array of dates within the specified range, depending on the chosen interval.
*/
export function dateRange(begin: TZDate, end: TZDate, interval: DateInterval) {
const timespan = {
start: begin,
end: end,
};
if (interval === "day") {
return eachDayOfInterval(timespan);
}
if (interval == "hour") {
return eachHourOfInterval(timespan);
}
}
3 changes: 3 additions & 0 deletions backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ if (process.argv.length < 4) {
const apiYaml = process.argv[2];
const port = parseInt(process.argv[3]);

// fix timezone
process.env.TZ = "Asia/Singapore";

// setup services
const db = initDB();
const congestion = new CongestionSvc(db);
Expand Down
Loading

0 comments on commit 1424abd

Please sign in to comment.