Fixed timezone rendering, added test

This commit is contained in:
Felix Krause 2016-11-10 19:03:46 +01:00
commit 91a0674961
2 changed files with 17 additions and 4 deletions

View file

@ -732,6 +732,7 @@ const
secondsInMin = 60 secondsInMin = 60
secondsInHour = 60*60 secondsInHour = 60*60
secondsInDay = 60*60*24 secondsInDay = 60*60*24
minutesInHour = 60
epochStartYear = 1970 epochStartYear = 1970
proc formatToken(info: TimeInfo, token: string, buf: var string) = proc formatToken(info: TimeInfo, token: string, buf: var string) =
@ -824,20 +825,20 @@ proc formatToken(info: TimeInfo, token: string, buf: var string) =
buf.add(fyear) buf.add(fyear)
of "z": of "z":
let hours = abs(info.timezone) div secondsInHour let hours = abs(info.timezone) div secondsInHour
if info.timezone < 0: buf.add('+') if info.timezone <= 0: buf.add('+')
else: buf.add('-') else: buf.add('-')
buf.add($hours) buf.add($hours)
of "zz": of "zz":
let hours = abs(info.timezone) div secondsInHour let hours = abs(info.timezone) div secondsInHour
if info.timezone < 0: buf.add('+') if info.timezone <= 0: buf.add('+')
else: buf.add('-') else: buf.add('-')
if hours < 10: buf.add('0') if hours < 10: buf.add('0')
buf.add($hours) buf.add($hours)
of "zzz": of "zzz":
let let
hours = abs(info.timezone) div secondsInHour hours = abs(info.timezone) div secondsInHour
minutes = abs(info.timezone) mod 60 minutes = (abs(info.timezone) div secondsInMin) mod minutesInHour
if info.timezone < 0: buf.add('+') if info.timezone <= 0: buf.add('+')
else: buf.add('-') else: buf.add('-')
if hours < 10: buf.add('0') if hours < 10: buf.add('0')
buf.add($hours) buf.add($hours)

View file

@ -190,3 +190,15 @@ doAssert cmpTimeNoSideEffect(0.fromSeconds, 0.fromSeconds)
let seqA: seq[Time] = @[] let seqA: seq[Time] = @[]
let seqB: seq[Time] = @[] let seqB: seq[Time] = @[]
doAssert seqA == seqB doAssert seqA == seqB
for tz in [
(0, "+0", "+00", "+00:00"), # UTC
(-3600, "+1", "+01", "+01:00"), # CET
(-39600, "+11", "+11", "+11:00"), # two digits
(-1800, "+0", "+00", "+00:30"), # half an hour
(7200, "-2", "-02", "-02:00"), # positive
(38700, "-10", "-10", "-10:45")]: # positive with three quaters hour
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]