Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide ISO8601 time format #757

Merged
merged 5 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
})