monotimes

The std/monotimes module implements monotonic timestamps. A monotonic timestamp represents the time that has passed since some system defined point in time. The monotonic timestamps are guaranteed to always increase, meaning that that the following is guaranteed to work:

let a = getMonoTime()
# ... do some work
let b = getMonoTime()
assert a <= b

This is not guaranteed for the times.Time type! This means that the MonoTime should be used when measuring durations of time with high precision.

However, since MonoTime represents the time that has passed since some unknown time origin, it cannot be converted to a human readable timestamp. If this is required, the times.Time type should be used instead.

The MonoTime type stores the timestamp in nanosecond resolution, but note that the actual supported time resolution differs for different systems.

See also

Imports

times, posix

Types

MonoTime = object
  ticks: int64
Represents a monotonic timestamp. Source Edit

Procs

proc getMonoTime(): MonoTime {...}{.tags: [TimeEffect], raises: [].}

Get the current MonoTime timestamp.

When compiled with the JS backend and executed in a browser, this proc calls window.performance.now(), which is not supported by older browsers. See MDN for more information.

Source Edit
proc ticks(t: MonoTime): int64 {...}{.raises: [], tags: [].}
Returns the raw ticks value from a MonoTime. This value always uses nanosecond time resolution. Source Edit
proc `$`(t: MonoTime): string {...}{.raises: [], tags: [].}
Source Edit
proc `-`(a, b: MonoTime): Duration {...}{.raises: [], tags: [].}
Returns the difference between two MonoTime timestamps as a Duration. Source Edit
proc `+`(a: MonoTime; b: Duration): MonoTime {...}{.raises: [], tags: [].}
Increases a by b. Source Edit
proc `-`(a: MonoTime; b: Duration): MonoTime {...}{.raises: [], tags: [].}
Reduces a by b. Source Edit
proc `<`(a, b: MonoTime): bool {...}{.raises: [], tags: [].}
Returns true if a happened before b. Source Edit
proc `<=`(a, b: MonoTime): bool {...}{.raises: [], tags: [].}
Returns true if a happened before b or if they happened simultaneous. Source Edit
proc `==`(a, b: MonoTime): bool {...}{.raises: [], tags: [].}
Returns true if a and b happened simultaneous. Source Edit
proc high(typ: typedesc[MonoTime]): MonoTime
Returns the highest representable MonoTime. Source Edit
proc low(typ: typedesc[MonoTime]): MonoTime
Returns the lowest representable MonoTime. Source Edit

© 2006–2021 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/monotimes.html