"""pubsalesacc/utils/dates.py — Date and reporting month helpers.""" import datetime as dt def get_reporting_month_object(offset: int = -1) -> dict[str, str]: """Return a dict of date parts for the reporting month. Args: offset: -1 (default) returns last calendar month. 0 returns the current month. """ today = dt.date.today() first_of_today = today.replace(day=1) if offset == -1: last_month = (first_of_today - dt.timedelta(days=1)).replace(day=1) else: last_month = first_of_today return { "YYYY": last_month.strftime("%Y"), "MM": last_month.strftime("%m"), "YYYYMM": last_month.strftime("%Y%m"), "MON": last_month.strftime("%b").upper(), "Month": last_month.strftime("%B"), } def get_timestamp() -> str: """Return current datetime as a sortable string: YYYYMMDD-HHMMSS.""" return dt.datetime.now().strftime("%Y%m%d-%H%M%S") def get_today() -> str: """Return today's date as YYYY-MM-DD.""" return dt.date.today().isoformat() def get_first_of_month(reporting_month: dict[str, str]) -> dt.date: """Return a date object for the first of the given reporting month dict.""" return dt.date(int(reporting_month["YYYY"]), int(reporting_month["MM"]), 1)