From 8da16d1b14fdf827502e3e32fcca2b5b1cfcf3f4 Mon Sep 17 00:00:00 2001 From: Rocky Fischer Date: Wed, 13 Sep 2023 13:59:18 -0400 Subject: [PATCH 1/2] feat(analytics-utilities): Add date formatting support [MA-1969] - Refactor tests to allow using `runUtcTest` in more files. - Add analytics-specific date format to analytics-utilities. --- .../analytics-utilities/src/format.spec.tz.ts | 21 +++++++++++++++++++ .../analytics-utilities/src/format.ts | 5 +++++ .../analytics-utilities/src/index.ts | 1 + .../src/queryTime.spec.tz.ts | 5 +---- .../analytics-utilities/src/specUtils.ts | 8 +++++++ 5 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 packages/analytics/analytics-utilities/src/format.spec.tz.ts create mode 100644 packages/analytics/analytics-utilities/src/format.ts create mode 100644 packages/analytics/analytics-utilities/src/specUtils.ts diff --git a/packages/analytics/analytics-utilities/src/format.spec.tz.ts b/packages/analytics/analytics-utilities/src/format.spec.tz.ts new file mode 100644 index 0000000000..45ec6abe89 --- /dev/null +++ b/packages/analytics/analytics-utilities/src/format.spec.tz.ts @@ -0,0 +1,21 @@ +import { it, expect } from 'vitest' +import { formatISOTimeWithTZ } from './format' +import { runNonUtcTest, runUtcTest } from './specUtils' + +runNonUtcTest('date formatting, non-UTC', () => { + it('formatISOTimeWithTZ should work', () => { + const pattern = /20\d{2}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d[+-]\d\d?:\d\d/ + const date = new Date('2021-01-01T06:02:03.000Z') + expect(formatISOTimeWithTZ(date)).toMatch(pattern) + expect(formatISOTimeWithTZ(date)).toMatch(pattern) + }) +}) + +runUtcTest('date formatting in UTC', () => { + it('formatISOTimeWithTZ should work', () => { + const str = '2020-01-01T01:02:03.000Z' + const date = new Date(str) + expect(formatISOTimeWithTZ(date)).toBe(str) + expect(formatISOTimeWithTZ(date)).toBe(str) + }) +}) diff --git a/packages/analytics/analytics-utilities/src/format.ts b/packages/analytics/analytics-utilities/src/format.ts new file mode 100644 index 0000000000..7a3ae6f049 --- /dev/null +++ b/packages/analytics/analytics-utilities/src/format.ts @@ -0,0 +1,5 @@ +import { format } from 'date-fns' + +export function formatISOTimeWithTZ(ts: number | Date) { + return format(ts, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX") +} diff --git a/packages/analytics/analytics-utilities/src/index.ts b/packages/analytics/analytics-utilities/src/index.ts index 66ede8373e..ea33e89ddf 100644 --- a/packages/analytics/analytics-utilities/src/index.ts +++ b/packages/analytics/analytics-utilities/src/index.ts @@ -1,4 +1,5 @@ export * from './types' +export * from './format' export * from './granularity' export * from './queryTime' export * from './timeframes' diff --git a/packages/analytics/analytics-utilities/src/queryTime.spec.tz.ts b/packages/analytics/analytics-utilities/src/queryTime.spec.tz.ts index 80ccb7d83a..61ffeb300c 100644 --- a/packages/analytics/analytics-utilities/src/queryTime.spec.tz.ts +++ b/packages/analytics/analytics-utilities/src/queryTime.spec.tz.ts @@ -6,6 +6,7 @@ import { DeltaQueryTime, TimeseriesQueryTime, UnaryQueryTime } from './queryTime import type { Timeframe } from './timeframes' import { datePickerSelectionToTimeframe, TimePeriods } from './timeframes' import { formatInTimeZone } from 'date-fns-tz' +import { runUtcTest } from './specUtils' const standardizeTimezone = (d: Date) => { // Adjust according to the test runner's timezone for consistent results. @@ -497,10 +498,6 @@ runDstTest('daylight savings time: fall', () => { }) }) -const supportsUtc = Intl.DateTimeFormat().resolvedOptions().timeZone === 'UTC' - -const runUtcTest = supportsUtc ? describe : describe.skip - runUtcTest('UTC: timezone handling', () => { beforeEach(() => { vi.useFakeTimers() diff --git a/packages/analytics/analytics-utilities/src/specUtils.ts b/packages/analytics/analytics-utilities/src/specUtils.ts new file mode 100644 index 0000000000..7260fdad06 --- /dev/null +++ b/packages/analytics/analytics-utilities/src/specUtils.ts @@ -0,0 +1,8 @@ +import { describe } from 'vitest' + +const supportsUtc = Intl.DateTimeFormat().resolvedOptions().timeZone === 'UTC' + +// Squash type errors; we don't actually export this file from the package, so it's fine that the type depends on Vitest. +export const runUtcTest: any = supportsUtc ? describe : describe.skip + +export const runNonUtcTest: any = supportsUtc ? describe.skip : describe From 01b4b51bf99fdfaddcbf8345513a816bf3b8a0f0 Mon Sep 17 00:00:00 2001 From: Rocky Fischer Date: Wed, 13 Sep 2023 15:55:50 -0400 Subject: [PATCH 2/2] Fix spec typo --- packages/analytics/analytics-utilities/src/format.spec.tz.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/analytics/analytics-utilities/src/format.spec.tz.ts b/packages/analytics/analytics-utilities/src/format.spec.tz.ts index 45ec6abe89..b59b77b340 100644 --- a/packages/analytics/analytics-utilities/src/format.spec.tz.ts +++ b/packages/analytics/analytics-utilities/src/format.spec.tz.ts @@ -7,7 +7,7 @@ runNonUtcTest('date formatting, non-UTC', () => { const pattern = /20\d{2}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d[+-]\d\d?:\d\d/ const date = new Date('2021-01-01T06:02:03.000Z') expect(formatISOTimeWithTZ(date)).toMatch(pattern) - expect(formatISOTimeWithTZ(date)).toMatch(pattern) + expect(formatISOTimeWithTZ(date.getTime())).toMatch(pattern) }) }) @@ -16,6 +16,6 @@ runUtcTest('date formatting in UTC', () => { const str = '2020-01-01T01:02:03.000Z' const date = new Date(str) expect(formatISOTimeWithTZ(date)).toBe(str) - expect(formatISOTimeWithTZ(date)).toBe(str) + expect(formatISOTimeWithTZ(date.getTime())).toBe(str) }) })