2022-02-17 16:00:15 +01:00
|
|
|
## Contains types and utilities for timestamps.
|
2022-11-04 10:52:27 +01:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
else:
|
|
|
|
|
{.push raises: [].}
|
2022-02-17 16:00:15 +01:00
|
|
|
|
2023-02-03 10:06:21 +02:00
|
|
|
import
|
2022-09-30 18:13:42 +05:30
|
|
|
std/times,
|
|
|
|
|
metrics
|
2022-02-17 16:00:15 +01:00
|
|
|
|
2023-02-03 10:06:21 +02:00
|
|
|
type Timestamp* = int64
|
2022-02-17 16:00:15 +01:00
|
|
|
|
2023-02-03 10:06:21 +02:00
|
|
|
proc getNanosecondTime*[T](timeInSeconds: T): Timestamp =
|
2022-03-18 12:04:11 +02:00
|
|
|
var ns = Timestamp(timeInSeconds.int64 * 1000_000_000.int64)
|
2022-02-17 16:00:15 +01:00
|
|
|
return ns
|
|
|
|
|
|
2023-02-03 10:06:21 +02:00
|
|
|
proc getMicrosecondTime*[T](timeInSeconds: T): Timestamp =
|
2022-03-18 12:04:11 +02:00
|
|
|
var us = Timestamp(timeInSeconds.int64 * 1000_000.int64)
|
2022-02-17 16:00:15 +01:00
|
|
|
return us
|
|
|
|
|
|
2023-02-03 10:06:21 +02:00
|
|
|
proc getMillisecondTime*[T](timeInSeconds: T): Timestamp =
|
2022-03-18 12:04:11 +02:00
|
|
|
var ms = Timestamp(timeInSeconds.int64 * 1000.int64)
|
2022-02-17 16:00:15 +01:00
|
|
|
return ms
|
2022-09-30 18:13:42 +05:30
|
|
|
|
|
|
|
|
proc nowInUnixFloat(): float =
|
|
|
|
|
return getTime().toUnixFloat()
|
|
|
|
|
|
2023-02-03 10:06:21 +02:00
|
|
|
proc getNowInNanosecondTime*(): Timestamp =
|
|
|
|
|
return getNanosecondTime(nowInUnixFloat())
|
|
|
|
|
|
2022-09-30 18:13:42 +05:30
|
|
|
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
|
2022-10-28 12:51:46 +03:00
|
|
|
metrics.set(collector, nowInUnixFloat() - start)
|
2022-09-30 18:13:42 +05:30
|
|
|
else:
|
2023-02-03 10:06:21 +02:00
|
|
|
body
|