Improve TimeInterval docs #6135 (#6341)

This commit is contained in:
Federico Ceratto 2017-09-07 07:29:20 +01:00 • committed by Andreas Rumpf
commit ca0de9a54d

View file

@ -222,8 +222,8 @@ proc toSeconds*(time: Time): float {.tags: [], raises: [], benign.}
proc `-`*(a, b: Time): int64 {.
rtl, extern: "ntDiffTime", tags: [], raises: [], noSideEffect, benign.}
## computes the difference of two calendar times. Result is in seconds.
## .. code-block:: nim
##
## .. code-block:: nim
## let a = fromSeconds(1_000_000_000)
## let b = fromSeconds(1_500_000_000)
## echo initInterval(seconds=int(b - a))
@ -324,8 +324,10 @@ proc `-`*(ti: TimeInterval): TimeInterval =
proc `-`*(ti1, ti2: TimeInterval): TimeInterval =
## Subtracts TimeInterval ``ti1`` from ``ti2``.
## .. code-block:: nim
##
## Time components are compared one-by-one, see output:
##
## .. code-block:: nim
## let a = fromSeconds(1_000_000_000)
## let b = fromSeconds(1_500_000_000)
## echo b.toTimeInterval - a.toTimeInterval
@ -1113,6 +1115,16 @@ proc timeToTimeInterval*(t: Time): TimeInterval {.deprecated.} =
proc toTimeInterval*(t: Time): TimeInterval =
## Converts a Time to a TimeInterval.
##
## To be used when diffing times.
##
## .. code-block:: nim
## let a = fromSeconds(1_000_000_000)
## let b = fromSeconds(1_500_000_000)
## echo a, " ", b # real dates
## echo a.toTimeInterval # meaningless value, don't use it by itself
## echo b.toTimeInterval - a.toTimeInterval
## # (milliseconds: 0, seconds: -40, minutes: -6, hours: 1, days: -2, months: -2, years: 16)
# Milliseconds not available from Time
var tInfo = t.getLocalTime()
initInterval(0, tInfo.second, tInfo.minute, tInfo.hour, tInfo.weekday.ord, tInfo.month.ord, tInfo.year)