nwaku/waku/waku_core/time.nim
Simon-Pierre Vivier 2cc86c51da
feat: Nwaku Sync (#2403)
* feat: Waku Sync Protocol

* feat: state machine (#2656)

* feat: pruning storage mehcanism (#2673)

* feat: message transfer mechanism & tests (#2688)

* update docker files

* added ENR filed for sync & misc. fixes

* adding new sync range param & fixes

---------

Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com>
Co-authored-by: Prem Chaitanya Prathi <chaitanyaprem@gmail.com>
2024-08-13 07:27:34 -04:00

49 lines
1.4 KiB
Nim

{.push raises: [].}
import std/times, metrics
type Timestamp* = int64 # A nanosecond precision timestamp
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))
return ns
proc nowInUnixFloat(): float =
return getTime().toUnixFloat()
proc getNowInNanosecondTime*(): Timestamp =
return getNanosecondTime(nowInUnixFloat())
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
# 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 ]#