def smartdate(context, mapping, args): """Date. Returns one of two values depending on whether the date provided is in the past and recent or not.""" date = templater.evalfuncarg(context, mapping, args[0]) threshold = templater.evalinteger(context, mapping, args[1]) now = time.time() then = date[0] if now - threshold <= then <= now: return templater.evalstring(context, mapping, args[2]) else: return templater.evalstring(context, mapping, args[3])
def simpledate(context, mapping, args): """Date. Returns a human-readable date/time that is simplified for dates and times in the recent past. """ ctx = mapping["ctx"] repo = ctx.repo() date = templater.evalfuncarg(context, mapping, args[0]) tz = None if len(args) == 2: tzname = templater.evalstring(context, mapping, args[1]) if tzname: try: import pytz tz = pytz.timezone(tzname) except ImportError: msg = "Couldn't import pytz, using default time zone\n" repo.ui.warn(msg) except pytz.UnknownTimeZoneError: msg = "Unknown time zone: %s\n" % tzname repo.ui.warn(msg) then = datetime.datetime.fromtimestamp(date[0], tz) now = datetime.datetime.now(tz) td = now.date() - then.date() if then > now: # Time is in the future, render it in full return then.strftime("%Y-%m-%d %H:%M") elif td.days == 0: # Today ("Today at HH:MM") return then.strftime("Today at %H:%M") elif td.days == 1: # Yesterday ("Yesterday at HH:MM") return then.strftime("Yesterday at %H:%M") elif td.days <= 6: # In the last week (e.g. "Monday at HH:MM") return then.strftime("%A at %H:%M") elif now.year == then.year or td.days <= 90: # This year or in the last 3 months (e.g. "Jan 05 at HH:MM") return then.strftime("%b %d at %H:%M") else: # Before, render it in full return then.strftime("%Y-%m-%d %H:%M")