WIP: Waiting for fix to be merged in Nim core so I can deprecate fixedParse.

This commit is contained in:
Jonathan Bernard 2016-10-21 17:35:16 -05:00
parent 79d2cb392d
commit b4804cdb52
3 changed files with 16 additions and 36 deletions

View File

@ -68,54 +68,39 @@ suite "timeutils":
check cmp(t1 + 10.seconds, t1) == 1 check cmp(t1 + 10.seconds, t1) == 1
test "startOfDay": test "startOfDay":
let t1 = fixedParse("13:42:19", "HH:mm:ss") let t1 = parse("13:42:19", "HH:mm:ss")
let t2 = fixedParse("2015-12-31 23:59:59", dtFormat) let t2 = parse("2015-12-31 23:59:59", dtFormat)
check: check:
fixedParse("00:00:00", "HH:mm:ss") == startOfDay(t1) parse("00:00:00", "HH:mm:ss") == startOfDay(t1)
#check fixedParse("2015-12-31 00:00:00", dtFormat) == startOfDay(t2) #check parse("2015-12-31 00:00:00", dtFormat) == startOfDay(t2)
startOfDay(startOfDay(t1)) == startOfDay(t1) startOfDay(startOfDay(t1)) == startOfDay(t1)
test "startOfWeek": test "startOfWeek":
let t1 = fixedParse("2015-12-31 23:59:59", dtFormat) let t1 = parse("2015-12-31 23:59:59", dtFormat)
let t2 = fixedParse("2015-12-26 23:59:59", dtFormat) let t2 = parse("2015-12-26 23:59:59", dtFormat)
let t3 = fixedParse("2016-01-01 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 # Not parsing the start of the day in order to work around the bug
# mentioned above. # mentioned above.
check: check:
# Start of week = Monday # Start of week = Monday
startOfWeek(t1) == startOfDay(getLocalTime(toTime(fixedParse("2015-12-28 12:01:00", dtFormat)))) startOfWeek(t1) == startOfDay(getLocalTime(toTime(parse("2015-12-28 12:01:00", dtFormat))))
startOfWeek(t1).weekday == dMon startOfWeek(t1).weekday == dMon
startOfWeek(startOfWeek(t1)) == startOfWeek(t1) startOfWeek(startOfWeek(t1)) == startOfWeek(t1)
startOfWeek(t2) == startOfDay(fixedParse("2015-12-21 01:00:00", dtFormat)) startOfWeek(t2) == startOfDay(parse("2015-12-21 01:00:00", dtFormat))
startOfWeek(t3) == startOfDay(fixedParse("2015-12-28 01:00:00", dtFormat)) startOfWeek(t3) == startOfDay(parse("2015-12-28 01:00:00", dtFormat))
# Start of week = Sunday # Start of week = Sunday
startOfWeek(t1, dSun) == startOfDay(fixedParse("2015-12-27 01:00:00", dtFormat)) startOfWeek(t1, dSun) == startOfDay(parse("2015-12-27 01:00:00", dtFormat))
startOfWeek(t1, dSun).weekday == dSun startOfWeek(t1, dSun).weekday == dSun
startOfWeek(startOfWeek(t1, dSun), dSun) == startOfWeek(t1, dSun) startOfWeek(startOfWeek(t1, dSun), dSun) == startOfWeek(t1, dSun)
startOfWeek(t2, dSun) == startOfDay(fixedParse("2015-12-20 01:00:00", dtFormat)) startOfWeek(t2, dSun) == startOfDay(parse("2015-12-20 01:00:00", dtFormat))
startOfWeek(t3, dSun) == startOfDay(fixedParse("2015-12-27 01:00:00", dtFormat)) startOfWeek(t3, dSun) == startOfDay(parse("2015-12-27 01:00:00", dtFormat))
test "times.parse is still broken":
let t1 = parse("2015-12-01 12:00:00", dtFormat)
let t2 = parse("2015-06-01 12:00:00", dtFormat)
# parse is broken in that is uses the DST setting of the current time when
# parsing dates when it should figure out the DST time for that date. So
# depending on if you are currently in DST or not, one of the above dates
# will not parse correctly. We want to check that one of those fails to
# parse correctly. When they both parse correctly, the times.parse bug has
# been fixed and fixedParse is no longer necessary.
# This test works because passing the time through getLocalTime(toTime())
# correctly the DST setting for the time.
check t1 != getLocalTime(toTime(t1)) or t2 != getLocalTime(toTime(t2))
test "fixedParse": test "fixedParse":
let t1 = fixedParse("2015-12-01 12:00:00", dtFormat) let t1 = fixedParse("2015-12-01 12:00:00", dtFormat)

View File

@ -51,10 +51,5 @@ proc startOfWeek*(ti: TimeInfo, startDay = dMon): TimeInfo =
# 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
proc fixedParse*(value, format: string): TimeInfo = {.deprecated.}
let firstParsed = parse(value, format) template fixedParse*(value, format: string): TimeInfo = parse(value, format)
let rectified = getLocalTime(toTime(firstParsed))
if firstParsed.isDST and not rectified.isDST: return rectified + 1.hours
elif not firstParsed.isDST and rectified.isDST: return rectified - 1.hours
else: return rectified

View File

@ -1,6 +1,6 @@
# Package # Package
version = "0.2.0" version = "0.2.1"
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. This is holding me over until I can write a proper time module for the stdlib and submit it."
license = "BSD3" license = "BSD3"