summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lib/pure/times.nim15
-rw-r--r--tests/stdlib/ttime.nim12
2 files changed, 20 insertions, 7 deletions
diff --git a/lib/pure/times.nim b/lib/pure/times.nim
index 1e869d301..f74003820 100644
--- a/lib/pure/times.nim
+++ b/lib/pure/times.nim
@@ -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(':')
diff --git a/tests/stdlib/ttime.nim b/tests/stdlib/ttime.nim
index 5d3c8325e..c1559ec7a 100644
--- a/tests/stdlib/ttime.nim
+++ b/tests/stdlib/ttime.nim
@@ -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]
\ No newline at end of file