Skip to content

Fix microsecond precision loss in timestamp parsing#223

Merged
wimglenn merged 1 commit intor1chardj0n3s:masterfrom
karthiksai109:fix-microsecond-precision
Feb 19, 2026
Merged

Fix microsecond precision loss in timestamp parsing#223
wimglenn merged 1 commit intor1chardj0n3s:masterfrom
karthiksai109:fix-microsecond-precision

Conversation

@karthiksai109
Copy link
Copy Markdown
Contributor

The fractional seconds conversion in date_convert used float arithmetic to turn the decimal portion into microseconds:

u = int(float('.' + u) * 1000000)

This causes precision loss for certain values due to floating-point representation. For example, float('.501902') * 1000000 evaluates to 501901.99999... which int() truncates to 501901 instead of the correct 501902.

The fix avoids float entirely by padding the fractional digits string to 6 characters (or truncating if longer) and converting directly to int:

u = int(u.ljust(6, '0')[:6])

This handles all cases correctly: fewer than 6 digits get zero-padded, exactly 6 digits pass through unchanged, and more than 6 digits get truncated to microsecond precision.

Includes a regression test covering these cases.

Fixes #162

@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.73%. Comparing base (7384477) to head (ba6803d).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #223   +/-   ##
=======================================
  Coverage   94.73%   94.73%           
=======================================
  Files           1        1           
  Lines         551      551           
  Branches      130      130           
=======================================
  Hits          522      522           
  Misses         19       19           
  Partials       10       10           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@wimglenn wimglenn merged commit cd6bf20 into r1chardj0n3s:master Feb 19, 2026
23 checks passed
@wimglenn
Copy link
Copy Markdown
Collaborator

Released in 1.21.1. Thanks for your contribution!

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.

Timestamp can be stored differently than parsed

3 participants