From patchwork Tue Sep 1 13:25:14 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [4,of,5] templater: port localdate filter to a function From: Yuya Nishihara X-Patchwork-Id: 10344 Message-Id: <7c5bb6ce0f2bb12648b7.1441113914@mimosa> To: mercurial-devel@selenic.com Date: Tue, 01 Sep 2015 22:25:14 +0900 # HG changeset patch # User Yuya Nishihara # Date 1441102516 -32400 # Tue Sep 01 19:15:16 2015 +0900 # Node ID 7c5bb6ce0f2bb12648b73d5a09579c9890a4dbea # Parent ced5f8e9a180e38cd554d8c15334189e9bced9cd templater: port localdate filter to a function It will be extended to accept a timezone argument. diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py --- a/mercurial/templatefilters.py +++ b/mercurial/templatefilters.py @@ -235,10 +235,6 @@ def jsonescape(s): 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 @@ filters = { "isodatesec": isodatesec, "json": json, "jsonescape": jsonescape, - "localdate": localdate, "lower": lower, "nonempty": nonempty, "obfuscate": obfuscate, diff --git a/mercurial/templater.py b/mercurial/templater.py --- a/mercurial/templater.py +++ b/mercurial/templater.py @@ -516,6 +516,21 @@ def label(context, mapping, args): # 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 @@ funcs = { "indent": indent, "join": join, "label": label, + "localdate": localdate, "pad": pad, "revset": revset, "rstdoc": rstdoc, diff --git a/tests/test-command-template.t b/tests/test-command-template.t --- a/tests/test-command-template.t +++ b/tests/test-command-template.t @@ -2495,6 +2495,10 @@ Behind the scenes, this will throw Attri 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'