132 lines
3.8 KiB
Nim
132 lines
3.8 KiB
Nim
import times, unittest
|
|
import "../timeutils"
|
|
|
|
let dtFormat = "yyyy-MM-dd HH:mm:ss"
|
|
|
|
suite "timeutils":
|
|
test "format TimeInterval":
|
|
let interval = seconds(70)
|
|
check:
|
|
interval.format("ss's'") == "10s"
|
|
interval.format("mm'm' ss's'") == "01m 10s"
|
|
interval.format("mm:ss") == "01:10"
|
|
|
|
test "DateTime difference":
|
|
var t1 = getTime().local
|
|
var t2 = t1 + 30.seconds
|
|
check (t2 - t1) == 30.seconds
|
|
|
|
t1 = parse("2016-10-10 09:45:00", "yyyy-MM-dd HH:mm:ss")
|
|
t2 = parse("2016-10-11 09:45:00", "yyyy-MM-dd HH:mm:ss")
|
|
check t2 - t1 == seconds(24 * 60 * 60)
|
|
|
|
t2 = parse("2016-10-11 10:00:00", "yyyy-MM-dd HH:mm:ss")
|
|
check t2 - t1 == seconds((24 * 60 + 15) * 60)
|
|
|
|
test "DateTime comparisons":
|
|
let t1 = getTime().local
|
|
|
|
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)
|
|
|
|
t1.between(t1 - 10.seconds, t1 + 10.seconds)
|
|
t1.between(t1, t1 + 10.seconds) # start is inclusive
|
|
not t1.between(t1 - 10.seconds, t1) # end is exclusive
|
|
|
|
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 "DateTime cmp":
|
|
let t1 = getTime().local
|
|
|
|
check cmp(t1, t1) == 0
|
|
check cmp(t1, t1 + 10.seconds) == -1
|
|
check cmp(t1 + 10.seconds, t1) == 1
|
|
|
|
test "startOfDay":
|
|
let t1 = parse("13:42:19", "HH:mm:ss")
|
|
let t2 = parse("2015-12-31 23:59:59", dtFormat)
|
|
|
|
check:
|
|
parse("00:00:00", "HH:mm:ss") == startOfDay(t1)
|
|
|
|
#check parse("2015-12-31 00:00:00", dtFormat) == startOfDay(t2)
|
|
|
|
startOfDay(startOfDay(t1)) == startOfDay(t1)
|
|
|
|
test "startOfWeek":
|
|
let t1 = parse("2015-12-31 23:59:59", dtFormat)
|
|
let t2 = parse("2015-12-26 23:59:59", dtFormat)
|
|
let t3 = parse("2016-01-01 23:59:59", dtFormat)
|
|
|
|
# Not parsing the start of the day in order to work around the bug
|
|
# mentioned above.
|
|
check:
|
|
# Start of week = Monday
|
|
startOfWeek(t1) == startOfDay(toTime(parse("2015-12-28 12:01:00", dtFormat)).local)
|
|
startOfWeek(t1).weekday == dMon
|
|
startOfWeek(startOfWeek(t1)) == startOfWeek(t1)
|
|
|
|
startOfWeek(t2) == startOfDay(parse("2015-12-21 01:00:00", dtFormat))
|
|
startOfWeek(t3) == startOfDay(parse("2015-12-28 01:00:00", dtFormat))
|
|
|
|
# Start of week = Sunday
|
|
startOfWeek(t1, dSun) == startOfDay(parse("2015-12-27 01:00:00", dtFormat))
|
|
startOfWeek(t1, dSun).weekday == dSun
|
|
startOfWeek(startOfWeek(t1, dSun), dSun) == startOfWeek(t1, dSun)
|
|
|
|
startOfWeek(t2, dSun) == startOfDay(parse("2015-12-20 01:00:00", dtFormat))
|
|
startOfWeek(t3, dSun) == startOfDay(parse("2015-12-27 01:00:00", dtFormat))
|
|
|
|
test "fixedParse":
|
|
let t1 = fixedParse("2015-12-01 12:00:00", dtFormat)
|
|
let t2 = fixedParse("2015-06-01 12:00:00", dtFormat)
|
|
|
|
check: # test both in DST and out of DST
|
|
t1 == toTime(t1).local
|
|
t2 == toTime(t2).local
|
|
|
|
test "parseIso8601":
|
|
let t1 = parseIso8601("2018-01-01T12:00:00-05:00")
|
|
let t2 = parseIso8601("2018-01-01T17:00:00Z")
|
|
|
|
check:
|
|
t1 == t2
|
|
|
|
test "formatIso8601":
|
|
let t1 = parseIso8601("2018-01-01T12:00:00-05:00")
|
|
let t2 = parseIso8601("2018-01-01T17:00:00Z")
|
|
|
|
check:
|
|
t1 == parseIso8601(formatIso8601(t1))
|
|
t2 == parseIso8601(formatIso8601(t2))
|