date: refactor timezone parsing stable
authorMatt Mackall <mpm@selenic.com>
Wed, 27 Jul 2016 15:14:19 -0500
branchstable
changeset 29636 84ef4517de03
parent 29635 dee24c87dbf0
child 29637 46b2ccce7fde
date: refactor timezone parsing We want to be able to accept ISO 8601 style timezones that don't include a space separator, so we change the timezone parsing function to accept a full date string and return both the offset and the non-timezone portion.
mercurial/templater.py
mercurial/util.py
tests/test-command-template.t
--- a/mercurial/templater.py	Thu Jul 28 08:53:36 2016 -0700
+++ b/mercurial/templater.py	Wed Jul 27 15:14:19 2016 -0500
@@ -670,7 +670,9 @@
         tzoffset = None
         tz = evalfuncarg(context, mapping, args[1])
         if isinstance(tz, str):
-            tzoffset = util.parsetimezone(tz)
+            tzoffset, remainder = util.parsetimezone(tz)
+            if remainder:
+                tzoffset = None
         if tzoffset is None:
             try:
                 tzoffset = int(tz)
--- a/mercurial/util.py	Thu Jul 28 08:53:36 2016 -0700
+++ b/mercurial/util.py	Wed Jul 27 15:14:19 2016 -0500
@@ -1746,24 +1746,27 @@
     """turn (timestamp, tzoff) tuple into iso 8631 date."""
     return datestr(date, format='%Y-%m-%d')
 
-def parsetimezone(tz):
-    """parse a timezone string and return an offset integer"""
-    if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
-        sign = (tz[0] == "+") and 1 or -1
-        hours = int(tz[1:3])
-        minutes = int(tz[3:5])
-        return -sign * (hours * 60 + minutes) * 60
-    if tz == "GMT" or tz == "UTC":
-        return 0
-    return None
+def parsetimezone(s):
+    """find a trailing timezone, if any, in string, and return a
+       (offset, remainder) pair"""
+
+    if s.endswith("GMT") or s.endswith("UTC"):
+        return 0, s[:-3].rstrip()
+
+    # Unix-style timezones [+-]hhmm
+    if len(s) >= 5 and s[-5] in "+-" and s[-4:].isdigit():
+        sign = (s[-5] == "+") and 1 or -1
+        hours = int(s[-4:-2])
+        minutes = int(s[-2:])
+        return -sign * (hours * 60 + minutes) * 60, s[:-5].rstrip()
+
+    return None, s
 
 def strdate(string, format, defaults=[]):
     """parse a localized time string and return a (unixtime, offset) tuple.
     if the string cannot be parsed, ValueError is raised."""
     # NOTE: unixtime = localunixtime + offset
-    offset, date = parsetimezone(string.split()[-1]), string
-    if offset is not None:
-        date = " ".join(string.split()[:-1])
+    offset, date = parsetimezone(string)
 
     # add missing elements from defaults
     usenow = False # default to using biased defaults
--- a/tests/test-command-template.t	Thu Jul 28 08:53:36 2016 -0700
+++ b/tests/test-command-template.t	Wed Jul 27 15:14:19 2016 -0500
@@ -3253,6 +3253,9 @@
   1970-01-01 09:00 +0900
   $ TZ=JST-09 hg log -r0 -T '{localdate(date, "UTC")|isodate}\n'
   1970-01-01 00:00 +0000
+  $ TZ=JST-09 hg log -r0 -T '{localdate(date, "blahUTC")|isodate}\n'
+  hg: parse error: localdate expects a timezone
+  [255]
   $ TZ=JST-09 hg log -r0 -T '{localdate(date, "+0200")|isodate}\n'
   1970-01-01 02:00 +0200
   $ TZ=JST-09 hg log -r0 -T '{localdate(date, "0")|isodate}\n'