@@ -590,10 +590,33 @@ 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 `toUnixFloat` for subsecond resolution.
593594 runnableExamples:
594595 doAssert fromUnix (0 ).toUnix () == 0
595596 t.seconds
596597
598+ proc fromUnixFloat (seconds: float ): Time {.benign , tags : [], raises : [], noSideEffect .} =
599+ # # Convert a unix timestamp in seconds to a `Time`; same as `fromUnix`
600+ # # but with subsecond resolution.
601+ runnableExamples:
602+ doAssert fromUnixFloat (123456.0 ) == fromUnixFloat (123456 )
603+ doAssert fromUnixFloat (- 123456.0 ) == fromUnixFloat (- 123456 )
604+ let secs = seconds.floor
605+ let nsecs = (seconds - secs) * 1 e9
606+ initTime (secs.int64 , nsecs.NanosecondRange )
607+
608+ proc toUnixFloat (t: Time ): float {.benign , tags : [], raises : [].} =
609+ # # Same as `toUnix` but using subsecond resolution.
610+ runnableExamples:
611+ let t = getTime ()
612+ # `<` because of rounding errors
613+ doAssert abs (t.toUnixFloat ().fromUnixFloat - t) < initDuration (nanoseconds = 1000 )
614+ t.seconds.float + t.nanosecond / convert (Seconds , Nanoseconds , 1 )
615+
616+ since ((1 , 1 )):
617+ export fromUnixFloat
618+ export toUnixFloat
619+
597620proc fromWinTime * (win: int64 ): Time =
598621 # # Convert a Windows file time (100-nanosecond intervals since
599622 # # ``1601-01-01T00:00:00Z``) to a ``Time``.
@@ -2685,14 +2708,12 @@ proc initInterval*(seconds, minutes, hours, days, months, years: int = 0):
26852708 initTimeInterval (0 , 0 , 0 , seconds, minutes, hours, days, 0 , months, years)
26862709
26872710proc fromSeconds * (since1970: float ): Time
2688- {.tags : [], raises : [], benign , deprecated .} =
2711+ {.tags : [], raises : [], benign , deprecated : " Use fromUnixFloat or fromUnix " .} =
26892712 # # Takes a float which contains the number of seconds since the unix epoch and
26902713 # # returns a time object.
26912714 # #
26922715 # # **Deprecated since v0.18.0:** use ``fromUnix`` instead
2693- let nanos = ((since1970 - since1970.int64 .float ) *
2694- convert (Seconds , Nanoseconds , 1 ).float ).int
2695- initTime (since1970.int64 , nanos)
2716+ fromUnixFloat (since1970)
26962717
26972718proc fromSeconds * (since1970: int64 ): Time
26982719 {.tags : [], raises : [], benign , deprecated .} =
@@ -2703,11 +2724,9 @@ proc fromSeconds*(since1970: int64): Time
27032724 fromUnix (since1970)
27042725
27052726proc toSeconds * (time: Time ): float
2706- {.tags : [], raises : [], benign , deprecated .} =
2707- # # Returns the time in seconds since the unix epoch.
2708- # #
2709- # # **Deprecated since v0.18.0:** use ``toUnix`` instead
2710- time.seconds.float + time.nanosecond / convert (Seconds , Nanoseconds , 1 )
2727+ {.tags : [], raises : [], benign , deprecated : " Use toUnixFloat or toUnix" .} =
2728+ # # Returns the time in seconds since the unix epoch, with subsecond resolution.
2729+ toUnixFloat (time)
27112730
27122731proc getLocalTime * (time: Time ): DateTime
27132732 {.tags : [], raises : [], benign , deprecated .} =
0 commit comments