From 7aa2cab0022a263231a89da7159e20a5e43ca69b Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 18:19:05 +0000 Subject: [PATCH] Format dates without a time zone and drop made-up zone names from %Z A date with no time zone that only dateutil could read (an RFC 822 date without a zone, or "01/02/2024 10:00") was kept as the raw feed string instead of being formatted with date_format. Such dates are now taken to be UTC, the same assumption feedparser makes for ISO dates without a zone. They're used only after feedparser's own *_parsed value, so dates with zone names that dateutil can't resolve still use feedparser's reading. A numeric offset such as +0100 was printed by %Z as "UTC+01:00", the name the stdlib gives a fixed-offset zone; before 1.0 %Z printed nothing for it. Offset-only zones now get a nameless tzinfo again, while UTC and real zone names (including Home Assistant's zone with local_time) keep their names. Fixes #184 Fixes #186 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FvqVzfJq7yZSkZfSyG3YHT --- README.md | 2 +- custom_components/feedparser/reader.py | 65 +++++++++++++++--------- tests/test_hardening.py | 68 +++++++++++++++++++++++++- 3 files changed, 110 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index a3941f08..a9370ff5 100644 --- a/README.md +++ b/README.md @@ -210,7 +210,7 @@ The integration supports both UI and YAML setup. Most options are the same in bo - UI uses comma-separated text for `inclusions` and `exclusions`. - YAML uses proper lists. - UI uses a single duration control for `scan_interval`; YAML uses `scan_interval` object keys such as `hours` and `minutes`. -- **Date parsing**: RSS (RFC 822) and Atom (ISO 8601) dates are both supported, including US timezone abbreviations such as `EDT`. If a date cannot be parsed at all, the original text is kept. +- **Date parsing**: RSS (RFC 822) and Atom (ISO 8601) dates are both supported, including US timezone abbreviations such as `EDT`. A date without a time zone is taken to be UTC. `%Z` in `date_format` prints a zone name only when there is one (`UTC`, or Home Assistant's zone with `local_time`); for a numeric offset such as `+0100` it prints nothing, so use `%z` to show the offset. If a date cannot be parsed at all, the original text is kept. Due to how `custom_components` are loaded, it is normal to see a `ModuleNotFoundError` error on first boot after adding this, to resolve it, restart Home-Assistant. diff --git a/custom_components/feedparser/reader.py b/custom_components/feedparser/reader.py index e6082a33..ebb0a252 100644 --- a/custom_components/feedparser/reader.py +++ b/custom_components/feedparser/reader.py @@ -14,11 +14,11 @@ import time import warnings from collections.abc import Mapping -from datetime import UTC, datetime, timezone +from datetime import UTC, datetime, timezone, tzinfo from typing import Protocol, cast import feedparser # type: ignore[import] -from dateutil import parser +from dateutil import parser, tz from homeassistant.util import dt from .fetch import USER_AGENT, fetch_feed, redact_proxy @@ -33,6 +33,25 @@ DATE_KEYS = ("published", "updated", "created", "expired") +def _without_offset_name(value: datetime) -> tzinfo | None: + """Return `value`'s tzinfo, minus any name that is only its UTC offset. + + Feeds mostly give a numeric offset (``+0100``) rather than a zone name. The + stdlib names such a zone ``UTC+01:00``, which ``%Z`` in `date_format` would + print; keep the offset but drop that made-up name so ``%Z`` prints nothing, + as it did before 1.0. UTC itself and real zone names are kept. + """ + zone = value.tzinfo + offset = value.utcoffset() + if ( + offset is None + or not offset + or (not isinstance(zone, timezone) and value.tzname()) + ): + return zone + return tz.tzoffset(None, offset) + + # Entry fields searched by the `match` / `exclude_match` filters. RSS # is exposed by feedparser as `summary`. FILTER_KEYS = ("title", "summary") @@ -282,12 +301,16 @@ def _parse_date( """Parse a feed date into an aware datetime, or None if impossible. Tried in order, keeping the feed's own UTC offset where possible: - RFC 822 (RSS), ISO 8601 / W3CDTF (Atom), feedparser's own normalized UTC - ``*_parsed`` value, and finally dateutil as a best effort. + RFC 822 (RSS), ISO 8601 / W3CDTF (Atom), dateutil, feedparser's own + normalized UTC ``*_parsed`` value, and finally a date without a time zone + (from any of the string parsers), which is taken to be UTC. """ - parsed_time = self._parse_date_string(date) + parsed_time, naive_time = self._parse_date_string(date) if parsed_time is None and isinstance(parsed, time.struct_time): parsed_time = datetime.fromtimestamp(calendar.timegm(parsed), tz=UTC) + if parsed_time is None and naive_time is not None: + # same assumption feedparser makes for ISO dates without a zone + parsed_time = naive_time.replace(tzinfo=UTC) if parsed_time is None: _LOGGER.debug( @@ -297,11 +320,7 @@ def _parse_date( ) return None - if not parsed_time.tzname(): - # replace tzinfo with UTC offset if tzinfo does not contain a TZ name - parsed_time = parsed_time.replace( - tzinfo=timezone(parsed_time.utcoffset()), # type: ignore[arg-type] - ) + parsed_time = parsed_time.replace(tzinfo=_without_offset_name(parsed_time)) if self.local_time: parsed_time = dt.as_local(parsed_time) @@ -309,26 +328,28 @@ def _parse_date( return parsed_time @staticmethod - def _parse_date_string(date: str) -> datetime | None: - """Parse a date string with the stdlib, then dateutil. Return aware or None.""" + def _parse_date_string(date: str) -> tuple[datetime | None, datetime | None]: + """Parse a date string with the stdlib, then dateutil. + + Return ``(aware, naive)``: the first result that has a time zone, and + otherwise the first one without, so the caller can prefer a better source + (feedparser's ``*_parsed``) before assuming a zone for the naive one. + """ candidates = [] with contextlib.suppress(TypeError, ValueError, IndexError): candidates.append(email.utils.parsedate_to_datetime(date)) with contextlib.suppress(ValueError): candidates.append(datetime.fromisoformat(date.strip())) - for candidate in candidates: - if candidate.tzinfo is not None: - return candidate - # dateutil warns (UnknownTimezoneWarning) on abbreviations such as EDT and - # returns a naive datetime; a naive result is treated as unparseable. + # returns a naive datetime for them. with warnings.catch_warnings(): warnings.simplefilter("ignore") - try: - fallback = parser.parse(date) - except (ValueError, OverflowError): - return None - return fallback if fallback.tzinfo is not None else None + with contextlib.suppress(ValueError, OverflowError): + candidates.append(parser.parse(date)) + + aware = next((c for c in candidates if c.tzinfo is not None), None) + naive = next((c for c in candidates if c.tzinfo is None), None) + return aware, naive def _process_image(self: FeedReader, feed_entry: Mapping[str, object]) -> str: """Return the best image URL for an entry, or the default thumbnail. diff --git a/tests/test_hardening.py b/tests/test_hardening.py index b6600e48..432904dc 100644 --- a/tests/test_hardening.py +++ b/tests/test_hardening.py @@ -3,11 +3,14 @@ from __future__ import annotations import warnings +import zoneinfo from pathlib import Path +from typing import Any from unittest.mock import patch import pytest import requests +from homeassistant.util import dt from reader_harness import ReaderHarness from custom_components.feedparser import fetch @@ -39,12 +42,14 @@ def _sensor( inclusions: list[str | None] | None = None, *, remove_summary_image: bool = False, + date_format: str = DATE_FORMAT, + local_time: bool = False, ) -> ReaderHarness: return ReaderHarness( feed=path.absolute().as_uri(), name="test", - date_format=DATE_FORMAT, - local_time=False, + date_format=date_format, + local_time=local_time, show_topn=10, remove_summary_image=remove_summary_image, inclusions=inclusions or [], @@ -90,6 +95,10 @@ def test_non_utf8_feed_is_decoded_from_xml_prolog(tmp_path: Path) -> None: ("2024-02-01T02:00:01+02:00", "2024-02-01T02:00:01+0200"), # US timezone abbreviation (#132): dateutil can't resolve it, feedparser can ("Tue, 08 Sep 2026 08:00:00 EDT", "2026-09-08T08:00:00-0400"), + # no time zone (#186): taken as UTC instead of keeping the raw string + ("Mon, 01 Jan 2024 10:00:00", "2024-01-01T10:00:00+0000"), + ("01/02/2024 10:00", "2024-01-02T10:00:00+0000"), + ("2024-01-01 10:00:00", "2024-01-01T10:00:00+0000"), ], ) def test_date_formats(tmp_path: Path, raw: str, expected: str) -> None: @@ -106,6 +115,61 @@ def test_date_formats(tmp_path: Path, raw: str, expected: str) -> None: assert sensor.feed_entries[0]["published"] == expected +def _published(sensor_path: Path, raw: str, **kwargs: Any) -> object: # noqa: ANN401 + sensor_path.write_text( + _rss(f"a{raw}"), + encoding="utf-8", + ) + sensor = _sensor(sensor_path, ["title", "published"], **kwargs) + sensor.update() + return sensor.feed_entries[0]["published"] + + +@pytest.mark.parametrize( + ("raw", "expected"), + [ + # a bare offset has no zone name: %Z prints nothing, as before 1.0 (#184) + ("Mon, 01 Jan 2024 10:00:00 +0100", "10:00 "), + ("2024-01-01T10:00:00-05:30", "10:00 "), + ("Mon, 01 Jan 2024 10:00:00 EDT", "10:00 "), + # UTC keeps its name + ("Mon, 01 Jan 2024 10:00:00 GMT", "10:00 UTC"), + ("2024-01-01T10:00:00Z", "10:00 UTC"), + # no zone at all: assumed UTC + ("Mon, 01 Jan 2024 10:00:00", "10:00 UTC"), + ], +) +def test_zone_name_in_date_format(tmp_path: Path, raw: str, expected: str) -> None: + """``%Z`` prints a real zone name, never a made-up ``UTC+01:00``.""" + published = _published(tmp_path / "zone.xml", raw, date_format="%H:%M %Z") + assert published == expected + + +@pytest.mark.parametrize( + ("raw", "expected"), + [ + ("Mon, 01 Jan 2024 10:00:00 +0100", "10:00 CET"), + ("Mon, 01 Jan 2024 10:00:00 EDT", "15:00 CET"), + ("Mon, 01 Jan 2024 10:00:00", "11:00 CET"), + ("01/02/2024 10:00", "11:00 CET"), + ], +) +def test_local_time_conversion(tmp_path: Path, raw: str, expected: str) -> None: + """With `local_time`, every date, zoned or not, ends up in HA's zone.""" + original = dt.get_default_time_zone() + dt.set_default_time_zone(zoneinfo.ZoneInfo("Europe/Prague")) + try: + published = _published( + tmp_path / "local.xml", + raw, + date_format="%H:%M %Z", + local_time=True, + ) + finally: + dt.set_default_time_zone(original) + assert published == expected + + def test_unparseable_date_keeps_raw_value(tmp_path: Path) -> None: """A garbage date keeps the raw string instead of failing the whole update.""" feed = tmp_path / "bad_date.xml"