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> </h3>
<div class="right_menu"> <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> <a href="{{ url_for('threads.thread', id=thread['id']) + '#' + thread['id'] }}">Anchor</a>
</div> </div>
</div> </div>
@ -264,7 +264,7 @@
</h3> </h3>
<div class="right_menu"> <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> <a href="{{ url_for('threads.thread', id=thread['id']) + '#' + descendant['id'] }}">Anchor</a>
</div> </div>
</div> </div>

View file

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