Added comparison operators for TimeInfo and TimeInterval, cmp for TimeInfo.

This commit is contained in:
Jonathan Bernard 2016-10-07 22:36:44 -05:00
parent 4a8f0db62d
commit 6067740bdd
3 changed files with 90 additions and 4 deletions

View File

@ -1,6 +1,6 @@
path = "."
hints = off
linedir = off
linedir = on
debuginfo
stacktrace = on
linetrace = on

View File

@ -3,6 +3,58 @@ import times, timeutils, unittest
suite "timeutils":
test "format TimeInterval":
let interval = seconds(70)
check interval.format("ss's'") == "10s"
check interval.format("mm'm' ss's'") == "01m 10s"
check interval.format("mm:ss") == "01:10"
check:
interval.format("ss's'") == "10s"
interval.format("mm'm' ss's'") == "01m 10s"
interval.format("mm:ss") == "01:10"
test "TimeInfo difference":
let t1 = getLocalTime(getTime())
let t2 = t1 + 30.seconds
check t2 - t1 == 30.seconds
test "TimeInfo comparisons":
let t1 = getLocalTime(getTime())
check:
t1 < t1 + 10.seconds
not (t1 + 10.seconds < t1)
not (t1 > t1)
t1 + 10.seconds > t1
not (t1 > t1 + 10.seconds)
not (t1 < t1)
t1 + 10.seconds >= t1
t1 >= t1
not (t1 >= t1 + 10.seconds)
t1 <= t1 + 10.seconds
t1 <= t1
not (t1 + 10.seconds <= t1)
test "TimeInterval comparisons":
check:
30.seconds > 10.seconds
not (10.seconds > 30.seconds)
not (10.seconds > 10.seconds)
10.minutes < 1.hours
not (1.hours < 10.minutes)
not (1.hours < 1.hours)
60.seconds >= 1.minutes
60.seconds >= 1.minutes
not (60.seconds >= 2.minutes)
60.seconds <= 1.minutes
60.seconds <= 2.minutes
not (2.minutes <= 60.seconds)
test "TimeInfo cmp":
let t1 = getLocalTime(getTime())
check cmp(t1, t1) == 0
check cmp(t1, t1 + 10.seconds) == -1
check cmp(t1 + 10.seconds, t1) == 1

View File

@ -1,5 +1,39 @@
import times
const zeroTime = fromSeconds(0)
proc format*(ti: TimeInterval, fmt: string): string =
let info = getGMTime(fromSeconds(0) + ti)
return info.format(fmt)
proc `-`*(a, b: TimeInfo): TimeInterval =
return seconds(cast[int](a.toTime - b.toTime))
proc `>`*(a, b: TimeInfo): bool =
return a.toTime > b.toTime
proc `<`*(a, b: TimeInfo): bool =
return a.toTime < b.toTime
proc `>=`*(a, b: TimeInfo): bool =
return a.toTime >= b.toTime
proc `<=`*(a, b: TimeInfo): bool =
return a.toTime <= b.toTime
proc `>`*(a, b: TimeInterval): bool =
return (zeroTime + a) > (zeroTime + b)
proc `<`*(a, b: TimeInterval): bool =
return (zeroTime + a) < (zeroTime + b)
proc `>=`*(a, b: TimeInterval): bool =
return (zeroTime + a) >= (zeroTime + b)
proc `<=`*(a, b: TimeInterval): bool =
return (zeroTime + a) <= (zeroTime + b)
proc cmp(a, b: TimeInfo): int =
if b == a: return 0
elif b > a: return 1
else: return -1