From 84e32a3b695b2e54ff7733ca660bd95332b21d38 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Tue, 28 Jun 2022 16:47:59 +0300 Subject: [PATCH] Add high and low operators for Duration and Moment; Add Moment.epochSeconds and Moment.epochNanoSeconds --- chronos/timer.nim | 18 ++++++++++++++++++ tests/testtime.nim | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/chronos/timer.nim b/chronos/timer.nim index c458423..477a7d4 100644 --- a/chronos/timer.nim +++ b/chronos/timer.nim @@ -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 diff --git a/tests/testtime.nim b/tests/testtime.nim index 8a1f84d..a66d129 100644 --- a/tests/testtime.nim +++ b/tests/testtime.nim @@ -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