Skip to content

fix(google-api-core): preserve exact timestamp seconds - #18562

Open
rioyu123 wants to merge 1 commit into
googleapis:mainfrom
rioyu123:fix/api-core-pre-epoch-timestamp
Open

rioyu123 wants to merge 1 commit into
googleapis:mainfrom
rioyu123:fix/api-core-pre-epoch-timestamp

Conversation

@rioyu123

@rioyu123 rioyu123 commented Oct 4, 2026

Copy link
Copy Markdown

Fixes #18561.

DatetimeWithNanoseconds.timestamp_pb() can shift a historical timestamp forward by one second. For example, 1969-12-31T23:59:59.123456789Z becomes 1970-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

  • Added 15 cases checked against protobuf's own conversion methods: historical dates, the maximum timestamp, naive and timezone-aware inputs, and unaffected controls. Nine fail on the base commit.
  • Complete google-api-core unit and asyncio suites: 1,504 passed, 2 skipped on Python 3.12.3 with protobuf 7.36.2.
  • The affected helper retains 100% statement and branch coverage. Single-environment package coverage is unchanged at 99.67%.
  • Pinned Ruff import/format checks and Flake8 pass.

The full Python/dependency matrix and credentialed Cloud system tests were not run.

Checklist

  • Opened an issue before writing the code.
  • Tests and linter pass in the environment described above.
  • Code coverage does not decrease.
  • Checked documentation needs: no API or documented behavior change.

@rioyu123
rioyu123 requested a review from a team as a code owner October 4, 2026 04:22

@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 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

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.

DatetimeWithNanoseconds.timestamp_pb() returns incorrect seconds for fractional timestamps

1 participant