Add , , and .

This commit is contained in:
Jonathan Bernard 2018-05-12 13:11:02 -05:00
parent 8987a4e268
commit f831fd6238
3 changed files with 47 additions and 20 deletions

View File

@ -12,9 +12,9 @@ suite "timeutils":
interval.format("mm:ss") == "01:10"
test "DateTime difference":
var t1 = getLocalTime(getTime())
var t1 = getTime().local
var t2 = t1 + 30.seconds
check 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")
@ -24,7 +24,7 @@ suite "timeutils":
check t2 - t1 == seconds((24 * 60 + 15) * 60)
test "DateTime comparisons":
let t1 = getLocalTime(getTime())
let t1 = getTime().local
check:
t1 < t1 + 10.seconds
@ -43,6 +43,10 @@ suite "timeutils":
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
@ -62,7 +66,7 @@ suite "timeutils":
not (2.minutes <= 60.seconds)
test "DateTime cmp":
let t1 = getLocalTime(getTime())
let t1 = getTime().local
check cmp(t1, t1) == 0
check cmp(t1, t1 + 10.seconds) == -1
@ -88,7 +92,7 @@ suite "timeutils":
# mentioned above.
check:
# 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(startOfWeek(t1)) == startOfWeek(t1)
@ -108,5 +112,20 @@ suite "timeutils":
let t2 = fixedParse("2015-06-01 12:00:00", dtFormat)
check: # test both in DST and out of DST
t1 == getLocalTime(toTime(t1))
t2 == getLocalTime(toTime(t2))
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))

View File

@ -1,28 +1,27 @@
import times
const zeroTime = fromSeconds(0)
const zeroTime = fromUnix(0)
const ISO_8601_FULL_FORMAT = "yyyy:MM:dd'T'HH:mm:sszzz"
proc format*(ti: TimeInterval, fmt: string): string =
let info = getGMTime(fromSeconds(0) + ti)
let info = (fromUnix(0) + ti).utc
return info.format(fmt)
# Will be deprecated in Nim 0.18.1 as it will exist in the standard times module.
proc `-`*(a, b: DateTime): TimeInterval =
return seconds(cast[int](a.toTime - b.toTime))
#[
# Deprecated as it exists in the standard times module now.
proc `>`*(a, b: TimeInfo): bool =
proc `>`*(a, b: DateTime): bool =
return a.toTime > b.toTime
proc `<`*(a, b: TimeInfo): bool =
proc `<`*(a, b: DateTime): bool =
return a.toTime < b.toTime
proc `>=`*(a, b: TimeInfo): bool =
proc `>=`*(a, b: DateTime): bool =
return a.toTime >= b.toTime
proc `<=`*(a, b: TimeInfo): bool =
proc `<=`*(a, b: DateTime): bool =
return a.toTime <= b.toTime
]#
proc `>`*(a, b: TimeInterval): bool =
return (zeroTime + a) > (zeroTime + b)
@ -36,6 +35,9 @@ proc `>=`*(a, b: TimeInterval): bool =
proc `<=`*(a, b: TimeInterval): bool =
return (zeroTime + a) <= (zeroTime + b)
proc between*(a, s, e: DateTime): bool =
return a >= s and a < e
proc cmp*(a, b: DateTime): int =
if b == a: return 0
elif a > b: return 1
@ -52,7 +54,13 @@ proc startOfWeek*(ti: DateTime, startDay = dMon): DateTime =
if diff < 0: diff += 7
return (ti - diff.days).startOfDay
proc parseIso8601*(val: string): DateTime =
return val.parse(ISO_8601_FULL_FORMAT)
proc formatIso8601*(d: DateTime): string =
return d.format(ISO_8601_FULL_FORMAT)
# This is a workaround needed due a bug in Nim's times.parse procedure.
# see: https://github.com/nim-lang/Nim/issues/4922
{.deprecated.}
template fixedParse*(value, format: string): DateTime = parse(value, format)
template fixedParse*(value, format: string): DateTime {.deprecated.} =
parse(value, format)

View File

@ -1,11 +1,11 @@
# Package
version = "0.2.2"
version = "0.3.0"
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."
license = "BSD3"
# Dependencies
requires "nim >= 0.18.1"
requires "nim >= 0.18.0"