Add high and low operators for Duration and Moment; Add Moment.epochSeconds and Moment.epochNanoSeconds

This commit is contained in:
Zahary Karadjov 2022-06-28 16:47:59 +03:00
parent c6ce4d4fb2
commit 84e32a3b69
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
2 changed files with 23 additions and 0 deletions

View File

@ -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)
@ -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

View File

@ -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