File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { describe , expect , it } from "vitest" ;
2+ import { normalizeTimestamp } from "./date-time.js" ;
3+
4+ describe ( "normalizeTimestamp" , ( ) => {
5+ it ( "normalizes numeric second and millisecond timestamps" , ( ) => {
6+ expect ( normalizeTimestamp ( "1700000000" ) ) . toEqual ( {
7+ timestampMs : 1_700_000_000_000 ,
8+ timestampUtc : "2023-11-14T22:13:20.000Z" ,
9+ } ) ;
10+ expect ( normalizeTimestamp ( "1700000000000" ) ) . toEqual ( {
11+ timestampMs : 1_700_000_000_000 ,
12+ timestampUtc : "2023-11-14T22:13:20.000Z" ,
13+ } ) ;
14+ } ) ;
15+
16+ it ( "ignores unsafe or out-of-range numeric timestamp strings" , ( ) => {
17+ expect ( normalizeTimestamp ( "9007199254740993" ) ) . toBeUndefined ( ) ;
18+ expect ( normalizeTimestamp ( "999999999999999999999999" ) ) . toBeUndefined ( ) ;
19+ } ) ;
20+ } ) ;
Original file line number Diff line number Diff line change @@ -5,6 +5,16 @@ export type ResolvedTimeFormat = "12" | "24";
55
66let cachedTimeFormat : ResolvedTimeFormat | undefined ;
77
8+ function buildNormalizedTimestamp (
9+ timestampMs : number ,
10+ ) : { timestampMs : number ; timestampUtc : string } | undefined {
11+ if ( ! Number . isSafeInteger ( timestampMs ) ) {
12+ return undefined ;
13+ }
14+ const timestampUtc = new Date ( timestampMs ) . toISOString ( ) ;
15+ return { timestampMs, timestampUtc } ;
16+ }
17+
818export function resolveUserTimezone ( configured ?: string ) : string {
919 const trimmed = configured ?. trim ( ) ;
1020 if ( trimmed ) {
@@ -69,7 +79,11 @@ export function normalizeTimestamp(
6979 if ( timestampMs === undefined || ! Number . isFinite ( timestampMs ) ) {
7080 return undefined ;
7181 }
72- return { timestampMs, timestampUtc : new Date ( timestampMs ) . toISOString ( ) } ;
82+ try {
83+ return buildNormalizedTimestamp ( timestampMs ) ;
84+ } catch {
85+ return undefined ;
86+ }
7387}
7488
7589export function withNormalizedTimestamp < T extends Record < string , unknown > > (
You can’t perform that action at this time.
0 commit comments