diff mercurial/templater.py @ 26128:51f6940d3b4f

templater: add optional timezone argument to localdate() The keyword extension uses "utcdate" for a different function, so we can't add new "utcdate" filter or function. Instead, this patch extends "localdate" to a general timezone converter.
author Yuya Nishihara <yuya@tcha.org>
date Tue, 18 Aug 2015 22:15:46 +0900
parents 7012be5ab5bd
children 662ea52d5dca
line wrap: on
line diff
--- a/mercurial/templater.py	Tue Sep 01 19:15:16 2015 +0900
+++ b/mercurial/templater.py	Tue Aug 18 22:15:46 2015 +0900
@@ -517,10 +517,11 @@
     yield args[1][0](context, mapping, args[1][1])
 
 def localdate(context, mapping, args):
-    """:localdate(date): Converts a date to local date."""
-    if len(args) != 1:
+    """:localdate(date[, tz]): Converts a date to the specified timezone.
+    The default is local date."""
+    if not (1 <= len(args) <= 2):
         # i18n: "localdate" is a keyword
-        raise error.ParseError(_("localdate expects one argument"))
+        raise error.ParseError(_("localdate expects one or two arguments"))
 
     date = evalfuncarg(context, mapping, args[0])
     try:
@@ -528,7 +529,19 @@
     except AttributeError:  # not str nor date tuple
         # i18n: "localdate" is a keyword
         raise error.ParseError(_("localdate expects a date information"))
-    tzoffset = util.makedate()[1]
+    if len(args) >= 2:
+        tzoffset = None
+        tz = evalfuncarg(context, mapping, args[1])
+        if isinstance(tz, str):
+            tzoffset = util.parsetimezone(tz)
+        if tzoffset is None:
+            try:
+                tzoffset = int(tz)
+            except (TypeError, ValueError):
+                # i18n: "localdate" is a keyword
+                raise error.ParseError(_("localdate expects a timezone"))
+    else:
+        tzoffset = util.makedate()[1]
     return (date[0], tzoffset)
 
 def revset(context, mapping, args):