Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(analytics-utilities): Add date formatting support [MA-1969] #785

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)).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)
hfukada marked this conversation as resolved.
Show resolved Hide resolved
})
})
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
Loading