mirror of
https://github.com/pinojs/pino.git
synced 2025-12-08 20:36:13 +00:00
* feat: added timestamp rfc3339 format with nanoseconds * chore: updated docs and tests * Update lib/time.js Co-authored-by: Andrea Carraro <toomuchdesign@users.noreply.github.com> * feat: updated and improved fn performance, fixed types * chore: updated docs and tests * chore: fixed leftover test comment --------- Co-authored-by: Matteo Collina <matteo.collina@gmail.com> Co-authored-by: Andrea Carraro <toomuchdesign@users.noreply.github.com>
40 lines
1.4 KiB
JavaScript
40 lines
1.4 KiB
JavaScript
'use strict'
|
|
|
|
const nullTime = () => ''
|
|
|
|
const epochTime = () => `,"time":${Date.now()}`
|
|
|
|
const unixTime = () => `,"time":${Math.round(Date.now() / 1000.0)}`
|
|
|
|
const isoTime = () => `,"time":"${new Date(Date.now()).toISOString()}"` // using Date.now() for testability
|
|
|
|
const NS_PER_MS = 1_000_000n
|
|
const NS_PER_SEC = 1_000_000_000n
|
|
|
|
const startWallTimeNs = BigInt(Date.now()) * NS_PER_MS
|
|
const startHrTime = process.hrtime.bigint()
|
|
|
|
const isoTimeNano = () => {
|
|
const elapsedNs = process.hrtime.bigint() - startHrTime
|
|
const currentTimeNs = startWallTimeNs + elapsedNs
|
|
|
|
const secondsSinceEpoch = currentTimeNs / NS_PER_SEC
|
|
const nanosWithinSecond = currentTimeNs % NS_PER_SEC
|
|
|
|
const msSinceEpoch = Number(secondsSinceEpoch * 1000n + nanosWithinSecond / 1_000_000n)
|
|
const date = new Date(msSinceEpoch)
|
|
|
|
const year = date.getUTCFullYear()
|
|
const month = (date.getUTCMonth() + 1).toString().padStart(2, '0')
|
|
const day = date.getUTCDate().toString().padStart(2, '0')
|
|
const hours = date.getUTCHours().toString().padStart(2, '0')
|
|
const minutes = date.getUTCMinutes().toString().padStart(2, '0')
|
|
const seconds = date.getUTCSeconds().toString().padStart(2, '0')
|
|
|
|
return `,"time":"${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${nanosWithinSecond
|
|
.toString()
|
|
.padStart(9, '0')}Z"`
|
|
}
|
|
|
|
module.exports = { nullTime, epochTime, unixTime, isoTime, isoTimeNano }
|