Skip to content

Commit e11ecc8

Browse files
timotheecourAraq
authored andcommitted
times: toUnixFloat, fromUnixFloat (#13044)
1 parent 5010bc1 commit e11ecc8

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353

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

56+
- Added `times.fromUnixFloat,toUnixFloat`, subsecond resolution versions of `fromUnix`,`toUnixFloat`.
57+
5658
## Library changes
5759

5860
- `asyncdispatch.drain` now properly takes into account `selector.hasPendingOperations`

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: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,33 @@ proc fromUnix*(unix: int64): Time
590590
591591
proc toUnix*(t: Time): int64 {.benign, tags: [], raises: [], noSideEffect.} =
592592
## Convert ``t`` to a unix timestamp (seconds since ``1970-01-01T00:00:00Z``).
593+
## See also `toUnixFloat` for subsecond resolution.
593594
runnableExamples:
594595
doAssert fromUnix(0).toUnix() == 0
595596
t.seconds
596597
598+
proc fromUnixFloat(seconds: float): Time {.benign, tags: [], raises: [], noSideEffect.} =
599+
## Convert a unix timestamp in seconds to a `Time`; same as `fromUnix`
600+
## but with subsecond resolution.
601+
runnableExamples:
602+
doAssert fromUnixFloat(123456.0) == fromUnixFloat(123456)
603+
doAssert fromUnixFloat(-123456.0) == fromUnixFloat(-123456)
604+
let secs = seconds.floor
605+
let nsecs = (seconds - secs) * 1e9
606+
initTime(secs.int64, nsecs.NanosecondRange)
607+
608+
proc toUnixFloat(t: Time): float {.benign, tags: [], raises: [].} =
609+
## Same as `toUnix` but using subsecond resolution.
610+
runnableExamples:
611+
let t = getTime()
612+
# `<` because of rounding errors
613+
doAssert abs(t.toUnixFloat().fromUnixFloat - t) < initDuration(nanoseconds = 1000)
614+
t.seconds.float + t.nanosecond / convert(Seconds, Nanoseconds, 1)
615+
616+
since((1, 1)):
617+
export fromUnixFloat
618+
export toUnixFloat
619+
597620
proc fromWinTime*(win: int64): Time =
598621
## Convert a Windows file time (100-nanosecond intervals since
599622
## ``1601-01-01T00:00:00Z``) to a ``Time``.
@@ -2685,14 +2708,12 @@ proc initInterval*(seconds, minutes, hours, days, months, years: int = 0):
26852708
initTimeInterval(0, 0, 0, seconds, minutes, hours, days, 0, months, years)
26862709

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

26972718
proc fromSeconds*(since1970: int64): Time
26982719
{.tags: [], raises: [], benign, deprecated.} =
@@ -2703,11 +2724,9 @@ proc fromSeconds*(since1970: int64): Time
27032724
fromUnix(since1970)
27042725

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

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

0 commit comments

Comments
 (0)