Mercurial > hg
changeset 37224:67efce231633
templater: factor out function that parses argument as date tuple
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 18 Mar 2018 15:55:31 +0900 |
parents | 08e042f0a67c |
children | e70a90a72b80 |
files | mercurial/templatefuncs.py mercurial/templateutil.py |
diffstat | 2 files changed, 17 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/templatefuncs.py Sun Mar 18 15:42:28 2018 +0900 +++ b/mercurial/templatefuncs.py Sun Mar 18 15:55:31 2018 +0900 @@ -34,6 +34,7 @@ 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 @@ # 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])
--- a/mercurial/templateutil.py Sun Mar 18 15:42:28 2018 +0900 +++ b/mercurial/templateutil.py Sun Mar 18 15:55:31 2018 +0900 @@ -16,6 +16,7 @@ util, ) from .utils import ( + dateutil, stringutil, ) @@ -318,6 +319,18 @@ # 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)