37 lines
No EOL
1.2 KiB
Python
37 lines
No EOL
1.2 KiB
Python
from datetime import datetime
|
|
|
|
def timeago(dt):
|
|
"""
|
|
Returns a human-readable string representing time difference between now and the given datetime.
|
|
|
|
For example: "2 minutes ago", "3 hours ago", "5 days ago", etc.
|
|
"""
|
|
now = datetime.utcnow()
|
|
diff = now - dt
|
|
|
|
seconds = diff.total_seconds()
|
|
|
|
if seconds < 60:
|
|
return "just now"
|
|
elif seconds < 3600:
|
|
minutes = int(seconds / 60)
|
|
return f"{minutes} minute{'s' if minutes != 1 else ''} ago"
|
|
elif seconds < 86400:
|
|
hours = int(seconds / 3600)
|
|
return f"{hours} hour{'s' if hours != 1 else ''} ago"
|
|
elif seconds < 604800:
|
|
days = int(seconds / 86400)
|
|
return f"{days} day{'s' if days != 1 else ''} ago"
|
|
elif seconds < 2592000:
|
|
weeks = int(seconds / 604800)
|
|
return f"{weeks} week{'s' if weeks != 1 else ''} ago"
|
|
elif seconds < 31536000:
|
|
months = int(seconds / 2592000)
|
|
return f"{months} month{'s' if months != 1 else ''} ago"
|
|
else:
|
|
years = int(seconds / 31536000)
|
|
return f"{years} year{'s' if years != 1 else ''} ago"
|
|
|
|
def init_app(app):
|
|
"""Register filters with the Flask app"""
|
|
app.jinja_env.filters['timeago'] = timeago |