Skip to content

fix: parse fractional seconds shorter than 3 digits as a decimal fraction - #3167

Open
lenamonj wants to merge 1 commit into
iamkun:devfrom
lenamonj:fix/fractional-seconds
Open

fix: parse fractional seconds shorter than 3 digits as a decimal fraction#3167
lenamonj wants to merge 1 commit into
iamkun:devfrom
lenamonj:fix/fractional-seconds

Conversation

@lenamonj

Copy link
Copy Markdown

REGEX_PARSE captures fractional seconds as bare digits and parseDate
truncates them with substring(0, 3) without right-padding, so a fraction
shorter than three digits is read as an integer count of milliseconds:

dayjs('2019-03-25T06:41:00.5').millisecond()  // 5  - moment and new Date() give 500
dayjs('2019-03-25T06:41:00.05').millisecond() // 5  - moment and new Date() give 50

ISO 8601 fractional seconds are a decimal fraction. Strings with a trailing
Z already take the native Date path and are unaffected; only the regex
path is hit.

Fix: right-pad the capture to three digits before truncating. Fractions of
three or more digits are unchanged (.999999999 still parses as 999), and
the no-fraction path still yields 0.

Test: one new case in test/parse.test.js beside the existing
parses unlimited millisecond test, comparing valueOf() and
millisecond() against moment for 1-, 2-, and 3-digit fractions. Without
the fix it fails (1 failed, 26 passed in the file); with it the file is 27
passed and the full suite is 774 passed across 93 suites, line coverage
unchanged at 100%.

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.

1 participant