Compare commits

...

6 Commits
0.3.0 ... 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
600747e1ac Update for Nim 1.0.0 - Added format for Durations. Remove deprecated functions. 2020-02-14 12:22:05 -06:00
08c0962b40 Add addtional valid ISO8601 formats. 2019-12-27 10:07:04 -06:00
a537b980e6 Expand allowed ISO8601 parsing formats. 2019-04-27 23:15:14 -05:00
Jonathan Bernard
dfee8aae33 Update for Nim 0.19.0, add trimNanoSec 2018-12-09 06:03:53 -06:00
3 changed files with 56 additions and 65 deletions

View File

@ -14,56 +14,40 @@ suite "timeutils":
test "DateTime difference": test "DateTime difference":
var t1 = getTime().local 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 = getTime().local 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 - 10.seconds, t1 + 10.seconds)
t1.between(t1, t1 + 10.seconds) # start is inclusive t1.between(t1, t1 + 10.seconds) # start is inclusive
not t1.between(t1 - 10.seconds, t1) # end is exclusive 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 = getTime().local let t1 = getTime().local

View File

@ -1,53 +1,57 @@
import times import times
const zeroTime = fromUnix(0) const zeroTime = fromUnix(0)
const ISO_8601_FULL_FORMAT = "yyyy:MM:dd'T'HH:mm:sszzz" 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 = (fromUnix(0) + ti).utc let info = (zeroTime + ti).utc
return info.format(fmt) return info.format(fmt)
# Will be deprecated in Nim 0.18.1 as it will exist in the standard times module. proc format*(d: Duration, fmt: string): string =
proc `-`*(a, b: DateTime): TimeInterval = let info = (fromUnix(0) + d).utc
return seconds(cast[int](a.toTime - b.toTime)) return info.format(fmt)
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: DateTime): 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 = 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 = 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)
@ -55,10 +59,14 @@ proc startOfWeek*(ti: DateTime, startDay = dMon): DateTime =
return (ti - diff.days).startOfDay return (ti - diff.days).startOfDay
proc parseIso8601*(val: string): DateTime = proc parseIso8601*(val: string): DateTime =
return val.parse(ISO_8601_FULL_FORMAT) 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 = proc formatIso8601*(d: DateTime): string =
return d.format(ISO_8601_FULL_FORMAT) 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

View File

@ -1,11 +1,10 @@
# Package # Package
version = "0.3.0" 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.0" requires "nim >= 1.3.1"