Add high and low operators for Duration and Moment; Add Moment.epochSeconds and Moment.epochNanoSeconds
This commit is contained in:
parent
c6ce4d4fb2
commit
84e32a3b69
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue