Skip to content

Commit

Permalink
fix(ci-test): local timezone differences should not fail tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelydev committed Oct 16, 2024
1 parent 7cf6ae5 commit 9547c92
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/DateUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export function toLocaTzIsoStringWithOffset(date: Date): string {
* @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 {
export function convertTimezone(date: Date, timeZoneId: string, localeString: string = "en-GB"): Date {
return new Date(
(typeof date === "string" ? new Date(date) : date)
.toLocaleString("en-GB", {timeZone: timeZoneId}));
.toLocaleString(localeString, {timeZone: timeZoneId}));
}
14 changes: 10 additions & 4 deletions test/DateUtilities.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { toLocaTzIsoStringWithOffset } from '../src/DateUtilities';
import { convertTimezone, 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';
const localTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const localTzDate = convertTimezone(date, localTimezone, "en-US");
const expectedLocalISOTime = toLocaTzIsoStringWithOffset(localTzDate);
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';
const localTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const localTzDate = convertTimezone(date, localTimezone, "en-US");
const expectedLocalISOTime = toLocaTzIsoStringWithOffset(localTzDate);
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';
const localTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const localTzDate = convertTimezone(date, localTimezone, "en-US");
const expectedLocalISOTime = toLocaTzIsoStringWithOffset(localTzDate);
expect(result).toBe(expectedLocalISOTime);
});
});

0 comments on commit 9547c92

Please sign in to comment.