mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-10 15:32:38 -07:00
Update human_duration helper function
This commit is contained in:
parent
bf1a59c5c0
commit
3f6612fe9a
2 changed files with 19 additions and 13 deletions
|
@ -290,8 +290,8 @@ class DataFactory(object):
|
||||||
'recordsTotal': query['totalCount'],
|
'recordsTotal': query['totalCount'],
|
||||||
'data': session.friendly_name_to_username(rows),
|
'data': session.friendly_name_to_username(rows),
|
||||||
'draw': query['draw'],
|
'draw': query['draw'],
|
||||||
'filter_duration': helpers.human_duration(filter_duration, sig='dhm'),
|
'filter_duration': helpers.human_duration(filter_duration, sig='dhm', units='s'),
|
||||||
'total_duration': helpers.human_duration(total_duration, sig='dhm')
|
'total_duration': helpers.human_duration(total_duration, sig='dhm', units='s')
|
||||||
}
|
}
|
||||||
|
|
||||||
return dict
|
return dict
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# This file is part of Tautulli.
|
# This file is part of Tautulli.
|
||||||
#
|
#
|
||||||
|
@ -250,30 +250,36 @@ def datetime_to_iso(dt, to_date=False):
|
||||||
return dt
|
return dt
|
||||||
|
|
||||||
|
|
||||||
def human_duration(s, sig='dhms'):
|
def human_duration(ms, sig='dhms', units='ms'):
|
||||||
|
factors = {'d': 86400000,
|
||||||
|
'h': 3600000,
|
||||||
|
'm': 60000,
|
||||||
|
's': 1000,
|
||||||
|
'ms': 1}
|
||||||
|
|
||||||
hd = ''
|
if str(ms).isdigit() and ms > 0:
|
||||||
|
ms = ms * factors[units]
|
||||||
|
|
||||||
if str(s).isdigit() and s > 0:
|
d, h = divmod(ms, factors['d'])
|
||||||
d, h = divmod(s, 86400)
|
h, m = divmod(h, factors['h'])
|
||||||
h, m = divmod(h, 3600)
|
m, s = divmod(m, factors['m'])
|
||||||
m, s = divmod(m, 60)
|
s, ms = divmod(s, factors['s'])
|
||||||
|
|
||||||
hd_list = []
|
hd_list = []
|
||||||
if sig >= 'd' and d > 0:
|
if sig >= 'd' and d > 0:
|
||||||
d = d + 1 if sig == 'd' and h >= 12 else d
|
d = d + 1 if sig == 'd' and h >= 12 else d
|
||||||
hd_list.append(str(d) + ' days')
|
hd_list.append(str(d) + ' day' + ('s' if d > 1 else ''))
|
||||||
|
|
||||||
if sig >= 'dh' and h > 0:
|
if sig >= 'dh' and h > 0:
|
||||||
h = h + 1 if sig == 'dh' and m >= 30 else h
|
h = h + 1 if sig == 'dh' and m >= 30 else h
|
||||||
hd_list.append(str(h) + ' hrs')
|
hd_list.append(str(h) + ' hr' + ('s' if h > 1 else ''))
|
||||||
|
|
||||||
if sig >= 'dhm' and m > 0:
|
if sig >= 'dhm' and m > 0:
|
||||||
m = m + 1 if sig == 'dhm' and s >= 30 else m
|
m = m + 1 if sig == 'dhm' and s >= 30 else m
|
||||||
hd_list.append(str(m) + ' mins')
|
hd_list.append(str(m) + ' min' + ('s' if m > 1 else ''))
|
||||||
|
|
||||||
if sig >= 'dhms' and s > 0:
|
if sig >= 'dhms' and s > 0:
|
||||||
hd_list.append(str(s) + ' secs')
|
hd_list.append(str(s) + ' sec' + ('s' if s > 1 else ''))
|
||||||
|
|
||||||
hd = ' '.join(hd_list)
|
hd = ' '.join(hd_list)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue