Compare commits

...

2 Commits
0.5.2 ... main

Author SHA1 Message Date
a9308cbaf3 Add ISO8601 date format (yyyy-MM-dd) to parseIso8601 utility function. 2023-02-28 23:25:44 -06:00
f4e392f910 Update for Nim 1.3.1
Change `startOfDay` and `trimNanoSec` to use `initDateTime` instead of
mutating the new copy via the deprecated property accessors.

Updated the test suite to reflect functions that moved from this library
to the standard library. We're still testing the expected functionality
to make sure that the contract is maintained for users of this library.
2021-07-17 09:45:39 -05:00
3 changed files with 28 additions and 18 deletions

View File

@ -14,27 +14,27 @@ suite "timeutils":
test "DateTime difference":
var t1 = getTime().local
var t2 = t1 + 30.seconds
check timeutils.`-`(t2, t1) == initDuration(seconds = 30)
check 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 timeutils.`-`(t2, t1) == initDuration(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")
check timeutils.`-`(t2, t1) == initDuration(seconds = (24 * 60 + 15) * 60)
check t2 - t1 == initDuration(seconds = (24 * 60 + 15) * 60)
test "DateTime comparisons":
let t1 = getTime().local
check:
timeutils.`<`(t1, t1 + 10.seconds)
not timeutils.`<`(t1 + 10.seconds, t1)
not timeutils.`<`(t1, t1)
not (t1 < t1)
t1 < t1 + 10.seconds
not (t1 + 10.seconds < t1)
t1 <= t1
t1 <= t1 + 10.seconds
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
not t1.between(t1 - 10.seconds, t1) # end is exclusive

View File

@ -2,6 +2,7 @@ import times
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",
@ -33,15 +34,24 @@ proc cmp*(a, b: DateTime): int =
else: return 1
proc startOfDay*(dt: DateTime): DateTime =
result = dt
result.hour = 0
result.minute = 0
result.second = 0
result.nanosecond = 0
result = initDateTime(
dt.monthday,
dt.month,
dt.year,
0, 0, 0, 0, # hour, minute, second, nanosecond
dt.timezone)
proc trimNanoSec*(dt: DateTime): DateTime =
result = dt
result.nanosecond = 0
result = initDateTime(
dt.monthday,
dt.month,
dt.year,
dt.hour,
dt.minute,
dt.second,
0,
dt.timezone)
proc startOfWeek*(ti: DateTime, startDay = dMon): DateTime =
var diff = (ti.weekday.ord - startDay.ord)

View File

@ -1,10 +1,10 @@
# Package
version = "0.5.2"
version = "0.5.4"
author = "Jonathan Bernard"
description = "Utility methods to fill in the lacking time support in Nim\'s stdlib."
license = "BSD3"
# Dependencies
requires "nim >= 1.0.0"
requires "nim >= 1.3.1"