From 13278b4a6853366dbdf4dd61172d639a969842fb Mon Sep 17 00:00:00 2001 From: FugoP <264910004+AgentGymLeader@users.noreply.github.com> Date: Wed, 10 Jun 2026 09:41:47 +0900 Subject: [PATCH] fix: add week unit support to get_next_standardized_reset_time The function handled d/h/m/s/mo units but silently fell through to the default next-midnight branch for the w (week) unit. This was inconsistent: _extract_from_regex already accepted w in its character class, and duration_in_seconds already returned value * 604800 for it. Add the missing elif unit == 'w' branch that delegates to _handle_day_reset with value * 7, which reuses the existing Monday- alignment logic for 1w and the generic N-day-from-midnight path for larger multiples. Add test_week_based_resets covering 1w from a Wednesday (expects next Monday) and 2w from a Monday (expects 14 days forward at midnight). Signed-off-by: FugoP <264910004+AgentGymLeader@users.noreply.github.com> --- litellm/litellm_core_utils/duration_parser.py | 2 ++ .../litellm_core_utils/test_duration_parser.py | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/litellm/litellm_core_utils/duration_parser.py b/litellm/litellm_core_utils/duration_parser.py index 6d2b4226ff4..036d691c686 100644 --- a/litellm/litellm_core_utils/duration_parser.py +++ b/litellm/litellm_core_utils/duration_parser.py @@ -131,6 +131,8 @@ def get_next_standardized_reset_time( # Handle different time units if unit == "d": return _handle_day_reset(current_time, base_midnight, value, tz) + elif unit == "w": + return _handle_day_reset(current_time, base_midnight, value * 7, tz) elif unit == "h": return _handle_hour_reset(current_time, base_midnight, value) elif unit == "m": diff --git a/tests/test_litellm/litellm_core_utils/test_duration_parser.py b/tests/test_litellm/litellm_core_utils/test_duration_parser.py index d95503665ec..f7a86fb032b 100644 --- a/tests/test_litellm/litellm_core_utils/test_duration_parser.py +++ b/tests/test_litellm/litellm_core_utils/test_duration_parser.py @@ -34,6 +34,17 @@ class TestStandardizedResetTime(unittest.TestCase): custom_day_result = get_next_standardized_reset_time("3d", base_time, "UTC") self.assertEqual(custom_day_result, custom_day_expected) + def test_week_based_resets(self): + wednesday = datetime(2023, 5, 17, 15, 45, 0, tzinfo=timezone.utc) + weekly_expected = datetime(2023, 5, 22, 0, 0, 0, tzinfo=timezone.utc) + weekly_result = get_next_standardized_reset_time("1w", wednesday, "UTC") + self.assertEqual(weekly_result, weekly_expected) + + base_time = datetime(2023, 5, 15, 10, 30, 0, tzinfo=timezone.utc) + two_week_expected = datetime(2023, 5, 29, 0, 0, 0, tzinfo=timezone.utc) + two_week_result = get_next_standardized_reset_time("2w", base_time, "UTC") + self.assertEqual(two_week_result, two_week_expected) + def test_hour_minute_second_resets(self): """Test hour, minute, and second based reset durations""" # Base time: 2023-05-15 15:20:30 UTC (3:20:30 PM)