Merge pull request #5009 from flyx/timezonefix2

Fixed timezone sign error
This commit is contained in:
Andreas Rumpf
2016-11-14 16:44:01 +01:00
committed by GitHub
2 changed files with 20 additions and 7 deletions

View File

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

View File

@@ -190,3 +190,15 @@ doAssert cmpTimeNoSideEffect(0.fromSeconds, 0.fromSeconds)
let seqA: seq[Time] = @[]
let seqB: seq[Time] = @[]
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]