Skip to content

Commit dc2eb3f

Browse files
committed
bugfix in deprecated fromSeconds: does not crash for negative input
1 parent ec685e2 commit dc2eb3f

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

lib/pure/times.nim

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,12 @@ proc toUnix*(t: Time): int64 {.benign, tags: [], raises: [], noSideEffect.} =
595595
doAssert fromUnix(0).toUnix() == 0
596596
t.seconds
597597
598-
proc fromUnixFloat*(seconds: float): Time
599-
{.benign, tags: [], raises: [], noSideEffect, since: (1, 1).} =
598+
proc fromUnixFloat(seconds: float): Time {.benign, tags: [], raises: [], noSideEffect.} =
600599
## Convert a unix timestamp in seconds to a `Time`; same as `fromUnix`
601600
## but with subsecond resolution.
602601
runnableExamples:
603-
doAssert fromUnixFloat(123.0) == fromUnixFloat(123)
602+
doAssert fromUnixFloat(123456.0) == fromUnixFloat(123456)
603+
doAssert fromUnixFloat(-123456.0) == fromUnixFloat(-123456)
604604
let secs = seconds.floor
605605
let nsecs = (seconds - secs) * 1e9
606606
initTime(secs.int64, nsecs.NanosecondRange)
@@ -614,6 +614,7 @@ proc toUnixFloat(t: Time): float {.benign, tags: [], raises: [].} =
614614
t.seconds.float + t.nanosecond / convert(Seconds, Nanoseconds, 1)
615615
616616
since((1, 1)):
617+
export fromUnixFloat
617618
export toUnixFloat
618619
619620
proc fromWinTime*(win: int64): Time =
@@ -2707,14 +2708,12 @@ proc initInterval*(seconds, minutes, hours, days, months, years: int = 0):
27072708
initTimeInterval(0, 0, 0, seconds, minutes, hours, days, 0, months, years)
27082709

27092710
proc fromSeconds*(since1970: float): Time
2710-
{.tags: [], raises: [], benign, deprecated.} =
2711+
{.tags: [], raises: [], benign, deprecated: "Use fromUnixFloat or fromUnix".} =
27112712
## Takes a float which contains the number of seconds since the unix epoch and
27122713
## returns a time object.
27132714
##
27142715
## **Deprecated since v0.18.0:** use ``fromUnix`` instead
2715-
let nanos = ((since1970 - since1970.int64.float) *
2716-
convert(Seconds, Nanoseconds, 1).float).int
2717-
initTime(since1970.int64, nanos)
2716+
fromUnixFloat(since1970)
27182717

27192718
proc fromSeconds*(since1970: int64): Time
27202719
{.tags: [], raises: [], benign, deprecated.} =

0 commit comments

Comments
 (0)