Skip to content

Commit 7bcb802

Browse files
cliffhallclaude
andcommitted
fix(time): reject a nonexistent local time in convert_time
convert_time built the source datetime with fold=0, so a wall-clock time skipped by a spring-forward transition (02:30 on 2024-03-10 in America/New_York) was converted with the pre-transition offset and reported as if it had happened. It now round-trips the source time through a timestamp and raises an error when the wall-clock time does not survive, naming the time, the zone and the date it was checked on. The pinning test now asserts the error, and its KNOWN BUG #5002 marker is gone. Closes #5002 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 571e2b3 commit 7bcb802

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

‎src/time/src/mcp_server_time/server.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ def convert_time(
102102
tzinfo=source_timezone,
103103
)
104104

105+
# A wall-clock time skipped by a DST jump (e.g. 02:30 on a
106+
# spring-forward day) does not survive a round trip through UTC.
107+
round_trip = datetime.fromtimestamp(source_time.timestamp(), source_timezone)
108+
if round_trip.replace(tzinfo=None) != source_time.replace(tzinfo=None):
109+
raise ValueError(
110+
f"Invalid time: {time_str} does not exist in {source_tz} on "
111+
f"{source_time.date().isoformat()} (skipped by a daylight "
112+
"saving time change)"
113+
)
114+
105115
target_time = source_time.astimezone(target_timezone)
106116
source_offset = source_time.utcoffset() or timedelta()
107117
target_offset = target_time.utcoffset() or timedelta()

‎src/time/tests/test_protocol.py‎

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -709,14 +709,13 @@ async def test_convert_time_anchors_to_today_in_the_source_timezone() -> None:
709709
assert result["target"]["datetime"] == "2024-01-02T00:00:00+00:00"
710710

711711

712-
# KNOWN BUG #5002: a nonexistent (spring-forward) local time is converted as if valid, with no warning; the fix changes this assertion.
713712
async def test_convert_time_into_a_nonexistent_local_time() -> None:
714713
# 02:30 on 2024-03-10 does not exist in New York (clocks jump 02:00 ->
715-
# 03:00). With fold=0 zoneinfo uses the pre-transition offset (EST), so
716-
# the server reports a wall-clock time that never happened, unflagged.
714+
# 03:00), so the conversion is rejected rather than reporting a
715+
# wall-clock time that never happened (#5002).
717716
async with connected() as session:
718717
with frozen_at("2024-03-10 12:00:00+00:00"):
719-
result = await call_json(
718+
result = await call(
720719
session,
721720
"convert_time",
722721
{
@@ -725,21 +724,10 @@ async def test_convert_time_into_a_nonexistent_local_time() -> None:
725724
"target_timezone": "UTC",
726725
},
727726
)
728-
assert result == {
729-
"source": {
730-
"timezone": "America/New_York",
731-
"datetime": "2024-03-10T02:30:00-05:00",
732-
"day_of_week": "Sunday",
733-
"is_dst": False,
734-
},
735-
"target": {
736-
"timezone": "UTC",
737-
"datetime": "2024-03-10T07:30:00+00:00",
738-
"day_of_week": "Sunday",
739-
"is_dst": False,
740-
},
741-
"time_difference": "+5.0h",
742-
}
727+
assert result == handler_error(
728+
"Invalid time: 02:30 does not exist in America/New_York on 2024-03-10 "
729+
"(skipped by a daylight saving time change)"
730+
)
743731

744732

745733
async def test_convert_time_at_an_ambiguous_local_time() -> None:

0 commit comments

Comments
 (0)