Fixed daylight saving time

* When formatting timezone, substract 1 hour from timezone when isDST
 * Do not depend DST in current timezone when parsing arbitrary date
   because formatted timestamps are never in DST.
 * On the way, removed an unnecessary line in parsing code which could
   cause bugs.
 * Added DST tests
This commit is contained in:
Felix Krause
2016-11-10 22:22:48 +01:00
parent 4a4a3cdf67
commit 544a2cfe1a
2 changed files with 38 additions and 20 deletions

View File

@@ -201,4 +201,25 @@ for tz in [
let ti = TimeInfo(monthday: 1, timezone: tz[0])
doAssert ti.format("z") == tz[1]
doAssert ti.format("zz") == tz[2]
doAssert ti.format("zzz") == tz[3]
doAssert ti.format("zzz") == tz[3]
block dstTest:
let nonDst = TimeInfo(year: 2015, month: mJan, monthday: 01, yearday: 0,
weekday: dThu, hour: 00, minute: 00, second: 00, isDST: false, timezone: 0)
var dst = nonDst
dst.isDst = true
# note that both isDST == true and isDST == false are valid here because
# DST is in effect on January 1st in some southern parts of Australia.
doAssert nonDst.toTime() - dst.toTime() == 3600
doAssert nonDst.format("z") == "+0"
doAssert dst.format("z") == "+1"
# parsing will set isDST in relation to the local time. We take a date in
# January and one in July to maximize the probability to hit one date with DST
# and one without on the local machine. However, this is not guaranteed.
let
parsedJul = parse("2016-07-01 04:00:00+01:00", "yyyy-MM-dd HH:mm:sszzz")
parsedJan = parse("2016-01-05 04:00:00+01:00", "yyyy-MM-ss HH:mm:sszzz")
doAssert toTime(parsedJan) == fromSeconds(1452394800)
doAssert toTime(parsedJul) == fromSeconds(1467342000)