-
Notifications
You must be signed in to change notification settings - Fork 664
Various methods and trait impls for time arithmetic #2502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
|
Presumably we should do something similar for C#. |
|
It occurs to me that we could also provide |
kazimuth
left a comment
There was a problem hiding this 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.
coolreader18
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Per James' review.
…s/spacetimedb into phoebe/timestamp-arithmetic
Description of Changes
This commit adds
checked_addandchecked_submethods for arithmetic withTimestampandTimeDuration, and implementations ofAdd,Sub,AddAssignandSubAssign. Prior to this, BitCraft's code had to do convoluted conversions fromTimestampintoSystemTime, then do arithmetic, then convert back toTimeStamp.Specifically, we add methods and trait impls for
T x U -> V, wherexis a binary operator, either + or -:Timestamp x TimeDuration -> TimestampTimestamp x Duration -> Timestamp.Durationinto aTimeDurationbefore 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::SystemTimeandstd::time::Duration, plus allowingDurationas a convenience.I have chosen not to implement
TimeDuration x DurationorDuration x TimeDurationoperations, under the theory that if you're already working with aTimeDuration, 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::Durationarithmetic.Testing