4 Commits
0.1.0 ... 0.2.2

Author SHA1 Message Date
8987a4e268 Changes to support Nim 0.18 (with new times implementation). 2018-04-02 14:24:40 -05:00
b4804cdb52 WIP: Waiting for fix to be merged in Nim core so I can deprecate fixedParse. 2016-10-21 17:35:16 -05:00
79d2cb392d Added startOfDay, startOfWeek, and fixedParse.
* `startOfDay(TimeInfo): TimeInfo` returns a new `TimeInfo` representing
  midnight at the beginning of the given day.
* `startOfWeek(TimeInfo, WeekDay): TimeInfo` returns a new `TimeInfo`
  representing midnight at the beginning of the first day of the week. By
  default Monday is used as the start of the week (to be consistent with
  `times` view of the day order), but the user can pass in any other day to
  "start" the week. Because this find the start of the *current* week, the
  returned `TimeInfo` will always be a date in the past or present, never a
  date in the future.
2016-10-21 15:34:09 -05:00
cb11d62e5d More unit tests (cover previous bug in Nim times library). 2016-10-12 16:34:22 -05:00
5 changed files with 137 additions and 66 deletions

View File

@ -1,60 +0,0 @@
import times, timeutils, unittest
suite "timeutils":
test "format TimeInterval":
let interval = seconds(70)
check:
interval.format("ss's'") == "10s"
interval.format("mm'm' ss's'") == "01m 10s"
interval.format("mm:ss") == "01:10"
test "TimeInfo difference":
let t1 = getLocalTime(getTime())
let t2 = t1 + 30.seconds
check t2 - t1 == 30.seconds
test "TimeInfo comparisons":
let t1 = getLocalTime(getTime())
check:
t1 < t1 + 10.seconds
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
not (t1 + 10.seconds <= t1)
test "TimeInterval comparisons":
check:
30.seconds > 10.seconds
not (10.seconds > 30.seconds)
not (10.seconds > 10.seconds)
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)
test "TimeInfo cmp":
let t1 = getLocalTime(getTime())
check cmp(t1, t1) == 0
check cmp(t1, t1 + 10.seconds) == -1
check cmp(t1 + 10.seconds, t1) == 1

112
tests/ttimeutils.nim Normal file
View File

@ -0,0 +1,112 @@
import times, unittest
import "../timeutils"
let dtFormat = "yyyy-MM-dd HH:mm:ss"
suite "timeutils":
test "format TimeInterval":
let interval = seconds(70)
check:
interval.format("ss's'") == "10s"
interval.format("mm'm' ss's'") == "01m 10s"
interval.format("mm:ss") == "01:10"
test "DateTime difference":
var t1 = getLocalTime(getTime())
var 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")
check t2 - t1 == 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)
test "DateTime comparisons":
let t1 = getLocalTime(getTime())
check:
t1 < t1 + 10.seconds
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
not (t1 + 10.seconds <= t1)
test "TimeInterval comparisons":
check:
30.seconds > 10.seconds
not (10.seconds > 30.seconds)
not (10.seconds > 10.seconds)
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)
test "DateTime cmp":
let t1 = getLocalTime(getTime())
check cmp(t1, t1) == 0
check cmp(t1, t1 + 10.seconds) == -1
check cmp(t1 + 10.seconds, t1) == 1
test "startOfDay":
let t1 = parse("13:42:19", "HH:mm:ss")
let t2 = parse("2015-12-31 23:59:59", dtFormat)
check:
parse("00:00:00", "HH:mm:ss") == startOfDay(t1)
#check parse("2015-12-31 00:00:00", dtFormat) == startOfDay(t2)
startOfDay(startOfDay(t1)) == startOfDay(t1)
test "startOfWeek":
let t1 = parse("2015-12-31 23:59:59", dtFormat)
let t2 = parse("2015-12-26 23:59:59", dtFormat)
let t3 = parse("2016-01-01 23:59:59", dtFormat)
# Not parsing the start of the day in order to work around the bug
# mentioned above.
check:
# Start of week = Monday
startOfWeek(t1) == startOfDay(getLocalTime(toTime(parse("2015-12-28 12:01:00", dtFormat))))
startOfWeek(t1).weekday == dMon
startOfWeek(startOfWeek(t1)) == startOfWeek(t1)
startOfWeek(t2) == startOfDay(parse("2015-12-21 01:00:00", dtFormat))
startOfWeek(t3) == startOfDay(parse("2015-12-28 01:00:00", dtFormat))
# Start of week = Sunday
startOfWeek(t1, dSun) == startOfDay(parse("2015-12-27 01:00:00", dtFormat))
startOfWeek(t1, dSun).weekday == dSun
startOfWeek(startOfWeek(t1, dSun), dSun) == startOfWeek(t1, dSun)
startOfWeek(t2, dSun) == startOfDay(parse("2015-12-20 01:00:00", dtFormat))
startOfWeek(t3, dSun) == startOfDay(parse("2015-12-27 01:00:00", dtFormat))
test "fixedParse":
let t1 = fixedParse("2015-12-01 12:00:00", dtFormat)
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))

View File

@ -6,9 +6,11 @@ proc format*(ti: TimeInterval, fmt: string): string =
let info = getGMTime(fromSeconds(0) + ti)
return info.format(fmt)
proc `-`*(a, b: TimeInfo): TimeInterval =
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 =
return a.toTime > b.toTime
@ -20,6 +22,7 @@ proc `>=`*(a, b: TimeInfo): bool =
proc `<=`*(a, b: TimeInfo): bool =
return a.toTime <= b.toTime
]#
proc `>`*(a, b: TimeInterval): bool =
return (zeroTime + a) > (zeroTime + b)
@ -33,7 +36,23 @@ proc `>=`*(a, b: TimeInterval): bool =
proc `<=`*(a, b: TimeInterval): bool =
return (zeroTime + a) <= (zeroTime + b)
proc cmp(a, b: TimeInfo): int =
proc cmp*(a, b: DateTime): int =
if b == a: return 0
elif b > a: return 1
elif a > b: return 1
else: return -1
proc startOfDay*(ti: DateTime): DateTime =
result = ti
result.hour = 0
result.minute = 0
result.second = 0
proc startOfWeek*(ti: DateTime, startDay = dMon): DateTime =
var diff = (ti.weekday.ord - startDay.ord)
if diff < 0: diff += 7
return (ti - diff.days).startOfDay
# 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)

View File

@ -1,11 +1,11 @@
# Package
version = "0.1.0"
version = "0.2.2"
author = "Jonathan Bernard"
description = "Utility methods to fill in the horrid 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. 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.15.0"
requires "nim >= 0.18.1"