Skip to content

Conversation

@gefjon
Copy link
Contributor

@gefjon gefjon commented Mar 25, 2025

Description of Changes

This commit adds checked_add and checked_sub methods for arithmetic with Timestamp and TimeDuration, and implementations of Add, Sub, AddAssign and SubAssign. Prior to this, BitCraft's code had to do convoluted conversions from Timestamp into SystemTime, then do arithmetic, then convert back to TimeStamp.

Specifically, we add methods and trait impls for T x U -> V, where x is a binary operator, either + or -:

  • Timestamp x TimeDuration -> Timestamp
  • Timestamp x Duration -> Timestamp.
    • This one is kind of weird, since we convert the Duration into a TimeDuration before doing math with it, which may be a lossy conversion depending on platform.
  • TimeDuration x TimeDuration -> TimeDuration.

These correspond to the operations that Rust exposes on std::time::SystemTime and std::time::Duration, plus allowing Duration as a convenience.

I have chosen not to implement
TimeDuration x Duration or Duration x TimeDuration operations, under the theory that if you're already working with a TimeDuration, you don't need to be shielded from the conversion.

Closes #2367 .

API and ABI breaking changes

This PR adds new APIs which will become stabilized. It does not remove or change any existing APIs.

Expected complexity level and risk

2? Some minor complexity in sats::Timestamp x std::time::Duration arithmetic.

Testing

  • Wrote a couple of simple sanity-check proptests.

This commit adds `checked_add` and `checked_sub` methods
for arithmetic with `Timestamp` and `TimeDuration`,
and implementations of `Add`, `Sub`, `AddAssign` and `SubAssign`.

Specifically, we add methods and trait impls for `T x U -> V`, where `x` is a binary operator,
either + or -:

- `Timestamp x TimeDuration -> Timestamp`
- `Timestamp x Duration -> Timestamp`.
  - This one is kind of weird, since we convert the `Duration` into a `TimeDuration`
    before doing math with it, which may be a lossy conversion depending on platform.
- `TimeDuration x TimeDuration -> TimeDuration`.

These correspond to the operations that Rust exposes on
`std::time::SystemTime` and `std::time::Duration`,
plus allowing `Duration` as a convenience.

I have chosen not to implement
`TimeDuration x Duration` or `Duration x TimeDuration` operations,
under the theory that if you're already working with a `TimeDuration`,
you don't need to be shielded from the conversion.
@gefjon gefjon added enhancement New feature or request release-any To be landed in any release window labels Mar 25, 2025
@gefjon gefjon self-assigned this Mar 25, 2025
@gefjon
Copy link
Contributor Author

gefjon commented Mar 25, 2025

Presumably we should do something similar for C#.

@gefjon
Copy link
Contributor Author

gefjon commented Mar 26, 2025

It occurs to me that we could also provide impl Sub<Timestamp> for Timestamp { type Output = TimeDuration; }. This would have the same behavior as Timestamp::time_duration_since. Rust doesn't provide this on SystemTime because their Duration is unsigned and so such a method would frequently panic, but we have signed TimeDuration so our version would only panic on under/overflow.

Copy link
Contributor

@kazimuth kazimuth left a comment

Choose a reason for hiding this comment

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

This makes sense. If you wouldn't mind making the corresponding change at the same time in C# that would be nice, it's in bindings-csharp/BSATN.Runtime/Builtins.rs, there are already a few operator overrides in there.

I'm wondering if we should put the corresponding operations inside https://stackoverflow.com/questions/2056445/no-overflow-exception-for-int-in-c blocks in C#. I think it would make sense for the C# API to always have exceptions thrown on overflow. Checked blocks are enforced syntactically, so the user has no other way to enable checking for time operations on the C# side.

Copy link
Collaborator

@coolreader18 coolreader18 left a comment

Choose a reason for hiding this comment

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

LGTM

@gefjon gefjon requested a review from kazimuth April 1, 2025 14:58
@gefjon gefjon enabled auto-merge April 3, 2025 17:42
@gefjon gefjon added this pull request to the merge queue Apr 3, 2025
Merged via the queue into master with commit 62818e9 Apr 3, 2025
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request release-any To be landed in any release window

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Timestamp methods for arithmetic with Duration

5 participants