forked from Tivix/django-cron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
32 lines (24 loc) · 913 Bytes
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from django.utils.translation import gettext_lazy as _
def humanize_duration(duration):
"""
Returns a humanized string representing time difference
For example: 2 days 1 hour 25 minutes 10 seconds
"""
days = duration.days
hours = duration.seconds / 3600
minutes = duration.seconds % 3600 / 60
seconds = duration.seconds % 3600 % 60
parts = []
if days > 0:
parts.append(u'%s %s' % (days, _('day') if days == 1 else _('days')))
if hours > 0:
parts.append(u'%s %s' % (hours, _('hour') if hours == 1 else _('hours')))
if minutes > 0:
parts.append(
u'%s %s' % (minutes, _('minute') if minutes == 1 else _('minutes'))
)
if seconds > 0:
parts.append(
u'%s %s' % (seconds, _('second') if seconds == 1 else _('seconds'))
)
return ' '.join(parts) if len(parts) != 0 else _('< 1 second')