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

Handle timestamp 0 in prettifyTime #76

Merged
merged 1 commit into from Aug 15, 2019
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
15 changes: 12 additions & 3 deletions lib/utils.js
Expand Up @@ -326,9 +326,18 @@ function prettifyObject ({
* string.
*/
function prettifyTime ({ log, timestampKey = TIMESTAMP_KEY, translateFormat = undefined }) {
if (timestampKey in log === false && 'timestamp' in log === false) return undefined
let time = null

if (timestampKey in log) {
time = log[timestampKey]
} else if ('timestamp' in log) {
time = log.timestamp
}

if (time === null) return undefined
if (translateFormat) {
return '[' + formatTime(log[timestampKey] || log.timestamp, translateFormat) + ']'
return '[' + formatTime(time, translateFormat) + ']'
}
return `[${log[timestampKey] || log.timestamp}]`

return `[${time}]`
}
10 changes: 10 additions & 0 deletions test/lib/utils.public.test.js
Expand Up @@ -252,5 +252,15 @@ tap.test('prettifyTime', t => {
t.is(str, '[2019-04-07T09:15:00.000-04:00]')
})

t.test('handles the 0 timestamp', async t => {
let log = { time: 0 }
let str = prettifyTime({ log })
t.is(str, '[0]')

log = { timestamp: 0 }
str = prettifyTime({ log })
t.is(str, '[0]')
})

t.end()
})