changeset 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 a103ecb8a04a
files mercurial/help/templates.txt mercurial/templater.py tests/test-command-template.t
diffstat 3 files changed, 40 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/help/templates.txt	Tue Sep 01 19:15:16 2015 +0900
+++ b/mercurial/help/templates.txt	Tue Aug 18 22:15:46 2015 +0900
@@ -69,6 +69,10 @@
 
    $ hg log -r 0 --template "{date(date, '%Y')}\n"
 
+- Display date in UTC::
+
+   $ hg log -r 0 --template "{localdate(date, 'UTC')|date}\n"
+
 - Output the description set to a fill-width of 30::
 
    $ hg log -r 0 --template "{fill(desc, 30)}"
--- 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):
--- a/tests/test-command-template.t	Tue Sep 01 19:15:16 2015 +0900
+++ b/tests/test-command-template.t	Tue Aug 18 22:15:46 2015 +0900
@@ -3109,6 +3109,25 @@
   hg: parse error: get() expects a dict as first argument
   [255]
 
+Test localdate(date, tz) function:
+
+  $ TZ=JST-09 hg log -r0 -T '{date|localdate|isodate}\n'
+  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, "+0200")|isodate}\n'
+  1970-01-01 02:00 +0200
+  $ TZ=JST-09 hg log -r0 -T '{localdate(date, "0")|isodate}\n'
+  1970-01-01 00:00 +0000
+  $ TZ=JST-09 hg log -r0 -T '{localdate(date, 0)|isodate}\n'
+  1970-01-01 00:00 +0000
+  $ hg log -r0 -T '{localdate(date, "invalid")|isodate}\n'
+  hg: parse error: localdate expects a timezone
+  [255]
+  $ hg log -r0 -T '{localdate(date, date)|isodate}\n'
+  hg: parse error: localdate expects a timezone
+  [255]
+
 Test shortest(node) function:
 
   $ echo b > b