@@ -590,10 +590,33 @@ 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 `toUnixFloat` for subsecond resolution.
593
594
runnableExamples:
594
595
doAssert fromUnix(0).toUnix() == 0
595
596
t.seconds
596
597
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) * 1e9
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
+
597
620
proc fromWinTime*(win: int64 ): Time =
598
621
## Convert a Windows file time (100-nanosecond intervals since
599
622
## ``1601-01-01T00:00:00Z``) to a ``Time``.
@@ -2685,14 +2708,12 @@ proc initInterval*(seconds, minutes, hours, days, months, years: int = 0):
2685
2708
initTimeInterval(0 , 0 , 0 , seconds, minutes, hours, days, 0 , months, years)
2686
2709
2687
2710
proc fromSeconds* (since1970: float ): Time
2688
- {.tags: [], raises: [], benign, deprecated.} =
2711
+ {.tags: [], raises: [], benign, deprecated: " Use fromUnixFloat or fromUnix " .} =
2689
2712
# # Takes a float which contains the number of seconds since the unix epoch and
2690
2713
# # returns a time object.
2691
2714
# #
2692
2715
# # **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)
2696
2717
2697
2718
proc fromSeconds* (since1970: int64 ): Time
2698
2719
{.tags: [], raises: [], benign, deprecated.} =
@@ -2703,11 +2724,9 @@ proc fromSeconds*(since1970: int64): Time
2703
2724
fromUnix(since1970)
2704
2725
2705
2726
proc 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)
2711
2730
2712
2731
proc getLocalTime* (time: Time): DateTime
2713
2732
{.tags: [], raises: [], benign, deprecated.} =
0 commit comments