nwaku/waku/v2/utils/time.nim
Hanno Cornelius 16c85db43c
chore: update submodules and bump Nim to 1.6.6 (#1307)
* chore: update submodules

* fix: libp2p now provides callback to update peer addrs

* fix: breaking change in EthereumNode constructor

* fix: contentType type has changed (again)

* fix: explicit future type

* fix: nim 1.6.6 error handling requirements

* fix: missed a spot - peer info addrs in sim2

* fix: help compiler a bit here
2022-10-28 12:51:46 +03:00

39 lines
996 B
Nim

## Contains types and utilities for timestamps.
{.push raises: [Defect].}
import
std/times,
metrics
type Timestamp* = int64
proc getNanosecondTime*[T](timeInSeconds: T): Timestamp =
var ns = Timestamp(timeInSeconds.int64 * 1000_000_000.int64)
return ns
proc getMicrosecondTime*[T](timeInSeconds: T): Timestamp =
var us = Timestamp(timeInSeconds.int64 * 1000_000.int64)
return us
proc getMillisecondTime*[T](timeInSeconds: T): Timestamp =
var ms = Timestamp(timeInSeconds.int64 * 1000.int64)
return ms
proc nowInUnixFloat(): float =
return getTime().toUnixFloat()
template nanosecondTime*(collector: Summary | Histogram, body: untyped) =
when defined(metrics):
let start = nowInUnixFloat()
body
collector.observe(nowInUnixFloat() - start)
else:
body
template nanosecondTime*(collector: Gauge, body: untyped) =
when defined(metrics):
let start = nowInUnixFloat()
body
metrics.set(collector, nowInUnixFloat() - start)
else:
body