times: toUnixFloat, fromUnixFloat (#13044)
This commit is contained in:
parent
5010bc1af5
commit
e11ecc8266
3 changed files with 31 additions and 10 deletions
|
|
@ -53,6 +53,8 @@
|
||||||
|
|
||||||
- Added `os.normalizePathEnd` for additional path sanitization.
|
- Added `os.normalizePathEnd` for additional path sanitization.
|
||||||
|
|
||||||
|
- Added `times.fromUnixFloat,toUnixFloat`, subsecond resolution versions of `fromUnix`,`toUnixFloat`.
|
||||||
|
|
||||||
## Library changes
|
## Library changes
|
||||||
|
|
||||||
- `asyncdispatch.drain` now properly takes into account `selector.hasPendingOperations`
|
- `asyncdispatch.drain` now properly takes into account `selector.hasPendingOperations`
|
||||||
|
|
|
||||||
|
|
@ -112,7 +112,7 @@ proc normalizePathEnd(path: string, trailingSep = false): string =
|
||||||
result = path
|
result = path
|
||||||
result.normalizePathEnd(trailingSep)
|
result.normalizePathEnd(trailingSep)
|
||||||
|
|
||||||
when (NimMajor, NimMinor) >= (1, 1):
|
since((1, 1)):
|
||||||
export normalizePathEnd
|
export normalizePathEnd
|
||||||
|
|
||||||
proc joinPath*(head, tail: string): string {.
|
proc joinPath*(head, tail: string): string {.
|
||||||
|
|
|
||||||
|
|
@ -590,10 +590,33 @@ proc fromUnix*(unix: int64): Time
|
||||||
|
|
||||||
proc toUnix*(t: Time): int64 {.benign, tags: [], raises: [], noSideEffect.} =
|
proc toUnix*(t: Time): int64 {.benign, tags: [], raises: [], noSideEffect.} =
|
||||||
## Convert ``t`` to a unix timestamp (seconds since ``1970-01-01T00:00:00Z``).
|
## Convert ``t`` to a unix timestamp (seconds since ``1970-01-01T00:00:00Z``).
|
||||||
|
## See also `toUnixFloat` for subsecond resolution.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
doAssert fromUnix(0).toUnix() == 0
|
doAssert fromUnix(0).toUnix() == 0
|
||||||
t.seconds
|
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)):
|
||||||
|
export fromUnixFloat
|
||||||
|
export toUnixFloat
|
||||||
|
|
||||||
proc fromWinTime*(win: int64): Time =
|
proc fromWinTime*(win: int64): Time =
|
||||||
## Convert a Windows file time (100-nanosecond intervals since
|
## Convert a Windows file time (100-nanosecond intervals since
|
||||||
## ``1601-01-01T00:00:00Z``) to a ``Time``.
|
## ``1601-01-01T00:00:00Z``) to a ``Time``.
|
||||||
|
|
@ -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)
|
initTimeInterval(0, 0, 0, seconds, minutes, hours, days, 0, months, years)
|
||||||
|
|
||||||
proc fromSeconds*(since1970: float): Time
|
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
|
## Takes a float which contains the number of seconds since the unix epoch and
|
||||||
## returns a time object.
|
## returns a time object.
|
||||||
##
|
##
|
||||||
## **Deprecated since v0.18.0:** use ``fromUnix`` instead
|
## **Deprecated since v0.18.0:** use ``fromUnix`` instead
|
||||||
let nanos = ((since1970 - since1970.int64.float) *
|
fromUnixFloat(since1970)
|
||||||
convert(Seconds, Nanoseconds, 1).float).int
|
|
||||||
initTime(since1970.int64, nanos)
|
|
||||||
|
|
||||||
proc fromSeconds*(since1970: int64): Time
|
proc fromSeconds*(since1970: int64): Time
|
||||||
{.tags: [], raises: [], benign, deprecated.} =
|
{.tags: [], raises: [], benign, deprecated.} =
|
||||||
|
|
@ -2703,11 +2724,9 @@ proc fromSeconds*(since1970: int64): Time
|
||||||
fromUnix(since1970)
|
fromUnix(since1970)
|
||||||
|
|
||||||
proc toSeconds*(time: Time): float
|
proc toSeconds*(time: Time): float
|
||||||
{.tags: [], raises: [], benign, deprecated.} =
|
{.tags: [], raises: [], benign, deprecated: "Use toUnixFloat or toUnix".} =
|
||||||
## Returns the time in seconds since the unix epoch.
|
## Returns the time in seconds since the unix epoch, with subsecond resolution.
|
||||||
##
|
toUnixFloat(time)
|
||||||
## **Deprecated since v0.18.0:** use ``toUnix`` instead
|
|
||||||
time.seconds.float + time.nanosecond / convert(Seconds, Nanoseconds, 1)
|
|
||||||
|
|
||||||
proc getLocalTime*(time: Time): DateTime
|
proc getLocalTime*(time: Time): DateTime
|
||||||
{.tags: [], raises: [], benign, deprecated.} =
|
{.tags: [], raises: [], benign, deprecated.} =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue