Merge pull request #7456 from data-man/cmp_datetime
Comparing datetimes
This commit is contained in:
commit
92d2ce01d6
2 changed files with 21 additions and 1 deletions
|
|
@ -274,6 +274,18 @@ proc toTime*(dt: DateTime): Time {.tags: [], raises: [], benign.} =
|
||||||
# so we need to compensate for that here.
|
# so we need to compensate for that here.
|
||||||
result.inc dt.utcOffset
|
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 =
|
proc initDateTime(zt: ZonedTime, zone: Timezone): DateTime =
|
||||||
let adjTime = zt.adjTime.int64
|
let adjTime = zt.adjTime.int64
|
||||||
let epochday = (if adjTime >= 0: adjTime else: adjTime - (secondsInDay - 1)) div secondsInDay
|
let epochday = (if adjTime >= 0: adjTime else: adjTime - (secondsInDay - 1)) div secondsInDay
|
||||||
|
|
|
||||||
|
|
@ -367,4 +367,12 @@ suite "ttimes":
|
||||||
check $(dt - 1.months) == "2017-02-15T00:00:00+00:00"
|
check $(dt - 1.months) == "2017-02-15T00:00:00+00:00"
|
||||||
dt = initDateTime(31, mMar, 2017, 00, 00, 00, utc())
|
dt = initDateTime(31, mMar, 2017, 00, 00, 00, utc())
|
||||||
# This happens due to monthday overflow. It's consistent with Phobos.
|
# This happens due to monthday overflow. It's consistent with Phobos.
|
||||||
check $(dt - 1.months) == "2017-03-03T00:00:00+00:00"
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue