summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorFelix Krause <contact@flyx.org>2016-11-14 18:36:03 +0100
committerFelix Krause <contact@flyx.org>2016-11-14 18:36:03 +0100
commit0587a578075498dffaadbe7cf0ba1885eb597536 (patch)
tree24c34da8c4b0ab63c00bd58da42059f7ad87ac96 /lib
parentaa08c32c2b6a32da1aa1f98234512f37330a6691 (diff)
downloadNim-0587a578075498dffaadbe7cf0ba1885eb597536.tar.gz
Assume local DST iff no timezone is given
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/times.nim19
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/pure/times.nim b/lib/pure/times.nim
index b6b9fad5b..ef23c4cf9 100644
--- a/lib/pure/times.nim
+++ b/lib/pure/times.nim
@@ -1097,6 +1097,7 @@ proc parseToken(info: var TimeInfo; token, value: string; j: var int) =
     else:
       raise newException(ValueError,
         "Couldn't parse timezone offset (z), got: " & value[j])
+    info.isDST = false
     j += 2
   of "zz":
     if value[j] == '+':
@@ -1106,6 +1107,7 @@ proc parseToken(info: var TimeInfo; token, value: string; j: var int) =
     else:
       raise newException(ValueError,
         "Couldn't parse timezone offset (zz), got: " & value[j])
+    info.isDST = false
     j += 3
   of "zzz":
     var factor = 0
@@ -1118,6 +1120,7 @@ proc parseToken(info: var TimeInfo; token, value: string; j: var int) =
     j += 4
     info.timezone += factor * value[j..j+1].parseInt() * 60
     j += 2
+    info.isDST = false
   else:
     # Ignore the token and move forward in the value string by the same length
     j += token.len
@@ -1167,7 +1170,8 @@ proc parse*(value, layout: string): TimeInfo =
   info.hour = 0
   info.minute = 0
   info.second = 0
-  info.isDST = false # DST is never encoded in timestamps.
+  info.isDST = true # using this is flag for checking whether a timezone has \
+      # been read (because DST is always false when a tz is parsed)
   while true:
     case layout[i]
     of ' ', '-', '/', ':', '\'', '\0', '(', ')', '[', ']', ',':
@@ -1197,8 +1201,17 @@ proc parse*(value, layout: string): TimeInfo =
         parseToken(info, token, value, j)
         token = ""
 
-  # Now we process it again with the correct isDST to correct things like
-  # weekday and yearday.
+  if info.isDST:
+    # means that no timezone has been parsed. In this case, we need to check
+    # whether the date is within DST of the local time.
+    let tmp = getLocalTime(toTime(info))
+    # correctly set isDST so that the following step works on the correct time
+    info.isDST = tmp.isDST
+
+  # Correct weekday and yearday; transform timestamp to local time.
+  # There currently is no way of returning this with the original (parsed)
+  # timezone while also setting weekday and yearday (we are depending on stdlib
+  # to provide this calculation).
   return getLocalTime(toTime(info))
 
 # Leap year calculations are adapted from: