dodil/erp-brick-consolidation
DODIL ERP brick: consolidation
src/brick_consolidation/infrastructure/clock.py
"""Business time: 'today' is the date in the business timezone, never the UTC date (shared.yaml conventions.business_timezone).
The timezone is ONE value per bucket owned by the platform, not a setting of this brick. Until the platform serves it, it is read from the
environment (BUSINESS_TIMEZONE, an IANA name) and defaults to Europe/London, as the other bricks' default does. A moment is aware (work.now())."""
import contextvars
import os
from datetime import date, timedelta
from erp_platform import work
DEFAULT_ZONE = "Europe/London"
_gl_zone = contextvars.ContextVar("consolidation_business_timezone", default="")
def use_gl_zone(zone: str) -> None:
"""v3: the bucket's one business_timezone as gl answers it for the group's top company (GET /companies/{top_company_code}), read per request
and never stored. It holds until the request's use case ends (reset_gl_zone)."""
_gl_zone.set((zone or "").strip())
def reset_gl_zone() -> None:
_gl_zone.set("")
def business_timezone() -> str:
return _gl_zone.get() or os.environ.get("BUSINESS_TIMEZONE", "").strip() or DEFAULT_ZONE
def today() -> date:
return work.now().astimezone(work.zone(business_timezone())).date()
def tomorrow() -> date:
return today() + timedelta(days=1)