diff --git a/babel/dates.py b/babel/dates.py index d75bb4ea4..b6609912a 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -2056,10 +2056,10 @@ def untokenize_pattern(tokens: Iterable[tuple[str, str | tuple[str, int]]]) -> s if tok_type == "field": output.append(tok_value[0] * tok_value[1]) elif tok_type == "chars": - if not any(ch in PATTERN_CHARS for ch in tok_value): # No need to quote - output.append(tok_value) - else: - output.append("'%s'" % tok_value.replace("'", "''")) + literal = tok_value.replace("'", "''") + if any(ch in PATTERN_CHARS for ch in tok_value): + literal = f"'{literal}'" + output.append(literal) return "".join(output) diff --git a/tests/test_dates.py b/tests/test_dates.py index a1a3dfdbb..94291696a 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -1340,3 +1340,11 @@ def test_issue_1192_fmt(timezone_getter): assert dates.format_datetime(dt, "YYYY-MM-dd H:mm z", locale="en_GB") == "2025-03-04 3:53 GMT-10" assert dates.format_datetime(dt, "YYYY-MM-dd H:mm zz", locale="en_US") == "2025-03-04 3:53 HST" assert dates.format_datetime(dt, "YYYY-MM-dd H:mm zz", locale="en_GB") == "2025-03-04 3:53 GMT-10" + + +def test_interval_pattern_preserves_literal_apostrophes(): + parts = dates.split_interval_pattern("HH''mm – HH''mm") + assert [dates.format_time(time(12, 30), part, locale='en') for part in parts] == [ + "12'30 – ", + "12'30", + ]