# HG changeset patch # User Yuya Nishihara # Date 2018-03-18 06:55:31 # Node ID 67efce231633d0715001b26075ce61c239105197 # Parent 08e042f0a67c15e07e04d0112753794c4345c1e7 templater: factor out function that parses argument as date tuple 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)