feat: better 'time ago'

This commit is contained in:
Ayo Ayco 2024-04-28 10:04:02 +02:00
parent 366d5d1a0c
commit 2e8fd9b444
2 changed files with 12 additions and 9 deletions

View file

@ -208,7 +208,7 @@
</h3>
<div class="right_menu">
<a href="{{ thread.url }}" title="{{ thread.created_at }}">{{ thread.created_at | time_ago }}</a> &middot;
<a href="{{ thread.url }}" title="{{ thread.created_at | format_date }}">{{ thread.created_at | time_ago }}</a> &middot;
<a href="{{ url_for('threads.thread', id=thread['id']) + '#' + thread['id'] }}">Anchor</a>
</div>
</div>
@ -264,7 +264,7 @@
</h3>
<div class="right_menu">
<a href="{{ descendant.url }}" title="{{ descendant.created_at }}">{{ descendant.created_at | time_ago }}</a> &middot;
<a href="{{ descendant.url }}" title="{{ descendant.created_at | format_date }}">{{ descendant.created_at | time_ago }}</a> &middot;
<a href="{{ url_for('threads.thread', id=thread['id']) + '#' + descendant['id'] }}">Anchor</a>
</div>
</div>

View file

@ -34,16 +34,19 @@ def time_ago(date):
now = datetime.now()
date_obj = datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ')
delta = now - date_obj
delta = delta - timedelta(hours=2)
days = delta.days
seconds = delta.seconds
if seconds < 3600:
return str(seconds / 60).split('.')[0] + ' minutes ago'
elif days == 0:
hours = timedelta(seconds=seconds)
return str(hours).split(':')[0] + ' hours ago'
if days == 0:
return 'just today'
elif days == 1:
return 'yesterday'
elif days < 7:
return str(days) + ' days ago'
elif days < 28:
weeks = int(days) // 7
if (weeks == 1):
return 'a week ago'
else:
return str(weeks) + ' weeks ago'
return date_obj.strftime('%b %d, %Y')
@threads.app_template_filter('format_date')