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")