From 413f343d5eaec560a623c732964dff91b3876c7f Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:25:12 +0530 Subject: [PATCH] fix(time-rounding): round exact halves up with the nearest method round() rounds .5 to the even integer, so at 30-minute rounding a 15-minute entry was stored as 0 and a 75-minute entry as 60, while 45 became 60. --- app/utils/time_rounding.py | 4 ++-- tests/test_time_rounding_param.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/utils/time_rounding.py b/app/utils/time_rounding.py index 40d227af..8b5975e3 100644 --- a/app/utils/time_rounding.py +++ b/app/utils/time_rounding.py @@ -42,8 +42,8 @@ def round_time_duration(duration_seconds: int, rounding_minutes: int = 1, roundi rounded_minutes = math.ceil(duration_minutes / rounding_minutes) * rounding_minutes elif rounding_method == "down": rounded_minutes = math.floor(duration_minutes / rounding_minutes) * rounding_minutes - else: # 'nearest' - rounded_minutes = round(duration_minutes / rounding_minutes) * rounding_minutes + else: # 'nearest', halves round up (round() would round them to even) + rounded_minutes = math.floor(duration_minutes / rounding_minutes + 0.5) * rounding_minutes # Convert back to seconds return int(rounded_minutes * 60) diff --git a/tests/test_time_rounding_param.py b/tests/test_time_rounding_param.py index 45976c6d..09a2ad34 100644 --- a/tests/test_time_rounding_param.py +++ b/tests/test_time_rounding_param.py @@ -22,6 +22,8 @@ pytest.param(3720, 60, "nearest", 3600, id="62m->nearest-60m=60m"), pytest.param(5400, 60, "nearest", 7200, id="90m->nearest-60m=120m"), pytest.param(5340, 60, "nearest", 3600, id="89m->nearest-60m=60m"), + pytest.param(900, 30, "nearest", 1800, id="15m->nearest-30m=30m"), + pytest.param(9000, 60, "nearest", 10800, id="150m->nearest-60m=180m"), ], ) def test_round_time_duration_parametrized(seconds, interval, method, expected):