-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analytics-utilities): Add date formatting support [MA-1969] (#785)
- Refactor tests to allow using `runUtcTest` in more files. - Add analytics-specific date format to analytics-utilities.
- Loading branch information
Showing
5 changed files
with
36 additions
and
4 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
packages/analytics/analytics-utilities/src/format.spec.tz.ts
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,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) | ||
}) | ||
}) |
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,5 @@ | ||
import { format } from 'date-fns' | ||
|
||
export function formatISOTimeWithTZ(ts: number | Date) { | ||
return format(ts, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX") | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './types' | ||
export * from './format' | ||
export * from './granularity' | ||
export * from './queryTime' | ||
export * from './timeframes' |
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,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 |