diff mercurial/templateutil.py @ 37226: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 54355c243042
children e70a90a72b80
line wrap: on
line diff
--- 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)