diff --git a/lib/utils/format-time.js b/lib/utils/format-time.js index ef87bd93..5b2a1786 100644 --- a/lib/utils/format-time.js +++ b/lib/utils/format-time.js @@ -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. * @@ -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) } diff --git a/package.json b/package.json index 2ed74240..561dc623 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/basic.test.js b/test/basic.test.js index 06a74b64..aaf573a4 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -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')