changeset 26127:7012be5ab5bd

templater: port localdate filter to a function It will be extended to accept a timezone argument.
author Yuya Nishihara <yuya@tcha.org>
date Tue, 01 Sep 2015 19:15:16 +0900
parents 7b625baed995
children 51f6940d3b4f
files mercurial/templatefilters.py mercurial/templater.py tests/test-command-template.t
diffstat 3 files changed, 20 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templatefilters.py	Tue Sep 01 19:43:14 2015 +0900
+++ b/mercurial/templatefilters.py	Tue Sep 01 19:15:16 2015 +0900
@@ -235,10 +235,6 @@
         s = s.replace(k, v)
     return ''.join(_uescape(c) for c in s)
 
-def localdate(text):
-    """:localdate: Date. Converts a date to local date."""
-    return (util.parsedate(text)[0], util.makedate()[1])
-
 def lower(text):
     """:lower: Any text. Converts the text to lowercase."""
     return encoding.lower(text)
@@ -403,7 +399,6 @@
     "isodatesec": isodatesec,
     "json": json,
     "jsonescape": jsonescape,
-    "localdate": localdate,
     "lower": lower,
     "nonempty": nonempty,
     "obfuscate": obfuscate,
--- a/mercurial/templater.py	Tue Sep 01 19:43:14 2015 +0900
+++ b/mercurial/templater.py	Tue Sep 01 19:15:16 2015 +0900
@@ -516,6 +516,21 @@
     # ignore args[0] (the label string) since this is supposed to be a a no-op
     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:
+        # i18n: "localdate" is a keyword
+        raise error.ParseError(_("localdate expects one argument"))
+
+    date = evalfuncarg(context, mapping, args[0])
+    try:
+        date = util.parsedate(date)
+    except AttributeError:  # not str nor date tuple
+        # i18n: "localdate" is a keyword
+        raise error.ParseError(_("localdate expects a date information"))
+    tzoffset = util.makedate()[1]
+    return (date[0], tzoffset)
+
 def revset(context, mapping, args):
     """:revset(query[, formatargs...]): Execute a revision set query. See
     :hg:`help revset`."""
@@ -700,6 +715,7 @@
     "indent": indent,
     "join": join,
     "label": label,
+    "localdate": localdate,
     "pad": pad,
     "revset": revset,
     "rstdoc": rstdoc,
--- a/tests/test-command-template.t	Tue Sep 01 19:43:14 2015 +0900
+++ b/tests/test-command-template.t	Tue Sep 01 19:15:16 2015 +0900
@@ -2495,6 +2495,10 @@
   abort: template filter 'escape' is not compatible with keyword 'date'
   [255]
 
+  $ hg log -l 3 --template 'line: {extras|localdate}\n'
+  hg: parse error: localdate expects a date information
+  [255]
+
 Behind the scenes, this will throw ValueError
 
   $ hg tip --template '{author|email|date}\n'