Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the timestamp_pb method in datetime_helpers.py to calculate seconds using delta.days * 86400 + delta.seconds instead of int(delta.total_seconds()), preventing potential precision loss. Additionally, comprehensive unit tests have been added to verify that whole seconds are preserved correctly across various dates (including pre-1970 and far-future timestamps) and timezone offsets. There are no review comments to address, and the changes look solid.
This branch has not been deployed
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 #18561.
DatetimeWithNanoseconds.timestamp_pb()can shift a historical timestamp forward by one second. For example,1969-12-31T23:59:59.123456789Zbecomes1970-01-01T00:00:00.123456789Z. The same conversion can round the maximum valid timestamp past protobuf's supported range.The cause is
int(delta.total_seconds()): negative fractions truncate toward zero, and large values lose precision in the floating-point conversion. This change computes whole seconds directly from the timedelta's integer days and seconds. Nanosecond handling, the microsecond fallback and the return type stay unchanged.Tests
google-api-coreunit and asyncio suites: 1,504 passed, 2 skipped on Python 3.12.3 with protobuf 7.36.2.The full Python/dependency matrix and credentialed Cloud system tests were not run.
Checklist