Skip to content

fix(wkt): non-negative timestamp nanos#1634

Merged
coryan merged 3 commits intogoogleapis:mainfrom
coryan:fix-wkt-non-negative-timestamp-nanos
Mar 27, 2025
Merged

fix(wkt): non-negative timestamp nanos#1634
coryan merged 3 commits intogoogleapis:mainfrom
coryan:fix-wkt-non-negative-timestamp-nanos

Conversation

@coryan
Copy link
Copy Markdown
Collaborator

@coryan coryan commented Mar 27, 2025

The timestamp nanos are supposed to be non-negative. For timestamps
before the epoch:

Negative second values with fractions must still have non-negative
nanos values that count forward in time.

I took the opportunity to add some missing test cases.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.72%. Comparing base (e1d08db) to head (08c49d8).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1634   +/-   ##
=======================================
  Coverage   95.71%   95.72%           
=======================================
  Files          43       43           
  Lines        1822     1824    +2     
=======================================
+ Hits         1744     1746    +2     
  Misses         78       78           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The timestamp nanos are supposed to be non-negative. For timestamps
before the epoch:

> Negative second values with fractions must still have non-negative
> nanos values that count forward in time.

I took the opportunity to add some missing test cases.
@coryan coryan force-pushed the fix-wkt-non-negative-timestamp-nanos branch from 118e8b0 to bbb2448 Compare March 27, 2025 15:43
@coryan coryan marked this pull request as ready for review March 27, 2025 15:55
@coryan coryan requested review from codyoss and dbolduc March 27, 2025 15:56
let nanos_since_epoch = odt.unix_timestamp_nanos();
let seconds = (nanos_since_epoch / NS) as i64;
let nanos = (nanos_since_epoch % NS) as i32;
if !(0..Self::NS).contains(&nanos) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if nanos < 0 might be more clear?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

let seconds = (nanos_since_epoch / NS) as i64;
let nanos = (nanos_since_epoch % NS) as i32;
if !(0..Self::NS).contains(&nanos) {
return Timestamp::new(seconds - 1, Self::NS + nanos);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: Self::NS is just called NS above.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, they are different types. Ignore me.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I got confused myself.

@coryan coryan enabled auto-merge (squash) March 27, 2025 16:27
@coryan coryan merged commit 345cd34 into googleapis:main Mar 27, 2025
20 checks passed
@coryan coryan deleted the fix-wkt-non-negative-timestamp-nanos branch March 27, 2025 16:57
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.

2 participants