Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 63 additions & 10 deletions lib/utils/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,61 @@ const {
DATE_FORMAT_SIMPLE
} = require('../constants')

const dateformat = require('@pinojs/dateformat')
const { dateformat, DateFormatter } = require('@pinojs/dateformat')
const createDate = require('./create-date')
const isValidDate = require('./is-valid-date')

const simpleDateFormatter = new DateFormatter(DATE_FORMAT_SIMPLE)
const standardDateFormatter = new DateFormatter(DATE_FORMAT)

/**
* Checks if the given format string is a UTC format.
*
* @param {string} mask The format string to check.
* @returns {mask is `UTC:${string}`} `true` if the format string is a UTC format, otherwise `false`.
*/
function isUTC (mask) {
return (
(mask.charCodeAt(0) | 0x20 === 0x75) && // 'u'
(mask.charCodeAt(1) | 0x20 === 0x74) && // 't'
(mask.charCodeAt(2) | 0x20 === 0x63) // 'c'
)
}

/**
* Checks if the given format string is a SYS format.
*
* @param {string} mask The format string to check.
* @returns {mask is `SYS:${string}`} `true` if the format string is a SYS format, otherwise `false`.
*/
function isSYS (mask) {
return (
(mask.charCodeAt(0) | 0x20 === 0x73) && // 's'
(mask.charCodeAt(1) | 0x20 === 0x79) && // 'y'
(mask.charCodeAt(2) | 0x20 === 0x73) // 's'
)
}

/**
* Checks if the given format string is SYS:STANDARD.
*
* @param {`SYS:${string}`} mask The format string to check.
* @returns {mask is 'SYS:STANDARD'} `true` if the format string is SYS:STANDARD, otherwise `false`.
*/
function isSysStandard (mask) {
return (
mask.length === 12 &&
(mask.charCodeAt(4) | 0x20 === 0x73) && // 's'
(mask.charCodeAt(5) | 0x20 === 0x74) && // 't'
(mask.charCodeAt(6) | 0x20 === 0x61) && // 'a'
(mask.charCodeAt(7) | 0x20 === 0x6e) && // 'n'
(mask.charCodeAt(8) | 0x20 === 0x64) && // 'd'
(mask.charCodeAt(9) | 0x20 === 0x61) && // 'a'
(mask.charCodeAt(10) | 0x20 === 0x72) && // 'r'
(mask.charCodeAt(11) | 0x20 === 0x64) // 'd
)
}

/**
* Converts a given `epoch` to a desired display format.
*
Expand Down Expand Up @@ -46,21 +97,23 @@ function formatTime (epoch, translateTime = false) {
}

if (translateTime === true) {
return dateformat(instant, DATE_FORMAT_SIMPLE)
return simpleDateFormatter.format(instant)
}

const upperFormat = translateTime.toUpperCase()
if (upperFormat === 'SYS:STANDARD') {
return dateformat(instant, DATE_FORMAT)
if (translateTime[3] !== ':') {
return dateformat(instant, translateTime)
}

const prefix = upperFormat.substr(0, 4)
if (prefix === 'SYS:' || prefix === 'UTC:') {
if (prefix === 'UTC:') {
return dateformat(instant, translateTime)
if (isSYS(translateTime)) {
if (isSysStandard(translateTime)) {
return standardDateFormatter.format(instant)
}
return dateformat(instant, translateTime.slice(4))
}

return dateformat(instant, `UTC:${translateTime}`)
if (isUTC(translateTime)) {
return dateformat(instant, translateTime, true)
}

return dateformat(instant, translateTime)
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"test"
],
"dependencies": {
"@pinojs/dateformat": "^1.0.0-rc.1",
"@pinojs/dateformat": "^1.0.0-rc.2",
"colorette": "^2.0.7",
"fast-copy": "^3.0.2",
"fast-safe-stringify": "^2.1.1",
Expand Down
2 changes: 1 addition & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const os = require('node:os')
const { describe, test, beforeEach, afterEach } = require('node:test')
const match = require('@jsumners/assert-match')
const pino = require('pino')
const dateformat = require('@pinojs/dateformat')
const { dateformat } = require('@pinojs/dateformat')
const rimraf = require('rimraf')
const { join } = require('node:path')
const fs = require('node:fs')
Expand Down
Loading