Skip to content

Round float seconds to nearest subsecond unit in Timestamp constructor - #40257

Merged
jrmccluskey merged 2 commits into
apache:masterfrom
DeepikaMamidipelly009:fix-timestamp-float-rounding-40235
Sep 24, 2026
Merged

jrmccluskey merged 2 commits into
apache:masterfrom
DeepikaMamidipelly009:fix-timestamp-float-rounding-40235

Conversation

@DeepikaMamidipelly009

Copy link
Copy Markdown
Contributor

Fixes #40235

The Timestamp constructor converted float seconds to the internal integer representation with int(), 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 common Timestamp.of(time.time()) path — and was asymmetric about zero.

This change uses round() instead of int() for the seconds * 10**precision conversion, 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, and divmod still 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)
  • Checked that no other SDK tests assert the old truncation behavior.

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.
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @jrmccluskey for label python.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@jrmccluskey jrmccluskey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! I appreciate the testing and documentation

@jrmccluskey
jrmccluskey merged commit 5d32ff7 into apache:master Sep 24, 2026
100 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Timestamp(float seconds) truncates instead of rounding, losing a microsecond

2 participants