Patchwork [4,of,5] templater: port localdate filter to a function

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 1, 2015, 1:25 p.m.
Message ID <7c5bb6ce0f2bb12648b7.1441113914@mimosa>
Download mbox | patch
Permalink /patch/10344/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 1, 2015, 1:25 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# 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.
timeless - Sept. 1, 2015, 3:15 p.m.
> +        raise error.ParseError(_("localdate expects a date information"))

This should be word-smithed.

"a date" (it's singular), works; "date information" works (information is
more or less plural here, and date is an adjective); "a date information"
doesn't work (mixing singular and plural)

You could add "object" to the end, or replace the word information with it;
you could possibly omit "a".
Augie Fackler - Sept. 1, 2015, 5:01 p.m.
On Tue, Sep 01, 2015 at 11:15:11AM -0400, timeless wrote:
> > +        raise error.ParseError(_("localdate expects a date information"))
>
> This should be word-smithed.
>
> "a date" (it's singular), works; "date information" works (information is
> more or less plural here, and date is an adjective); "a date information"
> doesn't work (mixing singular and plural)
>
> You could add "object" to the end, or replace the word information with it;
> you could possibly omit "a".

I'm about to push this to http://hg.netv6.net/clowncopter - want to
send me a patch doing said wordsmithing?


> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> https://selenic.com/mailman/listinfo/mercurial-devel
Yuya Nishihara - Sept. 2, 2015, 2:43 p.m.
On Tue, 1 Sep 2015 11:15:11 -0400, timeless wrote:
> > +        raise error.ParseError(_("localdate expects a date information"))
> 
> This should be word-smithed.
> 
> "a date" (it's singular), works; "date information" works (information is
> more or less plural here, and date is an adjective); "a date information"
> doesn't work (mixing singular and plural)
> 
> You could add "object" to the end, or replace the word information with it;
> you could possibly omit "a".

Oops. I can just say "a date" here because the localdate function accepts
both (time, offset) tuple and a date text.

Is there a better term for (time, offset) tuple? Currently it is called as
"date information", but it seems unclear to me.

Patch

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'