From 20b05c14df4e65b91e19a38d36b3aca474d6e499 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 14 Jul 2026 15:05:06 +0000 Subject: [PATCH] Fix llm_time timezone_offset format for negative UTC offsets Two bugs in the original computation: floor division on negative offset_seconds gave wrong hours for sub-hour offsets (e.g. UTC-3:30 became UTC-4:50), and the :02d format specifier does not zero-pad negative numbers (so UTC-5 produced "UTC-5:00" instead of "UTC-05:00"). Fix by working with abs(offset_seconds) for the hour/minute split and tracking the sign separately, then formatting with f"UTC{sign}{h:02d}:{m:02d}". Adds a test that mocks time module globals to verify UTC-05:00 and UTC+05:30. --- llm/tools.py | 10 +++++----- tests/test_tools.py | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/llm/tools.py b/llm/tools.py index 5ac0a7dcb..7f41b9eec 100644 --- a/llm/tools.py +++ b/llm/tools.py @@ -20,12 +20,12 @@ def llm_time() -> dict: # Calculate offset offset_seconds = -time.timezone if not is_dst else -time.altzone - offset_hours = offset_seconds // 3600 - offset_minutes = (offset_seconds % 3600) // 60 + abs_seconds = abs(offset_seconds) + offset_hours = abs_seconds // 3600 + offset_minutes = (abs_seconds % 3600) // 60 + sign = "+" if offset_seconds >= 0 else "-" - timezone_offset = ( - f"UTC{'+' if offset_hours >= 0 else ''}{offset_hours:02d}:{offset_minutes:02d}" - ) + timezone_offset = f"UTC{sign}{offset_hours:02d}:{offset_minutes:02d}" return { "utc_time": utc_time.strftime("%Y-%m-%d %H:%M:%S UTC"), diff --git a/tests/test_tools.py b/tests/test_tools.py index 81d01dc4b..46984a74b 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -378,6 +378,28 @@ def test_default_tool_llm_time(): } +def test_llm_time_timezone_offset_format(monkeypatch): + from unittest.mock import MagicMock + + mock_tm = MagicMock() + mock_tm.tm_isdst = 0 + + # Simulate UTC-5 (EST): time.timezone = 18000 (seconds west of UTC) + monkeypatch.setattr(time, "timezone", 18000) + monkeypatch.setattr(time, "altzone", 14400) + monkeypatch.setattr(time, "localtime", lambda: mock_tm) + monkeypatch.setattr(time, "tzname", ("EST", "EDT")) + info = llm_time() + assert info["timezone_offset"] == "UTC-05:00" + + # Simulate UTC+5:30 (IST): time.timezone = -19800 + monkeypatch.setattr(time, "timezone", -19800) + monkeypatch.setattr(time, "altzone", -19800) + monkeypatch.setattr(time, "tzname", ("IST", "IST")) + info = llm_time() + assert info["timezone_offset"] == "UTC+05:30" + + def test_incorrect_tool_usage(): model = llm.get_model("echo")