Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions examples/company_news_monitor/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,20 @@ def parse_args():
return parser.parse_args()


# The live API sends `published` as "2026-09-30 05:04:18 +0000".
API_TIMESTAMP_FORMAT = "%Y-%m-%d %H:%M:%S %z"


def parse_timestamp(value, label):
if not isinstance(value, str) or not value:
raise ValueError("{} must be a non-empty RFC 3339 timestamp".format(label))
try:
parsed = datetime.fromisoformat(value.replace("Z", "+00:00"))
except ValueError as exc:
raise ValueError("{} must be an RFC 3339 timestamp".format(label)) from exc
except ValueError:
try:
parsed = datetime.strptime(value, API_TIMESTAMP_FORMAT)
except ValueError as exc:
raise ValueError("{} must be an RFC 3339 timestamp".format(label)) from exc
if parsed.tzinfo is None:
raise ValueError("{} must include a timezone".format(label))
return parsed.astimezone(timezone.utc)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_company_news_monitor_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ def test_date_boundaries_are_inclusive_and_outside_articles_are_removed():
}


@pytest.mark.parametrize(
"value",
["2026-09-30 05:04:18 +0000", "2026-09-30 13:04:18 +0800", "2026-09-30T05:04:18Z"],
)
def test_parse_timestamp_accepts_live_api_published_format(value):
module = load_example_module()

parsed = module.parse_timestamp(value, "published")

assert parsed == datetime(2026, 9, 30, 5, 4, 18, tzinfo=timezone.utc)


def test_parse_timestamp_still_rejects_values_without_timezone():
module = load_example_module()

with pytest.raises(ValueError):
module.parse_timestamp("2026-09-30 05:04:18", "published")


def test_same_url_with_optional_id_drift_is_reported_once():
module = load_example_module()
start = datetime(2026, 8, 4, tzinfo=timezone.utc)
Expand Down
Loading