Skip to content

Commit

Permalink
Begin migrating Utc and related time components to signed integers.
Browse files Browse the repository at this point in the history
  • Loading branch information
imclerran committed Jan 29, 2024
1 parent e022fba commit e96ff7e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion platform/Effect.roc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ tcpWrite : List U8, InternalTcp.Stream -> Effect InternalTcp.WriteResult

pathType : List U8 -> Effect (Result InternalPath.InternalPathType InternalPath.GetMetadataErr)

posixTime : Effect U128
posixTime : Effect I128
sleepMillis : U64 -> Effect {}

commandStatus : Box InternalCommand.Command -> Effect (Result {} InternalCommand.CommandErr)
Expand Down
14 changes: 7 additions & 7 deletions platform/Utc.roc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface Utc
imports [Effect, InternalTask, Task.{ Task }]

## Stores a timestamp as nanoseconds since UNIX EPOCH
Utc := U128 implements [Inspect]
Utc := I128 implements [Inspect]

## Duration since UNIX EPOCH
now : Task Utc *
Expand All @@ -26,30 +26,30 @@ now =
nanosPerMilli = 1_000_000

## Convert Utc timestamp to milliseconds
toMillisSinceEpoch : Utc -> U128
toMillisSinceEpoch : Utc -> I128
toMillisSinceEpoch = \@Utc nanos ->
nanos // nanosPerMilli

## Convert milliseconds to Utc timestamp
fromMillisSinceEpoch : U128 -> Utc
fromMillisSinceEpoch : I128 -> Utc
fromMillisSinceEpoch = \millis ->
@Utc (millis * nanosPerMilli)

## Convert Utc timestamp to nanoseconds
toNanosSinceEpoch : Utc -> U128
toNanosSinceEpoch : Utc -> I128
toNanosSinceEpoch = \@Utc nanos ->
nanos

## Convert nanoseconds to Utc timestamp
fromNanosSinceEpoch : U128 -> Utc
fromNanosSinceEpoch : I128 -> Utc
fromNanosSinceEpoch = @Utc

## Calculate milliseconds between two Utc timestamps
deltaAsMillis : Utc, Utc -> U128
deltaAsMillis : Utc, Utc -> I128
deltaAsMillis = \@Utc first, @Utc second ->
(Num.absDiff first second) // nanosPerMilli

## Calculate nanoseconds between two Utc timestamps
deltaAsNanos : Utc, Utc -> U128
deltaAsNanos : Utc, Utc -> I128
deltaAsNanos = \@Utc first, @Utc second ->
Num.absDiff first second
4 changes: 2 additions & 2 deletions platform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,13 +524,13 @@ pub extern "C" fn roc_fx_cwd() -> RocList<u8> {
}

#[no_mangle]
pub extern "C" fn roc_fx_posixTime() -> roc_std::U128 {
pub extern "C" fn roc_fx_posixTime() -> roc_std::I128 {
// TODO in future may be able to avoid this panic by using C APIs
let since_epoch = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("time went backwards");

roc_std::U128::from(since_epoch.as_nanos())
roc_std::I128::from(since_epoch.as_nanos() as i128)
}

#[no_mangle]
Expand Down

0 comments on commit e96ff7e

Please sign in to comment.