Skip to content

Commit

Permalink
Add high and low operators for Duration and Moment; Add Moment.epochS…
Browse files Browse the repository at this point in the history
…econds and Moment.epochNanoSeconds
  • Loading branch information
zah committed Jun 28, 2022
1 parent c6ce4d4 commit 84e32a3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions chronos/timer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,18 @@ const
ZeroDuration* = Duration(value: 0'i64)
InfiniteDuration* = Duration(value: high(int64))

template high*(T: typedesc[Moment]): Moment =
Moment(value: high(int64))

template low*(T: typedesc[Moment]): Moment =
Moment(value: 0)

template high*(T: typedesc[Duration]): Duration =
Duration(value: high(int64))

template low*(T: typedesc[Duration]): Duration =
Duration(value: 0)

func nanoseconds*(v: SomeIntegerI64): Duration {.inline.} =
## Initialize Duration with nanoseconds value ``v``.
result.value = int64(v)
Expand Down Expand Up @@ -438,6 +450,12 @@ func init*(t: typedesc[Moment], value: int64, precision: Duration): Moment =
## ``precision``.
result.value = value * precision.value

func epochSeconds*(moment: Moment): int64 =
moment.value div Second.value

func epochNanoSeconds*(moment: Moment): int64 =
moment.value

proc fromNow*(t: typedesc[Moment], a: Duration): Moment {.inline.} =
## Returns moment in time which is equal to current moment + Duration.
result = Moment.now() + a
Expand Down
5 changes: 5 additions & 0 deletions tests/testtime.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import ../chronos, ../chronos/timer

when defined(nimHasUsed): {.used.}

static:
doAssert Moment.high - Moment.low == Duration.high
doAssert Moment.low.epochSeconds == 0
doAssert Moment.low.epochNanoSeconds == 0

suite "Asynchronous timers & steps test suite":
const TimersCount = 10

Expand Down

0 comments on commit 84e32a3

Please sign in to comment.