Skip to content

fix: pad short ISO fractional seconds when parsing milliseconds - #2367

Open
draarivpatel-ui wants to merge 1 commit into
jquense:masterfrom
draarivpatel-ui:fix/iso-subsecond-fraction
Open

draarivpatel-ui wants to merge 1 commit into
jquense:masterfrom
draarivpatel-ui:fix/iso-subsecond-fraction

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Summary

date().cast() and date().validate() mis-parse ISO datetimes whose fractional-second component has fewer than three digits. The fraction is truncated to three characters but never right-padded, so the digits are read as a literal integer instead of a scaled fraction:

input current native Date
.1 1 ms 100 ms
.05 5 ms 50 ms
.12 12 ms 120 ms
.5 5 ms 500 ms
.123 123 ms 123 ms

The error is silent and can be up to 495 ms.

Fix

In src/util/parseIsoDate.ts, right-pad the captured fraction to three digits before converting it to a number:

-        toNumber(regexResult[7].substring(0, 3))
+        toNumber(regexResult[7].substring(0, 3).padEnd(3, "0"))

Fractions longer than three digits keep the existing truncation behaviour, and precision is untouched.

Tests

Added cases for .5, .05, .12 (local) and .5Z (UTC) in test/util/parseIsoDate.ts, each compared against the equivalent new Date(...) construction.

Full suite: 875 passed, 2 skipped. ESLint clean on both changed files.

Fixes #2339.

Fractional-second components with fewer than three digits were truncated
rather than scaled, so ".5" parsed as 5ms instead of 500ms and ".05" as
5ms instead of 50ms. Pad the fraction to three digits on the right before
converting to a number, matching the native ISO 8601 parser.
Copilot AI balanced review requested due to automatic review settings November 25, 2026 23:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

date() cast mis-parses sub-3-digit fractional seconds (.5Z -> 5ms instead of 500ms)

2 participants