generate_relative_date.py 569 B

12345678910111213141516171819202122
  1. from datetime import datetime
  2. def _generate_relative_date(date: datetime):
  3. now = datetime.now()
  4. diff = now - date
  5. if diff.days > 0:
  6. return f"{diff.days} days ago"
  7. elif diff.seconds > 3600:
  8. return f"{diff.seconds // 3600} hours ago"
  9. elif diff.seconds > 60:
  10. return f"{diff.seconds // 60} minutes ago"
  11. else:
  12. return f"{diff.seconds} seconds ago"
  13. def generate_relative_date(date: datetime):
  14. try:
  15. return _generate_relative_date(date)
  16. except Exception as e:
  17. print(e)
  18. return "Unknown"