Skip to content

parser.parse( ) also silently truncates fractional seconds beyond 6 digits (via _parsems int(f.ljust(6,"0")[:6])) #1546

Description

@plotarmordev

Summary

dateutil.parser.parse truncates fractional-second digits beyond 6 in the generic parser path:

# dateutil/parser/_parser.py (_parsems)
return int(i), int(f.ljust(6, "0")[:6])
>>> from dateutil import parser
>>> parser.parse("2020-01-01 00:00:00.1234567").microsecond
123456      # correct rounding = 123457; the 7th digit is silently dropped

Same family as the isoparse report (#1545); this is the generic/most-used API (parser.parse), a distinct sink (_parsems). ISO-8601 permits arbitrary fraction precision; a silent wrong instant is returned with no error.

Suggested fix

Round to the nearest microsecond (Decimal / ROUND_HALF_EVEN) instead of truncating, and handle the carry when a rounded value reaches 1_000_000us. Alternatively reject >6 digits (matching datetime.fromisoformat). Happy to adjust to maintainer preference; caught by the exterminator sweep, four-gated.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions