2023-02-21 12:52:30 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
|
|
|
testutils/unittests
|
|
|
|
import
|
2023-08-09 17:11:50 +00:00
|
|
|
../../waku/waku_core/time
|
2023-02-21 12:52:30 +00:00
|
|
|
|
2023-04-19 14:39:52 +00:00
|
|
|
suite "Waku Core - Time":
|
2023-02-21 12:52:30 +00:00
|
|
|
|
|
|
|
test "Test timestamp conversion":
|
|
|
|
## Given
|
|
|
|
let
|
|
|
|
nanoseconds = 1676562429123456789.int64
|
|
|
|
secondsPart = nanoseconds div 1_000_000_000
|
|
|
|
nanosecondsPart = nanoseconds mod 1_000_000_000
|
|
|
|
secondsFloat = secondsPart.float64 + (nanosecondsPart.float64 / 1_000_000_000.float64)
|
|
|
|
lowResTimestamp = Timestamp(secondsPart.int64 * 1_000_000_000.int64) # 1676562429000000000
|
|
|
|
highResTimestamp = Timestamp(secondsFloat * 1_000_000_000.float64) # 1676562429123456789
|
|
|
|
|
|
|
|
require highResTimestamp > lowResTimestamp # Sanity check
|
|
|
|
|
|
|
|
## When
|
|
|
|
let
|
|
|
|
timeInSecondsInt = secondsPart.int
|
|
|
|
timeInSecondsInt64 = secondsPart.int64
|
|
|
|
timeInSecondsFloat = float(secondsFloat)
|
|
|
|
timeInSecondsFloat64 = float64(secondsFloat)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
getNanosecondTime(timeInSecondsInt) == lowResTimestamp
|
|
|
|
getNanosecondTime(timeInSecondsInt64) == lowResTimestamp
|
|
|
|
getNanosecondTime(timeInSecondsFloat) == highResTimestamp
|
|
|
|
getNanosecondTime(timeInSecondsFloat64) == highResTimestamp
|