Skip to content

Fix nanosecond truncation in _pb_timestamp_to_datetime - #19265

Open
adityajai119 wants to merge 1 commit into
googleapis:mainfrom
adityajai119:fix-pb-timestamp-nanos-truncation
Open

adityajai119 wants to merge 1 commit into
googleapis:mainfrom
adityajai119:fix-pb-timestamp-nanos-truncation

Conversation

@adityajai119

Copy link
Copy Markdown

@google-cloud/core's shared helper converted a Timestamp's nanoseconds to microseconds with float division. datetime.timedelta then rounded that float, so a timestamp with nanos=999999999 became one full second past the epoch, and nanos=123456789 became 123457 microseconds instead of 123456.

This switches the conversion to integer floor division, matching the truncation already used when writing timestamps, so sub-second values are preserved and never overflow into the next second.

Added a regression test in tests/unit/test__helpers.py. Narrow tests: 56 passed in test__helpers.py; ruff check on both changed files passed.

@adityajai119
adityajai119 requested a review from a team as a code owner November 29, 2026 17:09

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request modifies the _pb_timestamp_to_datetime helper function to use integer floor division (//) instead of float division (/) when converting nanoseconds to microseconds, ensuring nanoseconds are truncated rather than rounded. Corresponding unit tests have been added to verify this behavior. There are no review comments to address.

This branch has not been deployed

No deployments
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.

1 participant