def timeslice(d, now = None):
# Convert datetime.date to datetime.datetime for comparison.
if not d:
return ''
if not isinstance(d, datetime.datetime):
d = datetime.datetime(d.year, d.month, d.day)
if now and not isinstance(now, datetime.datetime):
now = datetime.datetime(now.year, now.month, now.day)
if not now:
if d.tzinfo:
now = datetime.datetime.now(LocalTimezone(d))
else:
now = datetime.datetime.now()
# ignore microsecond part of 'd' since we removed it from 'now'
delta = now - (d - datetime.timedelta(0, 0, d.microsecond))
since = delta.days * 24 * 60 * 60 + delta.seconds
if since // (60 * 60 * 24) < 3:
return _("%s ago") % _timesince(d)
return _date(d, "Y-m-d H:i")