ChatList: Improve timedelta function
Versions
- OS: Arch Linux
- Gajim version: 1.5.2
- GTK version: 3.24.34
- Python-nbxmpp version: 3.2.4
Steps to reproduce the problem
- Check the date shown in the ChatList for the last message in a chat/muc:
Expected behavior + Actual behavior
Depending on the time between now and the last message the time is shown in different formats ("Just now", "10:57", "Yesterday", etc.) and i think there is a bug (Case 1) and room for improvement (Case 2):
Case 1:
Time now: 2022-10-10 02:00:15 (all times in 24h format)
Time last message: 2022-10-08 10:00:00
Time delta: [1 day, 16:00:15]
Result: "Yesterday"
The time delta is less than 2 days but it is the day before yesterday, so i think this is a bug.
Case 2:
Time now: 2022-10-10 00:00:15 (all times in 24h format)
Time last message: 2022-10-09 23:59:45
Time delta: [0:00:30] (30s)
Result: "Yesterday"
While technically correct, i think using "Just now" or "X minutes ago" would be better than "Yesterday" if the message was received a few minutes ago.
Proposed solution
The function doing this is located here: https://dev.gajim.org/gajim/gajim/-/blob/master/gajim/common/helpers.py#L256
I tried to improve it and came up with this:
if timespan < timedelta(minutes=1):
return 'Just now'
if timespan < timedelta(minutes=15):
minutes = int(timespan.seconds / 60)
return f'{minutes} min ago'
if timespan < timedelta(days=7):
if date_time.strftime('%d') == now.strftime('%d'): # today
return date_time.strftime('%H:%M')
if timespan < timedelta(days=1) or \
timespan < timedelta(days=2) and now.time() > date_time.time():
return ('Yesterday')
return date_time.strftime('%a')
if timespan < timedelta(days=365):
return date_time.strftime('%b %d')
return str(date_time.year)
I opened this issue instead of a MR to get some more input so what do you think?