Skip to content

Commit

Permalink
bugfix in deprecated fromSeconds: does not crash for negative input
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jan 15, 2020
1 parent ec685e2 commit dc2eb3f
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/pure/times.nim
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,12 @@ proc toUnix*(t: Time): int64 {.benign, tags: [], raises: [], noSideEffect.} =
doAssert fromUnix(0).toUnix() == 0
t.seconds

proc fromUnixFloat*(seconds: float): Time
{.benign, tags: [], raises: [], noSideEffect, since: (1, 1).} =
proc fromUnixFloat(seconds: float): Time {.benign, tags: [], raises: [], noSideEffect.} =
## Convert a unix timestamp in seconds to a `Time`; same as `fromUnix`
## but with subsecond resolution.
runnableExamples:
doAssert fromUnixFloat(123.0) == fromUnixFloat(123)
doAssert fromUnixFloat(123456.0) == fromUnixFloat(123456)
doAssert fromUnixFloat(-123456.0) == fromUnixFloat(-123456)
let secs = seconds.floor
let nsecs = (seconds - secs) * 1e9
initTime(secs.int64, nsecs.NanosecondRange)
Expand All @@ -614,6 +614,7 @@ proc toUnixFloat(t: Time): float {.benign, tags: [], raises: [].} =
t.seconds.float + t.nanosecond / convert(Seconds, Nanoseconds, 1)

since((1, 1)):
export fromUnixFloat
export toUnixFloat

proc fromWinTime*(win: int64): Time =
Expand Down Expand Up @@ -2707,14 +2708,12 @@ proc initInterval*(seconds, minutes, hours, days, months, years: int = 0):
initTimeInterval(0, 0, 0, seconds, minutes, hours, days, 0, months, years)

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

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

0 comments on commit dc2eb3f

Please sign in to comment.