diff --git a/apps/schedule/schedule_xml.py b/apps/schedule/schedule_xml.py index ec104ee37..c06b966e4 100644 --- a/apps/schedule/schedule_xml.py +++ b/apps/schedule/schedule_xml.py @@ -17,7 +17,11 @@ def get_duration(start_time, end_time): duration = (end_time - start_time).total_seconds() / 60 hours = int(duration // 60) minutes = int(duration % 60) - return "{0:01d}:{1:02d}".format(hours, minutes) + if hours < 24: + return f"{hours:d}:{minutes:02d}" + days = int(hours // 24) + hours = int(hours % 24) + return f"{days:d}:{hours:02d}:{minutes:02d}" def get_day_start_end(dt, start_time=time(4, 0)): diff --git a/tests/test_schedule.py b/tests/test_schedule.py new file mode 100644 index 000000000..18550113b --- /dev/null +++ b/tests/test_schedule.py @@ -0,0 +1,20 @@ +from datetime import datetime + +import pytest + +from apps.schedule import schedule_xml + + +@pytest.mark.parametrize('start_time, end_time, expected', [ + ('2024-01-01 00:00:00', '2024-01-01 00:01:00', '0:01'), + ('2024-01-01 00:00:00', '2024-01-01 00:01:45', '0:01'), + ('2024-01-01 00:00:00', '2024-01-01 01:00:00', '1:00'), + ('2024-01-01 00:00:00', '2024-01-01 12:00:00', '12:00'), + ('2024-01-01 00:00:00', '2024-01-02 00:00:00', '1:00:00'), + ('2024-01-01 00:00:00', '2024-01-02 12:34:00', '1:12:34'), +]) +def test_get_duration(start_time, end_time, expected): + fmt = '%Y-%m-%d %H:%M:%S' + start_time = datetime.strptime(start_time, fmt) + end_time = datetime.strptime(end_time, fmt) + assert schedule_xml.get_duration(start_time, end_time) == expected