@@ -590,27 +590,31 @@ proc fromUnix*(unix: int64): Time
590
590
591
591
proc toUnix*(t: Time): int64 {.benign, tags: [], raises: [], noSideEffect.} =
592
592
## Convert ``t`` to a unix timestamp (seconds since ``1970-01-01T00:00:00Z``).
593
- ## See also `toUnix` .
593
+ ## See also `toUnixFloat` for subsecond resolution .
594
594
runnableExamples:
595
595
doAssert fromUnix(0).toUnix() == 0
596
596
t.seconds
597
597
598
- proc fromUnix*(unix : float ): Time
598
+ proc fromUnixFloat*(seconds : float ): Time
599
599
{.benign, tags: [], raises: [], noSideEffect, since: (1, 1).} =
600
- ## Overload working with subsecond resolution.
600
+ ## Convert a unix timestamp in seconds to a `Time`; same as `fromUnix`
601
+ ## but with subsecond resolution.
601
602
runnableExamples:
602
- doAssert fromUnix (123.0) == fromUnix (123)
603
- let secs = unix .floor
604
- let nsecs = (unix - secs) * 1e9
603
+ doAssert fromUnixFloat (123.0) == fromUnixFloat (123)
604
+ let secs = seconds .floor
605
+ let nsecs = (seconds - secs) * 1e9
605
606
initTime(secs.int64 , nsecs.NanosecondRange)
606
607
607
- proc toUnixFloat*(t: Time): float {.benign, tags: [], raises: [].} =
608
+ template toUnixFloatImpl(t): untyped =
609
+ t.seconds.float + t.nanosecond / convert(Seconds, Nanoseconds, 1)
610
+
611
+ proc toUnixFloat*(t: Time): float {.benign, tags: [], raises: [], since: (1, 1).} =
608
612
## Same as `toUnix` but using subsecond resolution.
609
613
runnableExamples:
610
614
let t = getTime()
611
615
# `<` because of rounding errors
612
- doAssert abs(t.toUnixFloat().fromUnix - t) < initDuration(nanoseconds = 1000)
613
- t.seconds. float + t.nanosecond / convert(Seconds, Nanoseconds, 1 )
616
+ doAssert abs(t.toUnixFloat().fromUnixFloat - t) < initDuration(nanoseconds = 1000)
617
+ toUnixFloatImpl(t )
614
618
615
619
proc fromWinTime*(win: int64 ): Time =
616
620
## Convert a Windows file time (100-nanosecond intervals since
@@ -2723,7 +2727,7 @@ proc fromSeconds*(since1970: int64): Time
2723
2727
proc toSeconds* (time: Time): float
2724
2728
{.tags: [], raises: [], benign, deprecated: " Use toUnixFloat or toUnix" .} =
2725
2729
# # Returns the time in seconds since the unix epoch, with subsecond resolution.
2726
- toUnixFloat (time)
2730
+ toUnixFloatImpl (time)
2727
2731
2728
2732
proc getLocalTime* (time: Time): DateTime
2729
2733
{.tags: [], raises: [], benign, deprecated.} =
0 commit comments