2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2022-02-17 15:00:15 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import std/times, metrics
|
2022-02-17 15:00:15 +00:00
|
|
|
|
2023-02-21 12:52:30 +00:00
|
|
|
type Timestamp* = int64 # A nanosecond precision timestamp
|
2022-02-17 15:00:15 +00:00
|
|
|
|
2024-05-22 01:00:22 +00:00
|
|
|
proc getNanosecondTime*(timeInSeconds: int64): Timestamp =
|
|
|
|
let ns = Timestamp(timeInSeconds * int64(1_000_000_000))
|
|
|
|
return ns
|
|
|
|
|
|
|
|
proc getNanosecondTime*(timeInSeconds: float64): Timestamp =
|
|
|
|
let ns = Timestamp(timeInSeconds * float64(1_000_000_000))
|
2022-02-17 15:00:15 +00:00
|
|
|
return ns
|
|
|
|
|
2022-09-30 12:43:42 +00:00
|
|
|
proc nowInUnixFloat(): float =
|
|
|
|
return getTime().toUnixFloat()
|
|
|
|
|
2023-02-03 08:06:21 +00:00
|
|
|
proc getNowInNanosecondTime*(): Timestamp =
|
|
|
|
return getNanosecondTime(nowInUnixFloat())
|
|
|
|
|
2024-09-25 11:08:01 +00:00
|
|
|
template nanosecondTime*(
|
|
|
|
collector: Summary | Histogram | typedesc[IgnoredCollector], body: untyped
|
|
|
|
) =
|
2022-09-30 12:43:42 +00:00
|
|
|
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 09:51:46 +00:00
|
|
|
metrics.set(collector, nowInUnixFloat() - start)
|
2022-09-30 12:43:42 +00:00
|
|
|
else:
|
2023-02-03 08:06:21 +00:00
|
|
|
body
|
2024-08-13 11:27:34 +00:00
|
|
|
|
|
|
|
# Unused yet. Kept for future use in Waku Sync.
|
|
|
|
#[ proc timestampInSeconds*(time: Timestamp): Timestamp =
|
|
|
|
let timeStr = $time
|
|
|
|
var timestamp: Timestamp = time
|
|
|
|
|
|
|
|
if timeStr.len() > 16:
|
|
|
|
timestamp = Timestamp(time div Timestamp(1_000_000_000))
|
|
|
|
elif timeStr.len() < 16 and timeStr.len() > 13:
|
|
|
|
timestamp = Timestamp(time div Timestamp(1_000_000))
|
|
|
|
elif timeStr.len() > 10:
|
|
|
|
timestamp = Timestamp(time div Timestamp(1000))
|
|
|
|
return timestamp ]#
|