Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
a9308cbaf3 | |||
f4e392f910 | |||
600747e1ac | |||
08c0962b40 | |||
a537b980e6 | |||
|
dfee8aae33 | ||
|
f831fd6238 |
@ -12,57 +12,45 @@ suite "timeutils":
|
|||||||
interval.format("mm:ss") == "01:10"
|
interval.format("mm:ss") == "01:10"
|
||||||
|
|
||||||
test "DateTime difference":
|
test "DateTime difference":
|
||||||
var t1 = getLocalTime(getTime())
|
var t1 = getTime().local
|
||||||
var t2 = t1 + 30.seconds
|
var t2 = t1 + 30.seconds
|
||||||
check t2 - t1 == 30.seconds
|
check t2 - t1 == initDuration(seconds = 30)
|
||||||
|
|
||||||
t1 = parse("2016-10-10 09:45:00", "yyyy-MM-dd HH:mm:ss")
|
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")
|
t2 = parse("2016-10-11 09:45:00", "yyyy-MM-dd HH:mm:ss")
|
||||||
check t2 - t1 == seconds(24 * 60 * 60)
|
check t2 - t1 == initDuration(seconds = 24 * 60 * 60)
|
||||||
|
|
||||||
t2 = parse("2016-10-11 10:00:00", "yyyy-MM-dd HH:mm:ss")
|
t2 = parse("2016-10-11 10:00:00", "yyyy-MM-dd HH:mm:ss")
|
||||||
check t2 - t1 == seconds((24 * 60 + 15) * 60)
|
check t2 - t1 == initDuration(seconds = (24 * 60 + 15) * 60)
|
||||||
|
|
||||||
test "DateTime comparisons":
|
test "DateTime comparisons":
|
||||||
let t1 = getLocalTime(getTime())
|
let t1 = getTime().local
|
||||||
|
|
||||||
check:
|
check:
|
||||||
|
not (t1 < t1)
|
||||||
t1 < t1 + 10.seconds
|
t1 < t1 + 10.seconds
|
||||||
not (t1 + 10.seconds < t1)
|
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
|
t1 <= t1
|
||||||
|
t1 <= t1 + 10.seconds
|
||||||
not (t1 + 10.seconds <= 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":
|
test "TimeInterval comparisons":
|
||||||
check:
|
check:
|
||||||
30.seconds > 10.seconds
|
timeutils.`<`(10.minutes, 1.hours)
|
||||||
not (10.seconds > 30.seconds)
|
not timeutils.`<`(1.hours, 10.minutes)
|
||||||
not (10.seconds > 10.seconds)
|
not timeutils.`<`(1.hours, 1.hours)
|
||||||
|
|
||||||
10.minutes < 1.hours
|
timeutils.`<=`(60.seconds, 1.minutes)
|
||||||
not (1.hours < 10.minutes)
|
timeutils.`<=`(60.seconds, 2.minutes)
|
||||||
not (1.hours < 1.hours)
|
not timeutils.`<=`(2.minutes, 60.seconds)
|
||||||
|
|
||||||
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":
|
test "DateTime cmp":
|
||||||
let t1 = getLocalTime(getTime())
|
let t1 = getTime().local
|
||||||
|
|
||||||
check cmp(t1, t1) == 0
|
check cmp(t1, t1) == 0
|
||||||
check cmp(t1, t1 + 10.seconds) == -1
|
check cmp(t1, t1 + 10.seconds) == -1
|
||||||
@ -88,7 +76,7 @@ suite "timeutils":
|
|||||||
# mentioned above.
|
# mentioned above.
|
||||||
check:
|
check:
|
||||||
# Start of week = Monday
|
# Start of week = Monday
|
||||||
startOfWeek(t1) == startOfDay(getLocalTime(toTime(parse("2015-12-28 12:01:00", dtFormat))))
|
startOfWeek(t1) == startOfDay(toTime(parse("2015-12-28 12:01:00", dtFormat)).local)
|
||||||
startOfWeek(t1).weekday == dMon
|
startOfWeek(t1).weekday == dMon
|
||||||
startOfWeek(startOfWeek(t1)) == startOfWeek(t1)
|
startOfWeek(startOfWeek(t1)) == startOfWeek(t1)
|
||||||
|
|
||||||
@ -108,5 +96,20 @@ suite "timeutils":
|
|||||||
let t2 = fixedParse("2015-06-01 12:00:00", dtFormat)
|
let t2 = fixedParse("2015-06-01 12:00:00", dtFormat)
|
||||||
|
|
||||||
check: # test both in DST and out of DST
|
check: # test both in DST and out of DST
|
||||||
t1 == getLocalTime(toTime(t1))
|
t1 == toTime(t1).local
|
||||||
t2 == getLocalTime(toTime(t2))
|
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))
|
||||||
|
@ -1,58 +1,74 @@
|
|||||||
import times
|
import times
|
||||||
|
|
||||||
const zeroTime = fromSeconds(0)
|
const zeroTime = fromUnix(0)
|
||||||
|
const ISO_8601_FORMATS = @[
|
||||||
|
"yyyy-MM-dd",
|
||||||
|
"yyyy-MM-dd'T'HH:mm:ssz",
|
||||||
|
"yyyy-MM-dd'T'HH:mm:sszzz",
|
||||||
|
"yyyy-MM-dd'T'HH:mm:ss'.'fffzzz",
|
||||||
|
"yyyy-MM-dd HH:mm:ssz",
|
||||||
|
"yyyy-MM-dd HH:mm:sszzz",
|
||||||
|
"yyyy-MM-dd HH:mm:ss'.'fffzzz"
|
||||||
|
]
|
||||||
|
|
||||||
proc format*(ti: TimeInterval, fmt: string): string =
|
proc format*(ti: TimeInterval, fmt: string): string =
|
||||||
let info = getGMTime(fromSeconds(0) + ti)
|
let info = (zeroTime + ti).utc
|
||||||
return info.format(fmt)
|
return info.format(fmt)
|
||||||
|
|
||||||
proc `-`*(a, b: DateTime): TimeInterval =
|
proc format*(d: Duration, fmt: string): string =
|
||||||
return seconds(cast[int](a.toTime - b.toTime))
|
let info = (fromUnix(0) + d).utc
|
||||||
|
return info.format(fmt)
|
||||||
#[
|
|
||||||
# Deprecated as it exists in the standard times module now.
|
|
||||||
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 =
|
proc `<`*(a, b: TimeInterval): bool =
|
||||||
return (zeroTime + a) < (zeroTime + b)
|
return (zeroTime + a) < (zeroTime + b)
|
||||||
|
|
||||||
proc `>=`*(a, b: TimeInterval): bool =
|
|
||||||
return (zeroTime + a) >= (zeroTime + b)
|
|
||||||
|
|
||||||
proc `<=`*(a, b: TimeInterval): bool =
|
proc `<=`*(a, b: TimeInterval): bool =
|
||||||
return (zeroTime + a) <= (zeroTime + b)
|
return (zeroTime + a) <= (zeroTime + b)
|
||||||
|
|
||||||
|
proc between*(a, s, e: DateTime): bool =
|
||||||
|
return times.`<=`(s, a) and times.`<`(a, e)
|
||||||
|
|
||||||
proc cmp*(a, b: DateTime): int =
|
proc cmp*(a, b: DateTime): int =
|
||||||
if b == a: return 0
|
if b == a: return 0
|
||||||
elif a > b: return 1
|
elif times.`<`(a, b): return -1
|
||||||
else: return -1
|
else: return 1
|
||||||
|
|
||||||
proc startOfDay*(ti: DateTime): DateTime =
|
proc startOfDay*(dt: DateTime): DateTime =
|
||||||
result = ti
|
result = initDateTime(
|
||||||
result.hour = 0
|
dt.monthday,
|
||||||
result.minute = 0
|
dt.month,
|
||||||
result.second = 0
|
dt.year,
|
||||||
|
0, 0, 0, 0, # hour, minute, second, nanosecond
|
||||||
|
dt.timezone)
|
||||||
|
|
||||||
|
proc trimNanoSec*(dt: DateTime): DateTime =
|
||||||
|
result = dt
|
||||||
|
result = initDateTime(
|
||||||
|
dt.monthday,
|
||||||
|
dt.month,
|
||||||
|
dt.year,
|
||||||
|
dt.hour,
|
||||||
|
dt.minute,
|
||||||
|
dt.second,
|
||||||
|
0,
|
||||||
|
dt.timezone)
|
||||||
|
|
||||||
proc startOfWeek*(ti: DateTime, startDay = dMon): DateTime =
|
proc startOfWeek*(ti: DateTime, startDay = dMon): DateTime =
|
||||||
var diff = (ti.weekday.ord - startDay.ord)
|
var diff = (ti.weekday.ord - startDay.ord)
|
||||||
if diff < 0: diff += 7
|
if diff < 0: diff += 7
|
||||||
return (ti - diff.days).startOfDay
|
return (ti - diff.days).startOfDay
|
||||||
|
|
||||||
|
proc parseIso8601*(val: string): DateTime =
|
||||||
|
var errString = ""
|
||||||
|
for df in ISO_8601_FORMATS:
|
||||||
|
try: return val.parse(df)
|
||||||
|
except: errString &= "\n" & getCurrentExceptionMsg()
|
||||||
|
raise newException(Exception, "Could not parse date. Tried:" & errString)
|
||||||
|
|
||||||
|
proc formatIso8601*(d: DateTime): string =
|
||||||
|
return d.format(ISO_8601_FORMATS[2])
|
||||||
|
|
||||||
# This is a workaround needed due a bug in Nim's times.parse procedure.
|
# This is a workaround needed due a bug in Nim's times.parse procedure.
|
||||||
# see: https://github.com/nim-lang/Nim/issues/4922
|
# see: https://github.com/nim-lang/Nim/issues/4922
|
||||||
{.deprecated.}
|
template fixedParse*(value, format: string): DateTime {.deprecated.} =
|
||||||
template fixedParse*(value, format: string): DateTime = parse(value, format)
|
parse(value, format)
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "0.2.2"
|
version = "0.5.4"
|
||||||
author = "Jonathan Bernard"
|
author = "Jonathan Bernard"
|
||||||
description = "Utility methods to fill in the lacking time support in Nim\'s stdlib. This is holding me over until I can write a proper time module for the stdlib and submit it."
|
description = "Utility methods to fill in the lacking time support in Nim\'s stdlib."
|
||||||
license = "BSD3"
|
license = "BSD3"
|
||||||
|
|
||||||
# Dependencies
|
# Dependencies
|
||||||
|
|
||||||
requires "nim >= 0.18.1"
|
requires "nim >= 1.3.1"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user