Skip to content

Commit

Permalink
code style improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-4 authored Feb 9, 2024
1 parent 8547b50 commit e995c7c
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions platform/Utc.roc
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,16 @@ fromNanosSinceEpoch = @Utc

## Calculate milliseconds between two Utc timestamps
deltaAsMillis : Utc, Utc -> U128
deltaAsMillis = \@Utc first, @Utc 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) // nanosPerMilli
deltaAsMillis = \utcA, utcB ->
(deltaAsNanos utcA utcB) // nanosPerMilli

## Calculate nanoseconds between two Utc timestamps
deltaAsNanos : Utc, Utc -> U128
deltaAsNanos = \@Utc first, @Utc 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
deltaAsNanos = \@Utc nanosA, @Utc nanosB ->
# bitwiseXor for best performance
nanosAShifted = Num.bitwiseXor (Num.toU128 nanosA) (Num.shiftLeftBy 1 127)
nanosBShifted = Num.bitwiseXor (Num.toU128 nanosB) (Num.shiftLeftBy 1 127)
Num.absDiff nanosAShifted nanosBShifted

# TESTS
expect deltaAsNanos (fromNanosSinceEpoch 0) (fromNanosSinceEpoch 0) == 0
Expand All @@ -84,4 +83,4 @@ expect deltaAsMillis (fromMillisSinceEpoch 0) (fromMillisSinceEpoch 0) == 0
expect deltaAsMillis (fromNanosSinceEpoch 1) (fromNanosSinceEpoch 2) == 0
expect deltaAsMillis (fromMillisSinceEpoch 1) (fromMillisSinceEpoch 2) == 1
expect deltaAsMillis (fromMillisSinceEpoch -1) (fromMillisSinceEpoch 1) == 2
expect deltaAsMillis (fromNanosSinceEpoch Num.minI128) (fromNanosSinceEpoch Num.maxI128) == Num.maxU128 // nanosPerMilli
expect deltaAsMillis (fromNanosSinceEpoch Num.minI128) (fromNanosSinceEpoch Num.maxI128) == Num.maxU128 // nanosPerMilli

0 comments on commit e995c7c

Please sign in to comment.