mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 14:00:39 -07:00
fix(date): zero-pad milliseconds when formatting %N fractional seconds (#929)
%N renders the fractional part of the second. The milliseconds returned by
getMilliseconds() are the three most significant digits of that fraction and
must be zero-padded to three digits before use, otherwise sub-100ms values
lose their leading zeros:
50ms => strftime("%N") returned "500000000", expected "050000000"
5ms => strftime("%3N") returned "500", expected "005"
Pad the milliseconds to three digits before slicing to the requested width.
This commit is contained in:
@@ -98,7 +98,7 @@ const formatCodes: Record<string, FormatCodeHandler> = {
|
||||
M: (d: LiquidDate) => d.getMinutes(),
|
||||
N: (d: LiquidDate, opts: FormatOptions) => {
|
||||
const width = Number(opts.width) || 9
|
||||
const str = String(d.getMilliseconds()).slice(0, width)
|
||||
const str = padStart(String(d.getMilliseconds()), 3, '0').slice(0, width)
|
||||
opts.memoryLimit?.use(width - str.length)
|
||||
return padEnd(str, width, '0')
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user