Skip to content

Commit 7d8f239

Browse files
committed
bugfix in deprecated fromSeconds: does not crash for negative input
1 parent a255b9e commit 7d8f239

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

lib/pure/os.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ proc normalizePathEnd(path: string, trailingSep = false): string =
112112
result = path
113113
result.normalizePathEnd(trailingSep)
114114

115-
when (NimMajor, NimMinor) >= (1, 1):
115+
since((1, 1)):
116116
export normalizePathEnd
117117

118118
proc joinPath*(head, tail: string): string {.

lib/pure/times.nim

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -595,26 +595,27 @@ 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)
607607
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).} =
608+
proc toUnixFloat(t: Time): float {.benign, tags: [], raises: [].} =
612609
## Same as `toUnix` but using subsecond resolution.
613610
runnableExamples:
614611
let t = getTime()
615612
# `<` because of rounding errors
616613
doAssert abs(t.toUnixFloat().fromUnixFloat - t) < initDuration(nanoseconds = 1000)
617-
toUnixFloatImpl(t)
614+
t.seconds.float + t.nanosecond / convert(Seconds, Nanoseconds, 1)
615+
616+
since((1, 1)):
617+
export fromUnixFloat
618+
export toUnixFloat
618619
619620
proc fromWinTime*(win: int64): Time =
620621
## Convert a Windows file time (100-nanosecond intervals since
@@ -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.} =
@@ -2727,7 +2726,7 @@ proc fromSeconds*(since1970: int64): Time
27272726
proc toSeconds*(time: Time): float
27282727
{.tags: [], raises: [], benign, deprecated: "Use toUnixFloat or toUnix".} =
27292728
## Returns the time in seconds since the unix epoch, with subsecond resolution.
2730-
toUnixFloatImpl(time)
2729+
toUnixFloat(time)
27312730

27322731
proc getLocalTime*(time: Time): DateTime
27332732
{.tags: [], raises: [], benign, deprecated.} =

0 commit comments

Comments
 (0)