Skip to content

Commit 953290e

Browse files
authored
Merge pull request #5028 from modelcontextprotocol/v2/fix/5002-time-nonexistent-local-time
fix(time): reject a nonexistent local time in convert_time
2 parents be004b1 + 8b2d58b commit 953290e

2 files changed

Lines changed: 16 additions & 19 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ def convert_time(
109109
tzinfo=source_timezone,
110110
)
111111

112+
# A wall-clock time skipped by a clock change (e.g. 02:30 on a DST
113+
# spring-forward day) does not survive a round trip through UTC.
114+
round_trip = datetime.fromtimestamp(source_time.timestamp(), source_timezone)
115+
if round_trip.replace(tzinfo=None) != source_time.replace(tzinfo=None):
116+
raise ValueError(
117+
f"Invalid time: {time_str} does not exist in {source_tz} on "
118+
f"{source_time.date().isoformat()} (skipped by a clock change)"
119+
)
120+
112121
target_time = source_time.astimezone(target_timezone)
113122
source_offset = source_time.utcoffset() or timedelta()
114123
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
@@ -724,14 +724,13 @@ async def test_convert_time_anchors_to_today_in_the_source_timezone() -> None:
724724
assert result["target"]["datetime"] == "2024-01-02T00:00:00+00:00"
725725

726726

727-
# KNOWN BUG #5002: a nonexistent (spring-forward) local time is converted as if valid, with no warning; the fix changes this assertion.
728727
async def test_convert_time_into_a_nonexistent_local_time() -> None:
729728
# 02:30 on 2024-03-10 does not exist in New York (clocks jump 02:00 ->
730-
# 03:00). With fold=0 zoneinfo uses the pre-transition offset (EST), so
731-
# the server reports a wall-clock time that never happened, unflagged.
729+
# 03:00), so the conversion is rejected rather than reporting a
730+
# wall-clock time that never happened (#5002).
732731
async with connected() as session:
733732
with frozen_at("2024-03-10 12:00:00+00:00"):
734-
result = await call_json(
733+
result = await call(
735734
session,
736735
"convert_time",
737736
{
@@ -740,21 +739,10 @@ async def test_convert_time_into_a_nonexistent_local_time() -> None:
740739
"target_timezone": "UTC",
741740
},
742741
)
743-
assert result == {
744-
"source": {
745-
"timezone": "America/New_York",
746-
"datetime": "2024-03-10T02:30:00-05:00",
747-
"day_of_week": "Sunday",
748-
"is_dst": False,
749-
},
750-
"target": {
751-
"timezone": "UTC",
752-
"datetime": "2024-03-10T07:30:00+00:00",
753-
"day_of_week": "Sunday",
754-
"is_dst": False,
755-
},
756-
"time_difference": "+5.0h",
757-
}
742+
assert result == handler_error(
743+
"Invalid time: 02:30 does not exist in America/New_York on 2024-03-10 "
744+
"(skipped by a clock change)"
745+
)
758746

759747

760748
async def test_convert_time_at_an_ambiguous_local_time() -> None:

0 commit comments

Comments
 (0)