"""Reading the moment a Federal Reserve document became public. Every modern FOMC release states its own embargo: *For release at 2:00 p.m. EDT*. That line is the fact, and it is read out of the document rather than assumed -- a statement is at 2:00 p.m., the minutes three weeks later are at 2:00 p.m. too, and a longer-run strategy revision might be at 11:00 a.m. Assuming one of them would be right most of the time, which is the worst kind of wrong. Where a document states no time -- the minutes pages carry none, only the calendar's release date -- nothing is invented. The instant becomes the end of that day in Washington and `knowledge_time_precision` says `date`. That is late by up to a day and never early, which is the only direction a knowledge timestamp is allowed to be wrong in. """ from __future__ import annotations import re from datetime import UTC, date, datetime, time from .config import EASTERN MONTHS = { "january": 1, "february": 2, "march": 3, "april": 4, "may": 5, "june": 6, "july": 7, "august": 8, "september": 9, "october": 10, "november": 11, "december": 12, "jan": 1, "feb": 2, "mar": 3, "apr": 4, "jun": 6, "jul": 7, "aug": 8, "sept": 9, "sep": 9, "oct": 10, "nov": 11, "dec": 12, } MONTH_NAMES = "|".join(sorted(MONTHS, key=len, reverse=True)) DATE = re.compile(rf"\b({MONTH_NAMES})\.?\s+(\d{{1,2}})\s*,?\s+((?:19|20)\d{{2}})", re.IGNORECASE) # "For release at 2:00 p.m. EDT" and "For release at 2:00 p.m., EDT, June 17, 2026" RELEASE_TIME = re.compile( r"for\s+release\s+at\s+(\d{1,2}):(\d{2})\s*(a|p)\.?\s?m\.?", re.IGNORECASE ) # The end of the day in Washington: late, and never early. END_OF_DAY = time(23, 59, 59) def parse_date(text: str) -> date | None: match = DATE.search(text or "") if not match: return None month = MONTHS.get(match.group(1).lower().rstrip(".")) if month is None: return None try: return date(int(match.group(3)), month, int(match.group(2))) except ValueError: return None def release_instant(text: str, *, on: date | None = None, window: int = 6000) -> tuple: """The document's own embargo instant, or the end of its release day. Returns `(instant, precision)`. `on` is the release date where it is known from elsewhere -- the calendar states when minutes came out, and the minutes themselves do not. """ head = (text or "")[:window] match = RELEASE_TIME.search(head) day = on if day is None: day = parse_date(head) if day is None: return None, "unknown" if match is None: return _at(day, END_OF_DAY), "date" hour = int(match.group(1)) % 12 + (12 if match.group(3).lower() == "p" else 0) return _at(day, time(hour, int(match.group(2)))), "exact" def _at(day: date, moment: time) -> datetime: return datetime( day.year, day.month, day.day, moment.hour, moment.minute, moment.second, tzinfo=EASTERN ).astimezone(UTC) def end_of_day(day: date) -> datetime: return _at(day, END_OF_DAY) __all__ = ["END_OF_DAY", "MONTHS", "end_of_day", "parse_date", "release_instant"]