"""Time helpers. Timestamps are stored in UTC and displayed in IST."""
from datetime import datetime, timedelta, timezone

IST = timezone(timedelta(hours=5, minutes=30))


def now_utc_iso():
    """Current time as a UTC ISO-8601 string (for storage)."""
    return datetime.now(timezone.utc).isoformat()


def today_ist():
    """Today's date in IST as YYYY-MM-DD (for due dates / 'today')."""
    return datetime.now(IST).date().isoformat()


def to_ist(iso_value):
    """Parse a stored ISO timestamp and return an IST-aware datetime, or None."""
    if not iso_value:
        return None
    try:
        dt = datetime.fromisoformat(iso_value)
    except ValueError:
        return None
    if dt.tzinfo is None:
        dt = dt.replace(tzinfo=timezone.utc)
    return dt.astimezone(IST)
