Fix calculation of totals longer than a day

datetime.timedelta stores days, seconds and microseconds. Therefore the
seconds attribute only stores timedeltas of less than 24 hours. Use
total_seconds() instead which properly accounts for the days part.
This commit is contained in:
Michael Eischer 2019-10-12 18:57:00 +02:00 committed by lauft
parent cd0d5d175e
commit 8053ccf68e
2 changed files with 39 additions and 4 deletions

View file

@ -133,13 +133,15 @@ def calculate_totals(input_stream):
# Compose table rows. # Compose table rows.
grand_total = 0 grand_total = 0
for tag in sorted(totals): for tag in sorted(totals):
formatted = format_seconds(totals[tag].seconds) seconds = int(totals[tag].total_seconds())
grand_total += totals[tag].seconds formatted = format_seconds(seconds)
grand_total += seconds
output.append("{:{width}} {:10}".format(tag, formatted, width=max_width)) output.append("{:{width}} {:10}".format(tag, formatted, width=max_width))
if untagged is not None: if untagged is not None:
formatted = format_seconds(untagged.seconds) seconds = int(untagged.total_seconds())
grand_total += untagged.seconds formatted = format_seconds(seconds)
grand_total += seconds
output.append("{:{width}} {:10}".format("", formatted, width=max_width)) output.append("{:{width}} {:10}".format("", formatted, width=max_width))
# Compose total. # Compose total.

View file

@ -89,6 +89,39 @@ class TestTotals(TestCase):
], ],
out) out)
def test_totals_with_time_delta_larger_than_24_hours(self):
"""totals extension should print report for time delta larger than 24 hours"""
now = datetime.datetime.now()
two_days_before = now - datetime.timedelta(days=2)
now_utc = now.utcnow()
two_days_before_utc = now_utc - datetime.timedelta(days=2)
input_stream = [
'color: off\n',
'debug: on\n',
'temp.report.start: {:%Y%m%dT%H%M%S}Z\n'.format(two_days_before_utc),
'temp.report.end: {:%Y%m%dT%H%M%S}Z\n'.format(now_utc),
'\n',
'[{{"start":"{:%Y%m%dT%H%M%S}Z","end":"{:%Y%m%dT%H%M%S}Z","tags":["foo"]}}]'.format(two_days_before_utc, now_utc)
]
out = calculate_totals(input_stream)
self.assertEqual(
[
'',
'Total by Tag, for {:%Y-%m-%d %H:%M:%S} - {:%Y-%m-%d %H:%M:%S}'.format(two_days_before, now),
'',
'Tag Total',
'----- ----------',
'foo 48:00:00',
' ----------',
'Total 48:00:00',
'',
],
out)
def test_totals_with_emtpy_range(self): def test_totals_with_emtpy_range(self):
"""totals extension should report error on emtpy range""" """totals extension should report error on emtpy range"""
now = datetime.datetime.now() now = datetime.datetime.now()