more commits..
This commit is contained in:
parent
6dda02141e
commit
7823be6481
20 changed files with 1835 additions and 631 deletions
37
app/filters.py
Normal file
37
app/filters.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
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
|
Loading…
Add table
Add a link
Reference in a new issue