Skip to content

Commit

Permalink
provide ISO8601 time format (#757)
Browse files Browse the repository at this point in the history
* provide ISO8601 time format

* add tests

* bump minor version

* add docs

* revert minor version bump
  • Loading branch information
matthewadams authored and mcollina committed Jan 7, 2020
1 parent d54a017 commit 7538351
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/api.md
Expand Up @@ -807,6 +807,7 @@ The `pino.stdTimeFunctions` object provides a very small set of common functions
* `pino.stdTimeFunctions.epochTime`: Milliseconds since Unix epoch (Default)
* `pino.stdTimeFunctions.unixTime`: Seconds since Unix epoch
* `pino.stdTimeFunctions.nullTime`: Clears timestamp property (Used when `timestamp: false`)
* `pino.stdTimeFunctions.isoTime`: ISO 8601-formatted time in UTC

* See [`timestamp` option](#opt-timestamp)

Expand Down
4 changes: 3 additions & 1 deletion lib/time.js
Expand Up @@ -6,4 +6,6 @@ const epochTime = () => `,"time":${Date.now()}`

const unixTime = () => `,"time":${Math.round(Date.now() / 1000.0)}`

module.exports = { nullTime, epochTime, unixTime }
const isoTime = () => `,"time":"${new Date(Date.now()).toISOString()}"` // using Date.now() for testability

module.exports = { nullTime, epochTime, unixTime, isoTime }
18 changes: 18 additions & 0 deletions test/timestamp.test.js
Expand Up @@ -11,6 +11,7 @@ test('pino exposes standard time functions', async ({ ok }) => {
ok(pino.stdTimeFunctions.epochTime)
ok(pino.stdTimeFunctions.unixTime)
ok(pino.stdTimeFunctions.nullTime)
ok(pino.stdTimeFunctions.isoTime)
})

test('pino accepts external time functions', async ({ is }) => {
Expand Down Expand Up @@ -101,3 +102,20 @@ test('pino.stdTimeFunctions.unixTime returns seconds based timestamps', async ({
is(result.time, 1531069920)
Date.now = now
})

test('pino.stdTimeFunctions.isoTime returns ISO 8601 timestamps', async ({ is }) => {
const opts = {
timestamp: pino.stdTimeFunctions.isoTime
}
const stream = sink()
const instance = pino(opts, stream)
const ms = 1531069919686
const now = Date.now
Date.now = () => ms
const iso = new Date(ms).toISOString()
instance.info('foobar')
const result = await once(stream, 'data')
is(result.hasOwnProperty('time'), true)
is(result.time, iso)
Date.now = now
})

0 comments on commit 7538351

Please sign in to comment.