The algorithm used for time formatting is:
""" 1. if not in the same year '%Y year %m month %d day' 2. If in the same year 2.1 If in the same month 2.1.1 If on the same day '%H:%M' 2.1.2 If yesterday 'Yesterday %H:%M' 2.1.2 if in the same week 'week x 00:00' Remove the case of Sunday 2.2 Otherwise '%m month %d day %H:%M' """
The exact python code is below:
def fmtdt_str(dtstr, fmt): result = "" (locale.LC_CTYPE, 'chinese') curtime = () curYear = curMonth = str_time = (dtstr, fmt) if str_time.year == curYear: if str_time.month == curMonth: days_interval = ( - str_time.day) if days_interval == 0: result = str_time.strftime("%H:%M") elif days_interval == 1: result = str_time.strftime("Yesterday %H:%M") else: if ("%W") == str_time.strftime("%W"): week_str = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] str_weekno = str_time.weekday() if str_weekno == 0: result = str_time.strftime("%m month %d day %H:%M") else: result = str_time.strftime(week_str[str_weekno] + " %H:%M") else: result = str_time.strftime("%m month %d day %H:%M") else: result = str_time.strftime("%m month %d day %H:%M") else: result = str_time.strftime("%Y year %m month %d day") return result
summarize
to this article on the python implementation of the imitation microblogging chat time formatting code is introduced to this article, more related python time formatting content please search for my previous posts or continue to browse the following related articles I hope you will support me more in the future!