Patchwork [04,of,11] templater: factor out function that parses argument as date tuple

login
register
mail settings
Submitter Yuya Nishihara
Date March 31, 2018, 1:49 a.m.
Message ID <01d55beea227cca5b033.1522460972@mimosa>
Download mbox | patch
Permalink /patch/30040/
State Accepted
Headers show

Comments

Yuya Nishihara - March 31, 2018, 1:49 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521356131 -32400
#      Sun Mar 18 15:55:31 2018 +0900
# Node ID 01d55beea227cca5b033cfe9dd4a0a068aad5b8c
# Parent  5b226692c357623256819fc6ab2abead595734bc
templater: factor out function that parses argument as date tuple

Patch

diff --git a/mercurial/templatefuncs.py b/mercurial/templatefuncs.py
--- a/mercurial/templatefuncs.py
+++ b/mercurial/templatefuncs.py
@@ -34,6 +34,7 @@  from .utils import (
 evalrawexp = templateutil.evalrawexp
 evalfuncarg = templateutil.evalfuncarg
 evalboolean = templateutil.evalboolean
+evaldate = templateutil.evaldate
 evalinteger = templateutil.evalinteger
 evalstring = templateutil.evalstring
 evalstringliteral = templateutil.evalstringliteral
@@ -373,12 +374,9 @@  def localdate(context, mapping, args):
         # i18n: "localdate" is a keyword
         raise error.ParseError(_("localdate expects one or two arguments"))
 
-    date = evalfuncarg(context, mapping, args[0])
-    try:
-        date = dateutil.parsedate(date)
-    except AttributeError:  # not str nor date tuple
-        # i18n: "localdate" is a keyword
-        raise error.ParseError(_("localdate expects a date information"))
+    date = evaldate(context, mapping, args[0],
+                    # i18n: "localdate" is a keyword
+                    _("localdate expects a date information"))
     if len(args) >= 2:
         tzoffset = None
         tz = evalfuncarg(context, mapping, args[1])
diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -16,6 +16,7 @@  from . import (
     util,
 )
 from .utils import (
+    dateutil,
     stringutil,
 )
 
@@ -318,6 +319,18 @@  def evalboolean(context, mapping, arg):
     # empty dict/list should be False as they are expected to be ''
     return bool(stringify(thing))
 
+def evaldate(context, mapping, arg, err=None):
+    """Evaluate given argument as a date tuple or a date string; returns
+    a (unixtime, offset) tuple"""
+    return unwrapdate(evalrawexp(context, mapping, arg), err)
+
+def unwrapdate(thing, err=None):
+    thing = _unwrapvalue(thing)
+    try:
+        return dateutil.parsedate(thing)
+    except AttributeError:
+        raise error.ParseError(err or _('not a date tuple nor a string'))
+
 def evalinteger(context, mapping, arg, err=None):
     return unwrapinteger(evalrawexp(context, mapping, arg), err)