Skip to content

Commit

Permalink
feat(analytics-utilities): Add date formatting support [MA-1969] (#785)
Browse files Browse the repository at this point in the history
- Refactor tests to allow using `runUtcTest` in more files.
- Add analytics-specific date format to analytics-utilities.
  • Loading branch information
adorack authored Sep 14, 2023
1 parent 7f0ab6e commit 31578b4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
21 changes: 21 additions & 0 deletions packages/analytics/analytics-utilities/src/format.spec.tz.ts
Original file line number Diff line number Diff line change
@@ -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.getTime())).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.getTime())).toBe(str)
})
})
5 changes: 5 additions & 0 deletions packages/analytics/analytics-utilities/src/format.ts
Original file line number Diff line number Diff line change
@@ -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")
}
1 change: 1 addition & 0 deletions packages/analytics/analytics-utilities/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './types'
export * from './format'
export * from './granularity'
export * from './queryTime'
export * from './timeframes'
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions packages/analytics/analytics-utilities/src/specUtils.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 31578b4

Please sign in to comment.