-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(refactor): extract date utility functions
- cleanup utility functions in CalOohPay.ts
- Loading branch information
Showing
10 changed files
with
166 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Converts a given date to a local ISO string with timezone offset. | ||
* | ||
* This function takes a `Date` object and returns a string in the ISO 8601 format | ||
* with the local timezone offset included. The resulting string will be in the format | ||
* `YYYY-MM-DDTHH:mm:ss±HH:00`. | ||
* | ||
* @param date - The `Date` object to be converted. | ||
* @returns A string representing the local ISO time with timezone offset. | ||
*/ | ||
export function toLocaTzIsoStringWithOffset(date: Date): string { | ||
var timezoneOffsetInMilliseconds = date.getTimezoneOffset() * 60000; | ||
var localISOTime = (new Date(date.getTime() - timezoneOffsetInMilliseconds)).toISOString().slice(0, -5); | ||
let timezoneOffsetInHours = -(timezoneOffsetInMilliseconds / 3600000); | ||
let localISOTimeWithOffset = localISOTime + | ||
(timezoneOffsetInHours >= 0 ? '+' : '-') + | ||
(Math.abs(timezoneOffsetInHours) < 10 ? '0' : '') + | ||
timezoneOffsetInHours + ':00'; | ||
return localISOTimeWithOffset; | ||
} | ||
|
||
/** | ||
* Converts a given date to a specified timezone. | ||
* | ||
* @param date - The date to be converted. Can be a Date object or a string representing a date. | ||
* @param timeZoneId - The IANA timezone identifier (e.g., "America/New_York", "Europe/London"). | ||
* @returns A new Date object representing the same moment in time in the specified timezone. | ||
*/ | ||
export function convertTimezone(date: Date, timeZoneId: string): Date { | ||
return new Date( | ||
(typeof date === "string" ? new Date(date) : date) | ||
.toLocaleString("en-GB", {timeZone: timeZoneId})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
export interface Environment { | ||
API_TOKEN: string; | ||
} | ||
|
||
export function sanitiseEnvVariable(envVars: NodeJS.ProcessEnv): Environment { | ||
if (!envVars.API_TOKEN) { | ||
throw new Error("API_TOKEN not defined"); | ||
} | ||
return { | ||
API_TOKEN: envVars.API_TOKEN, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { FinalSchedule } from "./FinalSchedule.js"; | ||
|
||
export interface PagerdutySchedule { | ||
name: string; | ||
html_url: string; | ||
final_schedule: FinalSchedule; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { toLocaTzIsoStringWithOffset } from '../src/DateUtilities'; | ||
import {describe, expect, test} from '@jest/globals'; | ||
|
||
describe('DateUtilities.toLocaTzIsoStringWithOffset', () => { | ||
test('should convert UTC date to local ISO string with timezone offset', () => { | ||
const date = new Date('2023-10-01T12:00:00Z'); | ||
const result = toLocaTzIsoStringWithOffset(date); | ||
const expectedLocalISOTime = '2023-10-01T13:00:00+01:00'; | ||
expect(result).toBe(expectedLocalISOTime); | ||
}); | ||
|
||
test('should handle dates with positive timezone offsets', () => { | ||
const date = new Date('2023-10-01T12:00:00+02:00'); | ||
const result = toLocaTzIsoStringWithOffset(date); | ||
const expectedLocalISOTime = '2023-10-01T11:00:00+01:00'; | ||
expect(result).toBe(expectedLocalISOTime); | ||
}); | ||
|
||
test('should handle dates with negative timezone offsets', () => { | ||
const date = new Date('2023-10-01T12:00:00-05:00'); | ||
const result = toLocaTzIsoStringWithOffset(date); | ||
const expectedLocalISOTime = '2023-10-01T18:00:00+01:00'; | ||
expect(result).toBe(expectedLocalISOTime); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {describe, expect, test} from '@jest/globals'; | ||
import { OnCallPeriod } from '../src/OnCallPeriod'; | ||
|
||
describe('should initialise OnCallPeriod with the right number of weekdays and weekends', () => { | ||
test('- when the shift starts 1st of month and is until the 12th of the month', () => { | ||
const onCallPeriod = new OnCallPeriod(new Date('2024-08-01T00:00:00+01:00'), new Date('2024-08-12T10:00:00+01:00')); | ||
expect(onCallPeriod.since).toStrictEqual(new Date('2024-08-01T00:00:00+01:00')); | ||
expect(onCallPeriod.until).toStrictEqual(new Date('2024-08-12T10:00:00+01:00')); | ||
}); | ||
|
||
test('- when the shift starts and ends on the same day, just 2 hours in the evening', () => { | ||
const onCallPeriod = new OnCallPeriod( | ||
new Date('2024-09-20T16:30:00+01:00'), | ||
new Date('2024-09-20T18:30:00+01:00'), | ||
); | ||
expect(onCallPeriod.since).toStrictEqual(new Date('2024-09-20T16:30:00+01:00')); | ||
expect(onCallPeriod.until).toStrictEqual(new Date('2024-09-20T18:30:00+01:00')); | ||
expect(onCallPeriod.numberOfOOhWeekDays).toBe(0); | ||
expect(onCallPeriod.numberOfOohWeekends).toBe(0); | ||
}); | ||
|
||
test('- when the shift starts on Friday 8pm and extends until Monday morning, numberOfOohWeekends must be 3', () => { | ||
const since = new Date('2024-09-20T20:00:00+01:00'); | ||
const until = new Date('2024-09-23T10:00:00+01:00'); | ||
const onCallPeriod = new OnCallPeriod( | ||
since, | ||
until, | ||
); | ||
expect(onCallPeriod.since).toStrictEqual(since); | ||
expect(onCallPeriod.until).toStrictEqual(until); | ||
expect(onCallPeriod.numberOfOOhWeekDays).toBe(0); | ||
expect(onCallPeriod.numberOfOohWeekends).toBe(3); | ||
}); | ||
|
||
test('- when the shift starts on 2024-08-28T10:00:00+01:00 and extends until 2024-09-02T10:00:00+01:00', () => { | ||
const since = new Date('2024-08-28T10:00:00+01:00'); | ||
const until = new Date('2024-09-02T10:00:00+01:00'); | ||
const onCallPeriod = new OnCallPeriod( | ||
since, | ||
until, | ||
); | ||
expect(onCallPeriod.since).toStrictEqual(since); | ||
expect(onCallPeriod.until).toStrictEqual(until); | ||
expect(onCallPeriod.numberOfOOhWeekDays).toBe(2); | ||
expect(onCallPeriod.numberOfOohWeekends).toBe(3); | ||
}); | ||
}) |