2018-05-16 08:22:34 +00:00
|
|
|
#
|
2019-02-06 14:49:11 +00:00
|
|
|
# Chronos Timer
|
2018-05-16 08:22:34 +00:00
|
|
|
#
|
2019-02-06 14:49:11 +00:00
|
|
|
# (c) Copyright 2017 Eugene Kabanov
|
|
|
|
# (c) Copyright 2018-Present Status Research & Development GmbH
|
2018-05-16 08:22:34 +00:00
|
|
|
#
|
2018-06-06 00:22:28 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
|
|
|
|
2018-05-16 08:22:34 +00:00
|
|
|
## This module implements cross-platform system timer with
|
|
|
|
## milliseconds resolution.
|
2019-03-24 16:57:36 +00:00
|
|
|
##
|
|
|
|
## Timer supports two types of clocks:
|
|
|
|
## ``system`` uses the most fast OS primitive to obtain wall clock time.
|
|
|
|
## ``mono`` uses monotonic clock time (default).
|
|
|
|
##
|
|
|
|
## ``system`` clock is affected by discontinuous jumps in the system time. This
|
|
|
|
## clock is significantly faster then ``mono`` clock in most of the cases.
|
|
|
|
##
|
|
|
|
## ``mono`` clock is not affected by discontinuous jumps in the system time.
|
|
|
|
## This clock is slower then ``system`` clock.
|
|
|
|
##
|
|
|
|
## You can specify which timer you want to use ``-d:asyncTimer=<system/mono>``.
|
2022-11-02 07:09:15 +00:00
|
|
|
import stew/base10
|
2023-02-21 10:48:36 +00:00
|
|
|
import "."/osdefs
|
2022-11-02 07:09:15 +00:00
|
|
|
|
2019-03-24 16:57:36 +00:00
|
|
|
const asyncTimer* {.strdefine.} = "mono"
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2023-06-05 20:21:50 +00:00
|
|
|
{.push raises: [].}
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
|
2018-05-16 08:22:34 +00:00
|
|
|
when defined(windows):
|
2019-03-24 16:57:36 +00:00
|
|
|
when asyncTimer == "system":
|
|
|
|
proc fastEpochTime*(): uint64 {.
|
|
|
|
inline, deprecated: "Use Moment.now()".} =
|
|
|
|
## Timer resolution is millisecond.
|
|
|
|
var t: FILETIME
|
|
|
|
getSystemTimeAsFileTime(t)
|
2023-02-21 10:48:36 +00:00
|
|
|
((uint64(t.dwHighDateTime) shl 32) or uint64(t.dwLowDateTime)) div 10_000
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
proc fastEpochTimeNano(): uint64 {.inline.} =
|
|
|
|
## Timer resolution is nanosecond.
|
|
|
|
var t: FILETIME
|
|
|
|
getSystemTimeAsFileTime(t)
|
2023-02-21 10:48:36 +00:00
|
|
|
((uint64(t.dwHighDateTime) shl 32) or uint64(t.dwLowDateTime)) * 100
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
var queryFrequencyM: uint64
|
|
|
|
var queryFrequencyN: uint64
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2019-03-24 16:57:36 +00:00
|
|
|
proc fastEpochTimeNano(): uint64 {.inline.} =
|
|
|
|
## Procedure's resolution is nanosecond.
|
|
|
|
var res: uint64
|
2023-02-21 10:48:36 +00:00
|
|
|
queryPerformanceCounter(res)
|
2022-11-02 07:09:15 +00:00
|
|
|
res * queryFrequencyN
|
2019-03-24 16:57:36 +00:00
|
|
|
|
2022-11-02 07:09:15 +00:00
|
|
|
proc fastEpochTime*(): uint64 {.inline, deprecated: "Use Moment.now()".} =
|
2019-03-24 16:57:36 +00:00
|
|
|
## Procedure's resolution is millisecond.
|
|
|
|
var res: uint64
|
2023-02-21 10:48:36 +00:00
|
|
|
queryPerformanceCounter(res)
|
2022-11-02 07:09:15 +00:00
|
|
|
res div queryFrequencyM
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
proc setupQueryFrequence() =
|
|
|
|
var freq: uint64
|
2023-02-21 10:48:36 +00:00
|
|
|
queryPerformanceFrequency(freq)
|
2019-03-24 16:57:36 +00:00
|
|
|
if freq < 1000:
|
|
|
|
queryFrequencyM = freq
|
|
|
|
else:
|
|
|
|
queryFrequencyM = freq div 1_000
|
|
|
|
queryFrequencyN = 1_000_000_000'u64 div freq
|
|
|
|
|
|
|
|
setupQueryFrequence()
|
2018-05-16 08:22:34 +00:00
|
|
|
|
|
|
|
elif defined(macosx):
|
|
|
|
|
2019-03-24 16:57:36 +00:00
|
|
|
when asyncTimer == "system":
|
|
|
|
|
2023-02-21 10:48:36 +00:00
|
|
|
proc fastEpochTime*(): uint64 {.inline, deprecated: "Use Moment.now()".} =
|
2019-03-24 16:57:36 +00:00
|
|
|
## Procedure's resolution is millisecond.
|
|
|
|
var t: Timeval
|
|
|
|
posix_gettimeofday(t)
|
2022-11-02 07:09:15 +00:00
|
|
|
uint64(t.tv_sec) * 1_000 + uint64(t.tv_usec) div 1_000
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2019-03-24 16:57:36 +00:00
|
|
|
proc fastEpochTimeNano(): uint64 {.inline.} =
|
|
|
|
## Procedure's resolution is nanosecond.
|
|
|
|
var t: Timeval
|
|
|
|
posix_gettimeofday(t)
|
2022-11-02 07:09:15 +00:00
|
|
|
uint64(t.tv_sec) * 1_000_000_000 + uint64(t.tv_usec) * 1_000
|
2019-03-24 16:57:36 +00:00
|
|
|
else:
|
2019-09-03 14:30:28 +00:00
|
|
|
var queryFrequencyN: uint64
|
|
|
|
var queryFrequencyD: uint64
|
|
|
|
|
|
|
|
proc setupQueryFrequence() =
|
|
|
|
var info: MachTimebaseInfo
|
|
|
|
mach_timebase_info(info)
|
|
|
|
queryFrequencyN = info.numer
|
|
|
|
queryFrequencyD = info.denom
|
2018-05-29 09:59:39 +00:00
|
|
|
|
2023-02-21 10:48:36 +00:00
|
|
|
proc fastEpochTime*(): uint64 {.inline, deprecated: "Use Moment.now()".} =
|
2019-03-24 16:57:36 +00:00
|
|
|
## Procedure's resolution is millisecond.
|
2022-11-02 07:09:15 +00:00
|
|
|
let res = (mach_absolute_time() * queryFrequencyN) div queryFrequencyD
|
|
|
|
res div 1_000_000
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
proc fastEpochTimeNano(): uint64 {.inline.} =
|
|
|
|
## Procedure's resolution is nanosecond.
|
2022-11-02 07:09:15 +00:00
|
|
|
(mach_absolute_time() * queryFrequencyN) div queryFrequencyD
|
2019-09-03 14:30:28 +00:00
|
|
|
|
|
|
|
setupQueryFrequence()
|
2018-05-16 08:22:34 +00:00
|
|
|
|
|
|
|
elif defined(posix):
|
2019-03-24 16:57:36 +00:00
|
|
|
when asyncTimer == "system":
|
2023-02-21 10:48:36 +00:00
|
|
|
proc fastEpochTime*(): uint64 {.inline, deprecated: "Use Moment.now()".} =
|
2019-03-24 16:57:36 +00:00
|
|
|
## Procedure's resolution is millisecond.
|
|
|
|
var t: Timespec
|
|
|
|
discard clock_gettime(CLOCK_REALTIME, t)
|
2022-11-02 07:09:15 +00:00
|
|
|
uint64(t.tv_sec) * 1_000 + (uint64(t.tv_nsec) div 1_000_000)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
proc fastEpochTimeNano(): uint64 {.inline.} =
|
|
|
|
## Procedure's resolution is nanosecond.
|
|
|
|
var t: Timespec
|
|
|
|
discard clock_gettime(CLOCK_REALTIME, t)
|
2022-11-02 07:09:15 +00:00
|
|
|
uint64(t.tv_sec) * 1_000_000_000'u64 + uint64(t.tv_nsec)
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2019-03-24 16:57:36 +00:00
|
|
|
else:
|
2023-02-21 10:48:36 +00:00
|
|
|
proc fastEpochTime*(): uint64 {.inline, deprecated: "Use Moment.now()".} =
|
2019-03-24 16:57:36 +00:00
|
|
|
## Procedure's resolution is millisecond.
|
|
|
|
var t: Timespec
|
|
|
|
discard clock_gettime(CLOCK_MONOTONIC, t)
|
2022-11-02 07:09:15 +00:00
|
|
|
uint64(t.tv_sec) * 1_000 + (uint64(t.tv_nsec) div 1_000_000)
|
2018-05-16 08:22:34 +00:00
|
|
|
|
2019-03-24 16:57:36 +00:00
|
|
|
proc fastEpochTimeNano(): uint64 {.inline.} =
|
|
|
|
## Procedure's resolution is nanosecond.
|
|
|
|
var t: Timespec
|
|
|
|
discard clock_gettime(CLOCK_MONOTONIC, t)
|
2022-11-02 07:09:15 +00:00
|
|
|
uint64(t.tv_sec) * 1_000_000_000'u64 + uint64(t.tv_nsec)
|
2018-05-16 08:22:34 +00:00
|
|
|
|
|
|
|
elif defined(nimdoc):
|
2023-02-21 10:48:36 +00:00
|
|
|
discard
|
2018-05-16 08:22:34 +00:00
|
|
|
else:
|
|
|
|
error("Sorry, your operation system is not yet supported!")
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Moment* = object
|
|
|
|
## A Moment in time. Its value has no direct meaning, but can be compared
|
|
|
|
## with other Moments. Moments are captured using a monotonically
|
|
|
|
## non-decreasing clock (by default).
|
|
|
|
value: int64
|
|
|
|
|
|
|
|
Duration* = object
|
|
|
|
## A Duration is the interval between to points in time.
|
|
|
|
value: int64
|
|
|
|
|
|
|
|
when sizeof(int) == 4:
|
|
|
|
type SomeIntegerI64* = SomeSignedInt|uint|uint8|uint16|uint32
|
|
|
|
else:
|
|
|
|
type SomeIntegerI64* = SomeSignedInt|uint8|uint16|uint32
|
|
|
|
|
|
|
|
func `+`*(a: Duration, b: Duration): Duration {.inline.} =
|
|
|
|
## Duration + Duration = Duration
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: a.value + b.value)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `+`*(a: Duration, b: Moment): Moment {.inline.} =
|
|
|
|
## Duration + Moment = Moment
|
2022-11-02 07:09:15 +00:00
|
|
|
Moment(value: a.value + b.value)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `+`*(a: Moment, b: Duration): Moment {.inline.} =
|
|
|
|
## Moment + Duration = Moment
|
2022-11-02 07:09:15 +00:00
|
|
|
Moment(value: a.value + b.value)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `+=`*(a: var Moment, b: Duration) {.inline.} =
|
|
|
|
## Moment += Duration
|
|
|
|
a.value += b.value
|
|
|
|
|
|
|
|
func `+=`*(a: var Duration, b: Duration) {.inline.} =
|
|
|
|
## Duration += Duration
|
|
|
|
a.value += b.value
|
|
|
|
|
|
|
|
func `-`*(a, b: Moment): Duration {.inline.} =
|
|
|
|
## Moment - Moment = Duration
|
|
|
|
##
|
|
|
|
## Note: Duration can't be negative.
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: if a.value >= b.value: a.value - b.value else: 0'i64)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `-`*(a: Moment, b: Duration): Moment {.inline.} =
|
|
|
|
## Moment - Duration = Moment
|
|
|
|
##
|
|
|
|
## Note: Moment can be negative
|
2022-11-02 07:09:15 +00:00
|
|
|
Moment(value: a.value - b.value)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `-`*(a: Duration, b: Duration): Duration {.inline.} =
|
|
|
|
## Duration - Duration = Duration
|
|
|
|
##
|
|
|
|
## Note: Duration can't be negative.
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: if a.value >= b.value: a.value - b.value else: 0'i64)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
2022-11-02 07:09:15 +00:00
|
|
|
func `-=`*(a: var Duration, b: Duration) {.inline.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
## Duration -= Duration
|
|
|
|
a.value = if a.value >= b.value: a.value - b.value else: 0'i64
|
|
|
|
|
2022-11-02 07:09:15 +00:00
|
|
|
func `-=`*(a: var Moment, b: Duration) {.inline.} =
|
2019-03-24 16:57:36 +00:00
|
|
|
## Moment -= Duration
|
|
|
|
a.value -= b.value
|
|
|
|
|
|
|
|
func `==`*(a, b: Duration): bool {.inline.} =
|
|
|
|
## Returns ``true`` if ``a`` equal to ``b``.
|
2022-11-02 07:09:15 +00:00
|
|
|
a.value == b.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `==`*(a, b: Moment): bool {.inline.} =
|
|
|
|
## Returns ``true`` if ``a`` equal to ``b``.
|
2022-11-02 07:09:15 +00:00
|
|
|
a.value == b.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `<`*(a, b: Duration): bool {.inline.} =
|
|
|
|
## Returns ``true`` if ``a`` less then ``b``.
|
2022-11-02 07:09:15 +00:00
|
|
|
a.value < b.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `<`*(a, b: Moment): bool {.inline.} =
|
|
|
|
## Returns ``true`` if ``a`` less then ``b``.
|
2022-11-02 07:09:15 +00:00
|
|
|
a.value < b.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `<=`*(a, b: Duration): bool {.inline.} =
|
|
|
|
## Returns ``true`` if ``a`` less or equal ``b``.
|
2022-11-02 07:09:15 +00:00
|
|
|
a.value <= b.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `<=`*(a, b: Moment): bool {.inline.} =
|
|
|
|
## Returns ``true`` if ``a`` less or equal ``b``.
|
2022-11-02 07:09:15 +00:00
|
|
|
a.value <= b.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `>`*(a, b: Duration): bool {.inline.} =
|
|
|
|
## Returns ``true`` if ``a`` bigger then ``b``.
|
2022-11-02 07:09:15 +00:00
|
|
|
a.value > b.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `>`*(a, b: Moment): bool {.inline.} =
|
|
|
|
## Returns ``true`` if ``a`` bigger then ``b``.
|
2022-11-02 07:09:15 +00:00
|
|
|
a.value > b.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `>=`*(a, b: Duration): bool {.inline.} =
|
|
|
|
## Returns ``true`` if ``a`` bigger or equal ``b``.
|
2022-11-02 07:09:15 +00:00
|
|
|
a.value >= b.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `>=`*(a, b: Moment): bool {.inline.} =
|
|
|
|
## Returns ``true`` if ``a`` bigger or equal ``b``.
|
2022-11-02 07:09:15 +00:00
|
|
|
a.value >= b.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `*`*(a: Duration, b: SomeIntegerI64): Duration {.inline.} =
|
|
|
|
## Returns Duration multiplied by scalar integer.
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: a.value * int64(b))
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `*`*(a: SomeIntegerI64, b: Duration): Duration {.inline.} =
|
|
|
|
## Returns Duration multiplied by scalar integer.
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: int64(a) * b.value)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `div`*(a: Duration, b: SomeIntegerI64): Duration {.inline.} =
|
|
|
|
## Returns Duration which is result of dividing a Duration by scalar integer.
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: a.value div int64(b))
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
Nanosecond* = Duration(value: 1'i64)
|
|
|
|
Microsecond* = Nanosecond * 1_000'i64
|
|
|
|
Millisecond* = Microsecond * 1_000'i64
|
|
|
|
Second* = Millisecond * 1_000'i64
|
|
|
|
Minute* = Second * 60'i64
|
|
|
|
Hour* = Minute * 60'i64
|
|
|
|
Day* = Hour * 24'i64
|
|
|
|
Week* = Day * 7'i64
|
|
|
|
|
|
|
|
ZeroDuration* = Duration(value: 0'i64)
|
|
|
|
InfiniteDuration* = Duration(value: high(int64))
|
|
|
|
|
2022-06-28 13:47:59 +00:00
|
|
|
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)
|
|
|
|
|
2019-03-24 16:57:36 +00:00
|
|
|
func nanoseconds*(v: SomeIntegerI64): Duration {.inline.} =
|
|
|
|
## Initialize Duration with nanoseconds value ``v``.
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: int64(v))
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func microseconds*(v: SomeIntegerI64): Duration {.inline.} =
|
|
|
|
## Initialize Duration with microseconds value ``v``.
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: int64(v) * Microsecond.value)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func milliseconds*(v: SomeIntegerI64): Duration {.inline.} =
|
|
|
|
## Initialize Duration with milliseconds value ``v``.
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: int64(v) * Millisecond.value)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func seconds*(v: SomeIntegerI64): Duration {.inline.} =
|
|
|
|
## Initialize Duration with seconds value ``v``.
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: int64(v) * Second.value)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func minutes*(v: SomeIntegerI64): Duration {.inline.} =
|
|
|
|
## Initialize Duration with minutes value ``v``.
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: int64(v) * Minute.value)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func hours*(v: SomeIntegerI64): Duration {.inline.} =
|
|
|
|
## Initialize Duration with hours value ``v``.
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: int64(v) * Hour.value)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func days*(v: SomeIntegerI64): Duration {.inline.} =
|
|
|
|
## Initialize Duration with days value ``v``.
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: int64(v) * Day.value)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func weeks*(v: SomeIntegerI64): Duration {.inline.} =
|
|
|
|
## Initialize Duration with weeks value ``v``.
|
2022-11-02 07:09:15 +00:00
|
|
|
Duration(value: int64(v) * Week.value)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func nanoseconds*(v: Duration): int64 {.inline.} =
|
|
|
|
## Round Duration ``v`` to nanoseconds.
|
2022-11-02 07:09:15 +00:00
|
|
|
v.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func microseconds*(v: Duration): int64 {.inline.} =
|
|
|
|
## Round Duration ``v`` to microseconds.
|
2022-11-02 07:09:15 +00:00
|
|
|
v.value div Microsecond.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func milliseconds*(v: Duration): int64 {.inline.} =
|
|
|
|
## Round Duration ``v`` to milliseconds.
|
2022-11-02 07:09:15 +00:00
|
|
|
v.value div Millisecond.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func seconds*(v: Duration): int64 {.inline.} =
|
|
|
|
## Round Duration ``v`` to seconds.
|
2022-11-02 07:09:15 +00:00
|
|
|
v.value div Second.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func minutes*(v: Duration): int64 {.inline.} =
|
|
|
|
## Round Duration ``v`` to minutes.
|
2022-11-02 07:09:15 +00:00
|
|
|
v.value div Minute.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func hours*(v: Duration): int64 {.inline.} =
|
|
|
|
## Round Duration ``v`` to hours.
|
2022-11-02 07:09:15 +00:00
|
|
|
v.value div Hour.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func days*(v: Duration): int64 {.inline.} =
|
|
|
|
## Round Duration ``v`` to days.
|
2022-11-02 07:09:15 +00:00
|
|
|
v.value div Day.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func weeks*(v: Duration): int64 {.inline.} =
|
|
|
|
## Round Duration ``v`` to weeks.
|
2022-11-02 07:09:15 +00:00
|
|
|
v.value div Week.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func nanos*(v: SomeIntegerI64): Duration {.inline.} =
|
2022-11-02 07:09:15 +00:00
|
|
|
nanoseconds(v)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func micros*(v: SomeIntegerI64): Duration {.inline.} =
|
2022-11-02 07:09:15 +00:00
|
|
|
microseconds(v)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func millis*(v: SomeIntegerI64): Duration {.inline.} =
|
2022-11-02 07:09:15 +00:00
|
|
|
milliseconds(v)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func secs*(v: SomeIntegerI64): Duration {.inline.} =
|
2022-11-02 07:09:15 +00:00
|
|
|
seconds(v)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func nanos*(v: Duration): int64 {.inline.} =
|
2022-11-02 07:09:15 +00:00
|
|
|
nanoseconds(v)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func micros*(v: Duration): int64 {.inline.} =
|
2022-11-02 07:09:15 +00:00
|
|
|
microseconds(v)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func millis*(v: Duration): int64 {.inline.} =
|
2022-11-02 07:09:15 +00:00
|
|
|
milliseconds(v)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func secs*(v: Duration): int64 {.inline.} =
|
2022-11-02 07:09:15 +00:00
|
|
|
seconds(v)
|
|
|
|
|
|
|
|
template add(a: var string, b: Base10Buf[uint64]) =
|
|
|
|
for index in 0 ..< b.len:
|
|
|
|
a.add(char(b.data[index]))
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `$`*(a: Duration): string {.inline.} =
|
|
|
|
## Returns string representation of Duration ``a`` as nanoseconds value.
|
2022-11-02 07:09:15 +00:00
|
|
|
var res = ""
|
2019-03-24 16:57:36 +00:00
|
|
|
var v = a.value
|
2022-11-02 07:09:15 +00:00
|
|
|
|
2019-03-24 16:57:36 +00:00
|
|
|
if v >= Week.value:
|
2022-11-02 07:09:15 +00:00
|
|
|
res.add(Base10.toBytes(uint64(v div Week.value)))
|
|
|
|
res.add('w')
|
2019-03-24 16:57:36 +00:00
|
|
|
v = v mod Week.value
|
2022-11-02 07:09:15 +00:00
|
|
|
if v == 0: return res
|
2019-03-24 16:57:36 +00:00
|
|
|
if v >= Day.value:
|
2022-11-02 07:09:15 +00:00
|
|
|
res.add(Base10.toBytes(uint64(v div Day.value)))
|
|
|
|
res.add('d')
|
2019-03-24 16:57:36 +00:00
|
|
|
v = v mod Day.value
|
2022-11-02 07:09:15 +00:00
|
|
|
if v == 0: return res
|
2019-03-24 16:57:36 +00:00
|
|
|
if v >= Hour.value:
|
2022-11-02 07:09:15 +00:00
|
|
|
res.add(Base10.toBytes(uint64(v div Hour.value)))
|
|
|
|
res.add('h')
|
2019-03-24 16:57:36 +00:00
|
|
|
v = v mod Hour.value
|
2022-11-02 07:09:15 +00:00
|
|
|
if v == 0: return res
|
2019-03-24 16:57:36 +00:00
|
|
|
if v >= Minute.value:
|
2022-11-02 07:09:15 +00:00
|
|
|
res.add(Base10.toBytes(uint64(v div Minute.value)))
|
|
|
|
res.add('m')
|
2019-03-24 16:57:36 +00:00
|
|
|
v = v mod Minute.value
|
2022-11-02 07:09:15 +00:00
|
|
|
if v == 0: return res
|
2019-03-24 16:57:36 +00:00
|
|
|
if v >= Second.value:
|
2022-11-02 07:09:15 +00:00
|
|
|
res.add(Base10.toBytes(uint64(v div Second.value)))
|
|
|
|
res.add('s')
|
2019-03-24 16:57:36 +00:00
|
|
|
v = v mod Second.value
|
2022-11-02 07:09:15 +00:00
|
|
|
if v == 0: return res
|
2019-03-24 16:57:36 +00:00
|
|
|
if v >= Millisecond.value:
|
2022-11-02 07:09:15 +00:00
|
|
|
res.add(Base10.toBytes(uint64(v div Millisecond.value)))
|
|
|
|
res.add('m')
|
|
|
|
res.add('s')
|
2019-03-24 16:57:36 +00:00
|
|
|
v = v mod Millisecond.value
|
2022-11-02 07:09:15 +00:00
|
|
|
if v == 0: return res
|
2019-03-24 16:57:36 +00:00
|
|
|
if v >= Microsecond.value:
|
2022-11-02 07:09:15 +00:00
|
|
|
res.add(Base10.toBytes(uint64(v div Microsecond.value)))
|
|
|
|
res.add('u')
|
|
|
|
res.add('s')
|
2019-03-24 16:57:36 +00:00
|
|
|
v = v mod Microsecond.value
|
2022-11-02 07:09:15 +00:00
|
|
|
if v == 0: return res
|
|
|
|
res.add(Base10.toBytes(uint64(v div Nanosecond.value)))
|
|
|
|
res.add('n')
|
|
|
|
res.add('s')
|
|
|
|
res
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func `$`*(a: Moment): string {.inline.} =
|
|
|
|
## Returns string representation of Moment ``a`` as nanoseconds value.
|
2022-11-02 07:09:15 +00:00
|
|
|
var res = ""
|
|
|
|
res.add(Base10.toBytes(uint64(a.value)))
|
|
|
|
res.add('n')
|
|
|
|
res.add('s')
|
|
|
|
res
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func isZero*(a: Duration): bool {.inline.} =
|
|
|
|
## Returns ``true`` if Duration ``a`` is ``0``.
|
2022-11-02 07:09:15 +00:00
|
|
|
a.value == 0
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func isInfinite*(a: Duration): bool {.inline.} =
|
|
|
|
## Returns ``true`` if Duration ``a`` is infinite.
|
2022-11-02 07:09:15 +00:00
|
|
|
a.value == InfiniteDuration.value
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
proc now*(t: typedesc[Moment]): Moment {.inline.} =
|
|
|
|
## Returns current moment in time as Moment.
|
2022-11-02 07:09:15 +00:00
|
|
|
Moment(value: int64(fastEpochTimeNano()))
|
2019-03-24 16:57:36 +00:00
|
|
|
|
|
|
|
func init*(t: typedesc[Moment], value: int64, precision: Duration): Moment =
|
|
|
|
## Initialize Moment with absolute time value ``value`` with precision
|
|
|
|
## ``precision``.
|
2022-11-02 07:09:15 +00:00
|
|
|
Moment(value: value * precision.value)
|
2019-03-24 16:57:36 +00:00
|
|
|
|
2022-06-28 13:47:59 +00:00
|
|
|
func epochSeconds*(moment: Moment): int64 =
|
|
|
|
moment.value div Second.value
|
|
|
|
|
|
|
|
func epochNanoSeconds*(moment: Moment): int64 =
|
|
|
|
moment.value
|
|
|
|
|
2019-03-24 16:57:36 +00:00
|
|
|
proc fromNow*(t: typedesc[Moment], a: Duration): Moment {.inline.} =
|
|
|
|
## Returns moment in time which is equal to current moment + Duration.
|
2022-11-02 07:09:15 +00:00
|
|
|
Moment.now() + a
|