Skip to content

Commit 296d4bb

Browse files
committed
Merge remote-tracking branch 'origin/v2/fix/5002-time-nonexistent-local-time' into v2/fix/5004-time-wave7-rollup
2 parents 8c1e92e + fd00af1 commit 296d4bb

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
@@ -103,6 +103,15 @@ def convert_time(
103103
tzinfo=source_timezone,
104104
)
105105

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

725725

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

758746

759747
async def test_convert_time_at_an_ambiguous_local_time() -> None:

0 commit comments

Comments
 (0)