def deftime(context, dateval, extra=False, switch_point=DEFTIME_SWITCH_POINT): """ The 'default way of printing the date'. Use extra=True for the part that goes into the title or something. Summary: prints reltime for dates less than configured period (def. 24h) and timestamp for other dates, or reverse of that for title. """ now = context['now'] datespan = now - time.mktime(dateval.timetuple()) oldenough = datespan > switch_point # datets - now < - switching_point if bool(extra) != bool(oldenough): # XOR return dateval.strftime("%Y.%m.%d %H:%M:%S") else: return format_timedelta(datespan)
def render(self, context): now = context.get('now', None) extrakwa = {} if now is None: # Should never appen if template context processor # is providing it. now = time.mktime(datetime.datetime.now().timetuple()) context['now'] = now try: ndate = self.ndate.resolve(context) if self.nlen is not None: extrakwa['maxlen'] = \ template.Variable(self.nlen).resolve(context) except template.VariableDoesNotExist: return u'' if not isinstance(ndate, datetime.datetime): return (u'?sometime?') return format_timedelta(now - time.mktime(ndate.timetuple()), **extrakwa)