Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

times: toUnixFloat, fromUnixFloat #13044

Merged
merged 6 commits into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@

- Added `os.normalizePathEnd` for additional path sanitization.

- Added `times.fromUnixFloat,toUnixFloat`, subsecond resolution versions of `fromUnix`,`toUnixFloat`.

## Library changes

- `asyncdispatch.drain` now properly takes into account `selector.hasPendingOperations`
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ proc normalizePathEnd(path: string, trailingSep = false): string =
result = path
result.normalizePathEnd(trailingSep)

when (NimMajor, NimMinor) >= (1, 1):
since((1, 1)):
export normalizePathEnd

proc joinPath*(head, tail: string): string {.
Expand Down
37 changes: 28 additions & 9 deletions lib/pure/times.nim
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,33 @@ proc fromUnix*(unix: int64): Time

proc toUnix*(t: Time): int64 {.benign, tags: [], raises: [], noSideEffect.} =
## Convert ``t`` to a unix timestamp (seconds since ``1970-01-01T00:00:00Z``).
## See also `toUnixFloat` for subsecond resolution.
runnableExamples:
doAssert fromUnix(0).toUnix() == 0
t.seconds

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(123456.0) == fromUnixFloat(123456)
doAssert fromUnixFloat(-123456.0) == fromUnixFloat(-123456)
let secs = seconds.floor
let nsecs = (seconds - secs) * 1e9
initTime(secs.int64, nsecs.NanosecondRange)

proc toUnixFloat(t: Time): float {.benign, tags: [], raises: [].} =
## Same as `toUnix` but using subsecond resolution.
runnableExamples:
let t = getTime()
# `<` because of rounding errors
doAssert abs(t.toUnixFloat().fromUnixFloat - t) < initDuration(nanoseconds = 1000)
t.seconds.float + t.nanosecond / convert(Seconds, Nanoseconds, 1)

since((1, 1)):
timotheecour marked this conversation as resolved.
Show resolved Hide resolved
export fromUnixFloat
export toUnixFloat

proc fromWinTime*(win: int64): Time =
## Convert a Windows file time (100-nanosecond intervals since
## ``1601-01-01T00:00:00Z``) to a ``Time``.
Expand Down Expand Up @@ -2685,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 All @@ -2703,11 +2724,9 @@ proc fromSeconds*(since1970: int64): Time
fromUnix(since1970)

proc toSeconds*(time: Time): float
{.tags: [], raises: [], benign, deprecated.} =
## Returns the time in seconds since the unix epoch.
##
## **Deprecated since v0.18.0:** use ``toUnix`` instead
time.seconds.float + time.nanosecond / convert(Seconds, Nanoseconds, 1)
{.tags: [], raises: [], benign, deprecated: "Use toUnixFloat or toUnix".} =
## Returns the time in seconds since the unix epoch, with subsecond resolution.
toUnixFloat(time)

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