Skip to content

Commit

Permalink
Update delta functions to use unsigned types, and handle cases where …
Browse files Browse the repository at this point in the history
…the sign of first and second are oposed.
  • Loading branch information
imclerran committed Jan 31, 2024
1 parent a63a9fb commit 6152e35
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions platform/Utc.roc
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ fromNanosSinceEpoch : I128 -> Utc
fromNanosSinceEpoch = @Utc

## Calculate milliseconds between two Utc timestamps
deltaAsMillis : Utc, Utc -> I128
deltaAsMillis : Utc, Utc -> U128
deltaAsMillis = \@Utc first, @Utc second ->
(Num.absDiff first second) // nanosPerMilli
firstCast = Num.bitwiseXor (Num.toU128 first) (Num.shiftLeftBy 1 127)
secondCast = Num.bitwiseXor (Num.toU128 second) (Num.shiftLeftBy 1 127)
(Num.absDiff firstCast secondCast) // nanosPerMilli

## Calculate nanoseconds between two Utc timestamps
deltaAsNanos : Utc, Utc -> I128
deltaAsNanos : Utc, Utc -> U128
deltaAsNanos = \@Utc first, @Utc second ->
Num.absDiff first second
firstCast = Num.bitwiseXor (Num.toU128 first) (Num.shiftLeftBy 1 127)
secondCast = Num.bitwiseXor (Num.toU128 second) (Num.shiftLeftBy 1 127)
Num.absDiff firstCast secondCast

0 comments on commit 6152e35

Please sign in to comment.