Repository navigation
fix: keep exact milliseconds in from_datetime and from_timestamp - #71
Open
taltal-beep wants to merge 1 commit into
Open
taltal-beep wants to merge 1 commit into
taltal-beep wants to merge 1 commit into
Conversation
Converting seconds to milliseconds with int(value * 1000) truncates float error along with the sub-millisecond part. 1078625331.860 * 1000 is just below 1078625331860, so the ULID lands one millisecond early. This hit about 0.6% of random timestamps on an exact millisecond. Round to three decimals before truncating. That removes the float error and still drops real sub-millisecond digits.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Requested by Taltal
ULID.from_datetime()andULID.from_timestamp()can return a ULID one millisecond early for a value that sits exactly on a millisecond:_normalize_timestampdoesint(seconds * 1000). For some values the product is just below the integer as a float, andint()drops the whole fraction. In a random sample of 200,000 datetimes on exact milliseconds, about 0.6% came out one millisecond low. The same applies to a value read back fromULID.timestamp.The fix rounds to three decimals before truncating. That removes the float error and still drops real sub-millisecond digits, so
from_timestamp(1000.9999)stays at1000999. After the fix the same sample has no mismatches.The new test fails on
mainfor both methods and passes with the fix.pytest,ruff check,ruff format --checkanddoc8pass.pyrefly checkreports one error (overrideimport) that is also onmain.