comparison mercurial/templateutil.py @ 38297:8d6109b49b31

templater: introduce a wrapper for date tuple (BC) Strictly speaking, this is BC, but I believe the original string format (str(float(unixtime)) + str(int(tzoffset))) was just plain wrong.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 24 Mar 2018 17:54:02 +0900
parents e72697893c93
children 851fc9d42d6d
comparison
equal deleted inserted replaced
38296:e72697893c93 38297:8d6109b49b31
170 return pycompat.bytestr(self._value) 170 return pycompat.bytestr(self._value)
171 171
172 def tovalue(self, context, mapping): 172 def tovalue(self, context, mapping):
173 return self._value 173 return self._value
174 174
175 # stub for representing a date type; may be a real date type that can 175 class date(wrapped):
176 # provide a readable string value 176 """Wrapper for date tuple"""
177 class date(object): 177
178 pass 178 def __init__(self, value):
179 # value may be (float, int), but public interface shouldn't support
180 # floating-point timestamp
181 self._unixtime, self._tzoffset = map(int, value)
182
183 def contains(self, context, mapping, item):
184 raise error.ParseError(_('date is not iterable'))
185
186 def getmember(self, context, mapping, key):
187 raise error.ParseError(_('date is not a dictionary'))
188
189 def getmin(self, context, mapping):
190 raise error.ParseError(_('date is not iterable'))
191
192 def getmax(self, context, mapping):
193 raise error.ParseError(_('date is not iterable'))
194
195 def itermaps(self, context):
196 raise error.ParseError(_("date is not iterable"))
197
198 def join(self, context, mapping, sep):
199 raise error.ParseError(_("date is not iterable"))
200
201 def show(self, context, mapping):
202 return '%d %d' % (self._unixtime, self._tzoffset)
203
204 def tovalue(self, context, mapping):
205 return (self._unixtime, self._tzoffset)
179 206
180 class hybrid(wrapped): 207 class hybrid(wrapped):
181 """Wrapper for list or dict to support legacy template 208 """Wrapper for list or dict to support legacy template
182 209
183 This class allows us to handle both: 210 This class allows us to handle both:
641 a (unixtime, offset) tuple""" 668 a (unixtime, offset) tuple"""
642 thing = evalrawexp(context, mapping, arg) 669 thing = evalrawexp(context, mapping, arg)
643 return unwrapdate(context, mapping, thing, err) 670 return unwrapdate(context, mapping, thing, err)
644 671
645 def unwrapdate(context, mapping, thing, err=None): 672 def unwrapdate(context, mapping, thing, err=None):
673 if isinstance(thing, date):
674 return thing.tovalue(context, mapping)
675 # TODO: update hgweb to not return bare tuple; then just stringify 'thing'
646 thing = unwrapvalue(context, mapping, thing) 676 thing = unwrapvalue(context, mapping, thing)
647 try: 677 try:
648 return dateutil.parsedate(thing) 678 return dateutil.parsedate(thing)
649 except AttributeError: 679 except AttributeError:
650 raise error.ParseError(err or _('not a date tuple nor a string')) 680 raise error.ParseError(err or _('not a date tuple nor a string'))