@@ -8,4 +8,32 @@ const unixTime = () => `,"time":${Math.round(Date.now() / 1000.0)}`
88
99const isoTime = ( ) => `,"time":"${ new Date ( Date . now ( ) ) . toISOString ( ) } "` // using Date.now() for testability
1010
11- module . exports = { nullTime, epochTime, unixTime, isoTime }
11+ const NS_PER_MS = 1_000_000n
12+ const NS_PER_SEC = 1_000_000_000n
13+
14+ const startWallTimeNs = BigInt ( Date . now ( ) ) * NS_PER_MS
15+ const startHrTime = process . hrtime . bigint ( )
16+
17+ const isoTimeNano = ( ) => {
18+ const elapsedNs = process . hrtime . bigint ( ) - startHrTime
19+ const currentTimeNs = startWallTimeNs + elapsedNs
20+
21+ const secondsSinceEpoch = currentTimeNs / NS_PER_SEC
22+ const nanosWithinSecond = currentTimeNs % NS_PER_SEC
23+
24+ const msSinceEpoch = Number ( secondsSinceEpoch * 1000n + nanosWithinSecond / 1_000_000n )
25+ const date = new Date ( msSinceEpoch )
26+
27+ const year = date . getUTCFullYear ( )
28+ const month = ( date . getUTCMonth ( ) + 1 ) . toString ( ) . padStart ( 2 , '0' )
29+ const day = date . getUTCDate ( ) . toString ( ) . padStart ( 2 , '0' )
30+ const hours = date . getUTCHours ( ) . toString ( ) . padStart ( 2 , '0' )
31+ const minutes = date . getUTCMinutes ( ) . toString ( ) . padStart ( 2 , '0' )
32+ const seconds = date . getUTCSeconds ( ) . toString ( ) . padStart ( 2 , '0' )
33+
34+ return `,"time":"${ year } -${ month } -${ day } T${ hours } :${ minutes } :${ seconds } .${ nanosWithinSecond
35+ . toString ( )
36+ . padStart ( 9 , '0' ) } Z"`
37+ }
38+
39+ module . exports = { nullTime, epochTime, unixTime, isoTime, isoTimeNano }
0 commit comments