Repository navigation
Round float seconds to nearest subsecond unit in Timestamp constructor - #40257
Merged
jrmccluskey merged 2 commits intoSep 24, 2026
Merged
jrmccluskey merged 2 commits into
jrmccluskey merged 2 commits into
Conversation
Fixes apache#40235: the float-to-integer conversion used int(), which truncates toward zero, so inexactly-representable floats like 2.000002 landed one microsecond below the intended value. Using round() makes the conversion match the documented behavior ("the fractional part will be captured up to precision digits") and is symmetric about zero. Adds a regression test.
Contributor
|
Assigning reviewers: R: @jrmccluskey for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
jrmccluskey
approved these changes
Sep 24, 2026
jrmccluskey
left a comment
Contributor
There was a problem hiding this comment.
LGTM, thanks! I appreciate the testing and documentation
This was referenced Sep 24, 2026
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.
Fixes #40235
The
Timestampconstructor converted float seconds to the internal integer representation withint(), which truncates toward zero. Because most decimal fractions are not exactly representable in binary (e.g.2.000002 * 1e6 == 2000001.9999999998), this landed one subsecond unit below the caller's value in about 1.2% of cases — including the commonTimestamp.of(time.time())path — and was asymmetric about zero.This change uses
round()instead ofint()for theseconds * 10**precisionconversion, matching the documented behavior ("the fractional part will be captured up to precision digits") and restoring symmetry about zero for negative timestamps. Integer inputs and exact float values are unaffected, anddivmodstill floors so subseconds stay non-negative.Also updates the class docstring to say the fractional part is rounded to the nearest subsecond unit, and adds a regression test (
test_constructor_float_rounds_to_nearest).Tests run:
python -m pytest apache_beam/utils/timestamp_test.py— 42 passed (41 existing + 1 new)