@@ -590,27 +590,31 @@ proc fromUnix*(unix: int64): Time
590590
591591proc toUnix * (t: Time ): int64 {.benign , tags : [], raises : [], noSideEffect .} =
592592 # # Convert ``t`` to a unix timestamp (seconds since ``1970-01-01T00:00:00Z``).
593- # # See also `toUnix` .
593+ # # See also `toUnixFloat` for subsecond resolution .
594594 runnableExamples:
595595 doAssert fromUnix (0 ).toUnix () == 0
596596 t.seconds
597597
598- proc fromUnix * (unix : float ): Time
598+ proc fromUnixFloat * (seconds : float ): Time
599599 {.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.
601602 runnableExamples:
602- doAssert fromUnix (123.0 ) == fromUnix (123 )
603- let secs = unix .floor
604- let nsecs = (unix - secs) * 1 e9
603+ doAssert fromUnixFloat (123.0 ) == fromUnixFloat (123 )
604+ let secs = seconds .floor
605+ let nsecs = (seconds - secs) * 1 e9
605606 initTime (secs.int64 , nsecs.NanosecondRange )
606607
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 ).} =
608612 # # Same as `toUnix` but using subsecond resolution.
609613 runnableExamples:
610614 let t = getTime ()
611615 # `<` 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 )
614618
615619proc fromWinTime * (win: int64 ): Time =
616620 # # Convert a Windows file time (100-nanosecond intervals since
@@ -2723,7 +2727,7 @@ proc fromSeconds*(since1970: int64): Time
27232727proc toSeconds * (time: Time ): float
27242728 {.tags : [], raises : [], benign , deprecated : " Use toUnixFloat or toUnix" .} =
27252729 # # Returns the time in seconds since the unix epoch, with subsecond resolution.
2726- toUnixFloat (time)
2730+ toUnixFloatImpl (time)
27272731
27282732proc getLocalTime * (time: Time ): DateTime
27292733 {.tags : [], raises : [], benign , deprecated .} =
0 commit comments