Update for Nim 0.19.0, add trimNanoSec
This commit is contained in:
parent
f831fd6238
commit
dfee8aae33
@ -14,34 +14,26 @@ suite "timeutils":
|
||||
test "DateTime difference":
|
||||
var t1 = getTime().local
|
||||
var t2 = t1 + 30.seconds
|
||||
check (t2 - t1) == 30.seconds
|
||||
check timeutils.`-`(t2, t1) == initDuration(seconds = 30)
|
||||
|
||||
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)
|
||||
check timeutils.`-`(t2, t1) == initDuration(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)
|
||||
check timeutils.`-`(t2, t1) == initDuration(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)
|
||||
timeutils.`<`(t1, t1 + 10.seconds)
|
||||
not timeutils.`<`(t1 + 10.seconds, t1)
|
||||
not timeutils.`<`(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)
|
||||
timeutils.`<=`(t1, t1 + 10.seconds)
|
||||
timeutils.`<=`(t1, t1)
|
||||
not timeutils.`<=`(t1 + 10.seconds, t1)
|
||||
|
||||
t1.between(t1 - 10.seconds, t1 + 10.seconds)
|
||||
t1.between(t1, t1 + 10.seconds) # start is inclusive
|
||||
@ -49,21 +41,13 @@ suite "timeutils":
|
||||
|
||||
test "TimeInterval comparisons":
|
||||
check:
|
||||
30.seconds > 10.seconds
|
||||
not (10.seconds > 30.seconds)
|
||||
not (10.seconds > 10.seconds)
|
||||
timeutils.`<`(10.minutes, 1.hours)
|
||||
not timeutils.`<`(1.hours, 10.minutes)
|
||||
not timeutils.`<`(1.hours, 1.hours)
|
||||
|
||||
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)
|
||||
timeutils.`<=`(60.seconds, 1.minutes)
|
||||
timeutils.`<=`(60.seconds, 2.minutes)
|
||||
not timeutils.`<=`(2.minutes, 60.seconds)
|
||||
|
||||
test "DateTime cmp":
|
||||
let t1 = getTime().local
|
||||
|
@ -1,53 +1,46 @@
|
||||
import times
|
||||
|
||||
const zeroTime = fromUnix(0)
|
||||
const ISO_8601_FULL_FORMAT = "yyyy:MM:dd'T'HH:mm:sszzz"
|
||||
const ISO_8601_FULL_FORMAT = "yyyy-MM-dd'T'HH:mm:sszzz"
|
||||
|
||||
proc format*(ti: TimeInterval, fmt: string): string =
|
||||
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))
|
||||
proc `-`*(a, b: DateTime): Duration {.deprecated} =
|
||||
return times.`-`(a,b)
|
||||
|
||||
proc `>`*(a, b: DateTime): bool =
|
||||
return a.toTime > b.toTime
|
||||
proc `<`*(a, b: DateTime): bool {.deprecated} =
|
||||
return times.`<`(a,b)
|
||||
|
||||
proc `<`*(a, b: DateTime): bool =
|
||||
return a.toTime < b.toTime
|
||||
|
||||
proc `>=`*(a, b: DateTime): bool =
|
||||
return a.toTime >= b.toTime
|
||||
|
||||
proc `<=`*(a, b: DateTime): bool =
|
||||
return a.toTime <= b.toTime
|
||||
|
||||
proc `>`*(a, b: TimeInterval): bool =
|
||||
return (zeroTime + a) > (zeroTime + b)
|
||||
proc `<=`*(a, b: DateTime): bool {.deprecated} =
|
||||
return times.`<=`(a,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 between*(a, s, e: DateTime): bool =
|
||||
return a >= s and a < e
|
||||
return times.`<=`(s, a) and times.`<`(a, e)
|
||||
|
||||
proc cmp*(a, b: DateTime): int =
|
||||
if b == a: return 0
|
||||
elif a > b: return 1
|
||||
else: return -1
|
||||
elif times.`<`(a, b): return -1
|
||||
else: return 1
|
||||
|
||||
proc startOfDay*(ti: DateTime): DateTime =
|
||||
result = ti
|
||||
proc startOfDay*(dt: DateTime): DateTime =
|
||||
result = dt
|
||||
result.hour = 0
|
||||
result.minute = 0
|
||||
result.second = 0
|
||||
result.nanosecond = 0
|
||||
|
||||
proc trimNanoSec*(dt: DateTime): DateTime =
|
||||
result = dt
|
||||
result.nanosecond = 0
|
||||
|
||||
proc startOfWeek*(ti: DateTime, startDay = dMon): DateTime =
|
||||
var diff = (ti.weekday.ord - startDay.ord)
|
||||
|
@ -1,11 +1,11 @@
|
||||
# Package
|
||||
|
||||
version = "0.3.0"
|
||||
version = "0.4.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.0"
|
||||
requires "nim >= 0.19.0"
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user