def pretty_time(time, now=datetime.datetime.now()):
tomorrow = now + datetime.timedelta(days=1)
next_day = now + datetime.timedelta(days=2)
next_week = now + datetime.timedelta(weeks=1)
pretty_fmt = '%-I:%M %p'
pretty_prefix = ''
if tomorrow < time < next_day:
pretty_prefix = 'tomorrow at '
elif time > next_day and time < next_week:
pretty_prefix = '%A at '
elif time > next_week and time.month == now.month and time.year == now.year:
pretty_prefix = '%A the %-d' + pretty_suffix(time.day) + ' at '
elif time > next_week and time.year == now.year:
pretty_prefix = '%B %-d' + pretty_suffix(time.day) + ' at '
elif time > next_week:
pretty_prefix = '%B %-d' + pretty_suffix(time.day) + ' %Y at '
return time.strftime(pretty_prefix + pretty_fmt)
评论列表
文章目录