2024-06-28 16:04:57 +05:30
|
|
|
{.push raises: [].}
|
2022-02-17 16:00:15 +01:00
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
import std/times, metrics
|
2022-02-17 16:00:15 +01:00
|
|
|
|
2023-02-21 14:52:30 +02:00
|
|
|
type Timestamp* = int64 # A nanosecond precision timestamp
|
2022-02-17 16:00:15 +01:00
|
|
|
|
2024-05-21 21:00:22 -04: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 16:00:15 +01:00
|
|
|
return ns
|
|
|
|
|
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
|
2024-08-13 07:27:34 -04: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 ]#
|