mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-04 12:07:51 +00:00
Merge pull request #5002 from goldenreign/time-compare-nosideeffect
Add 'noSideEffect' pragma for Time type's operators. Fixes #4981
This commit is contained in:
@@ -96,7 +96,7 @@ elif defined(JS):
|
||||
getMinutes: proc (): int {.tags: [], raises: [], benign.}
|
||||
getMonth: proc (): int {.tags: [], raises: [], benign.}
|
||||
getSeconds: proc (): int {.tags: [], raises: [], benign.}
|
||||
getTime: proc (): int {.tags: [], raises: [], benign.}
|
||||
getTime: proc (): int {.tags: [], raises: [], noSideEffect, benign.}
|
||||
getTimezoneOffset: proc (): int {.tags: [], raises: [], benign.}
|
||||
getDate: proc (): int {.tags: [], raises: [], benign.}
|
||||
getUTCDate: proc (): int {.tags: [], raises: [], benign.}
|
||||
@@ -207,21 +207,21 @@ proc toSeconds*(time: Time): float {.tags: [], raises: [], benign.}
|
||||
## Returns the time in seconds since the unix epoch.
|
||||
|
||||
proc `-`*(a, b: Time): int64 {.
|
||||
rtl, extern: "ntDiffTime", tags: [], raises: [], benign.}
|
||||
rtl, extern: "ntDiffTime", tags: [], raises: [], noSideEffect, benign.}
|
||||
## computes the difference of two calendar times. Result is in seconds.
|
||||
|
||||
proc `<`*(a, b: Time): bool {.
|
||||
rtl, extern: "ntLtTime", tags: [], raises: [].} =
|
||||
rtl, extern: "ntLtTime", tags: [], raises: [], noSideEffect.} =
|
||||
## returns true iff ``a < b``, that is iff a happened before b.
|
||||
result = a - b < 0
|
||||
|
||||
proc `<=` * (a, b: Time): bool {.
|
||||
rtl, extern: "ntLeTime", tags: [], raises: [].}=
|
||||
rtl, extern: "ntLeTime", tags: [], raises: [], noSideEffect.}=
|
||||
## returns true iff ``a <= b``.
|
||||
result = a - b <= 0
|
||||
|
||||
proc `==`*(a, b: Time): bool {.
|
||||
rtl, extern: "ntEqTime", tags: [], raises: [].} =
|
||||
rtl, extern: "ntEqTime", tags: [], raises: [], noSideEffect.} =
|
||||
## returns true if ``a == b``, that is if both times represent the same value
|
||||
result = a - b == 0
|
||||
|
||||
|
||||
@@ -179,3 +179,14 @@ let dstT1 = parse("2016-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss")
|
||||
let dstT2 = parse("2016-06-01 00:00:00", "yyyy-MM-dd HH:mm:ss")
|
||||
doAssert dstT1 == getLocalTime(toTime(dstT1))
|
||||
doAssert dstT2 == getLocalTime(toTime(dstT2))
|
||||
|
||||
# Comparison between Time objects should be detected by compiler
|
||||
# as 'noSideEffect'.
|
||||
proc cmpTimeNoSideEffect(t1: Time, t2: Time): bool {.noSideEffect.} =
|
||||
result = t1 == t2
|
||||
doAssert cmpTimeNoSideEffect(0.fromSeconds, 0.fromSeconds)
|
||||
# Additionally `==` generic for seq[T] has explicit 'noSideEffect' pragma
|
||||
# so we can check above condition by comparing seq[Time] sequences
|
||||
let seqA: seq[Time] = @[]
|
||||
let seqB: seq[Time] = @[]
|
||||
doAssert seqA == seqB
|
||||
|
||||
Reference in New Issue
Block a user