"""The site's canonical local day.

There is one daily brief, so "today" must resolve to a single timezone — not the
host clock (which is UTC on the server and was silently reset on a rebuild).
GOODNEWS_TZ pins it explicitly (default UTC) so a rebuild can't reintroduce the
"brief shows tomorrow's date in the evening" bug. This governs when the brief
rolls over and the canonical date on the server-rendered /today page; each
visitor's *displayed* date is formatted in their own browser timezone.
"""

from __future__ import annotations

import os
from datetime import datetime
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError


def site_tz() -> ZoneInfo:
    name = os.environ.get("GOODNEWS_TZ", "UTC")
    try:
        return ZoneInfo(name)
    except (ZoneInfoNotFoundError, ValueError):
        return ZoneInfo("UTC")


def local_now() -> datetime:
    return datetime.now(site_tz())


def local_today() -> str:
    """The site's current calendar date (ISO yyyy-mm-dd) in GOODNEWS_TZ."""
    return local_now().date().isoformat()
