diff options
author | Dominik Picheta <dominikpicheta@googlemail.com> | 2018-04-01 17:02:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-01 17:02:23 +0100 |
commit | 92d2ce01d6a466ff6a98125ec729de26365c51ab (patch) | |
tree | 596d79ed7ff787f8a992ef64ca225fdb891bad9d | |
parent | e4e6b1f4962ebf7148a5ff0471f44df45f88eec8 (diff) | |
parent | 759023e157806bf9cdeb9f442002367a55fb8698 (diff) | |
download | Nim-92d2ce01d6a466ff6a98125ec729de26365c51ab.tar.gz |
Merge pull request #7456 from data-man/cmp_datetime
Comparing datetimes
-rw-r--r-- | lib/pure/times.nim | 12 | ||||
-rw-r--r-- | tests/stdlib/ttimes.nim | 10 |
2 files changed, 21 insertions, 1 deletions
diff --git a/lib/pure/times.nim b/lib/pure/times.nim index 6c1e1fe87..75b5bac43 100644 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -274,6 +274,18 @@ proc toTime*(dt: DateTime): Time {.tags: [], raises: [], benign.} = # so we need to compensate for that here. result.inc dt.utcOffset +proc `<`*(a, b: DateTime): bool = + ## Returns true iff ``a < b``, that is iff a happened before b. + return a.toTime < b.toTime + +proc `<=` * (a, b: DateTime): bool = + ## Returns true iff ``a <= b``. + return a.toTime <= b.toTime + +proc `==`*(a, b: DateTime): bool = + ## Returns true if ``a == b``, that is if both dates represent the same point in datetime. + return a.toTime == b.toTime + proc initDateTime(zt: ZonedTime, zone: Timezone): DateTime = let adjTime = zt.adjTime.int64 let epochday = (if adjTime >= 0: adjTime else: adjTime - (secondsInDay - 1)) div secondsInDay diff --git a/tests/stdlib/ttimes.nim b/tests/stdlib/ttimes.nim index ae056a79f..8efc50086 100644 --- a/tests/stdlib/ttimes.nim +++ b/tests/stdlib/ttimes.nim @@ -367,4 +367,12 @@ suite "ttimes": check $(dt - 1.months) == "2017-02-15T00:00:00+00:00" dt = initDateTime(31, mMar, 2017, 00, 00, 00, utc()) # This happens due to monthday overflow. It's consistent with Phobos. - check $(dt - 1.months) == "2017-03-03T00:00:00+00:00" \ No newline at end of file + check $(dt - 1.months) == "2017-03-03T00:00:00+00:00" + + test "compare datetimes": + var dt1 = now() + var dt2 = dt1 + check dt1 == dt2 + check dt1 <= dt2 + dt2 = dt2 + 1.seconds + check dt1 < dt2 |