changeset 29636:84ef4517de03 stable

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.
author Matt Mackall <mpm@selenic.com>
date Wed, 27 Jul 2016 15:14:19 -0500
parents dee24c87dbf0
children 46b2ccce7fde
files mercurial/templater.py mercurial/util.py tests/test-command-template.t
diffstat 3 files changed, 22 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- 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'