diff --git a/tests/tools/builtin_tools/test_datetime_tool.py b/tests/tools/builtin_tools/test_datetime_tool.py index 28d06dbac..ab926aee9 100644 --- a/tests/tools/builtin_tools/test_datetime_tool.py +++ b/tests/tools/builtin_tools/test_datetime_tool.py @@ -227,6 +227,20 @@ def test_diff_zero(): assert result["human"] == "0:00:00" +@pytest.mark.parametrize( + ("start", "end", "seconds", "human"), + [ + ("2026-03-08 01:30:00", "2026-03-08 03:30:00", 3600, "1:00:00"), + ("2026-11-01 00:30:00", "2026-11-01 02:30:00", 10800, "3:00:00"), + ], +) +def test_diff_reports_elapsed_time_across_dst(start, end, seconds, human): + result = datetime_tool("diff", value=start, value2=end, timezone="America/New_York") + assert result["seconds"] == seconds + assert result["days"] == pytest.approx(seconds / 86400) + assert result["human"] == human + + def test_diff_negative_seconds(): result = datetime_tool( "diff", diff --git a/veadk/tools/builtin_tools/datetime_tool.py b/veadk/tools/builtin_tools/datetime_tool.py index ea13d2a7e..84e5fc5eb 100644 --- a/veadk/tools/builtin_tools/datetime_tool.py +++ b/veadk/tools/builtin_tools/datetime_tool.py @@ -170,6 +170,9 @@ def _action_diff(value: str | None, value2: str | None, timezone: str | None) -> start = _parse(value, timezone) end = _parse(value2, timezone) delta = end - start + if start.tzinfo is end.tzinfo: + # Python subtraction ignores offset changes when tzinfo is shared. + delta += start.utcoffset() - end.utcoffset() seconds = delta.total_seconds() return { "seconds": seconds, @@ -209,7 +212,7 @@ def datetime( """Work with dates and times, including timezone conversions. Use this tool for the current time, parsing or formatting a timestamp, - converting a timestamp between timezones, computing the difference between + converting a timestamp between timezones, computing the elapsed time between two timestamps, or adding a duration. Timestamps without an explicit offset use ``timezone`` (default UTC).