Guest User

Untitled

a guest
Apr 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. from datetime import datetime
  2.  
  3.  
  4. def _ordinal(day):
  5. SUFFIX = {
  6. 1: 'st',
  7. 2: 'nd',
  8. 3: 'rd',
  9. }
  10. if day in [11, 12, 13]:
  11. suffix = 'th'
  12. else:
  13. last_digit = int(str(day)[-1])
  14. suffix = SUFFIX.get(last_digit, 'th')
  15. return '{day}{suffix}'.format(day=day, suffix=suffix)
  16.  
  17.  
  18. def _hour_to_str(date):
  19. return date.strftime('%-I %p')
  20.  
  21.  
  22. def _str_to_date(date_str):
  23. return datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%SZ")
  24.  
  25.  
  26. def _month_str(days, month):
  27. return '{days} {month}'.format(
  28. days=', '.join(days),
  29. month=month,
  30. )
  31.  
  32.  
  33. def humanizeTransmissionDates(datesAsStrings):
  34. date_parts = []
  35. current_month = []
  36. month_parts = []
  37.  
  38. for date_str in datesAsStrings:
  39. dt = _str_to_date(date_str)
  40. month = dt.strftime('%B')
  41. day = _ordinal(dt.day)
  42.  
  43. if month == current_month:
  44. month_parts.append(day)
  45. else:
  46. if current_month:
  47. date_parts.append(_month_str(days=month_parts, month=current_month))
  48.  
  49. month_parts = [day]
  50. current_month = month
  51.  
  52. time_str = _hour_to_str(dt)
  53.  
  54.  
  55.  
  56. if date_parts:
  57. humanized = '{all_months}, and '.format(
  58. all_months=', '.join(date_parts),
  59. )
  60. else:
  61. humanized = ''
  62.  
  63. humanized = '{humanized}{last_month} at {time_str}'.format(
  64. humanized=humanized,
  65. last_month=_month_str(days=month_parts, month=current_month),
  66. time_str=time_str,
  67. )
  68. return humanized
Add Comment
Please, Sign In to add comment