rairo commited on
Commit
343ff22
·
verified ·
1 Parent(s): 02e487d

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -6
main.py CHANGED
@@ -225,19 +225,37 @@ def sc_request(method: str, path: str, email: str, password: str, *,
225
  # -----------------------------------------------------------------------------
226
  # Temporal helpers
227
  # -----------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
228
  def now_harare() -> pd.Timestamp:
229
- return pd.Timestamp.now(tz=_TZ)
 
230
 
231
- def week_bounds_from(ts: pd.Timestamp) -> Tuple[pd.Timestamp, pd.Timestamp]:
232
- monday = ts.tz_convert(_TZ).normalize() - pd.Timedelta(days=ts.weekday())
 
233
  sunday = monday + pd.Timedelta(days=6, hours=23, minutes=59, seconds=59)
234
  return monday, sunday
235
 
236
- def this_month_bounds(ts: pd.Timestamp) -> Tuple[pd.Timestamp, pd.Timestamp]:
 
237
  first_this = ts.normalize().replace(day=1)
238
- first_next = first_this.replace(year=first_this.year + 1, month=1) if first_this.month == 12 else first_this.replace(month=first_this.month + 1)
239
- last_this = first_next - pd.Timedelta(seconds=1)
 
 
240
  return first_this, last_this
 
241
 
242
  def period_to_bounds(period: str) -> Tuple[pd.Timestamp, pd.Timestamp, str]:
243
  p = (period or "week").strip().lower()
 
225
  # -----------------------------------------------------------------------------
226
  # Temporal helpers
227
  # -----------------------------------------------------------------------------
228
+ # -----------------------------------------------------------------------------
229
+ # Timezone Configuration (fix for "_TZ is not defined")
230
+ # -----------------------------------------------------------------------------
231
+ import os
232
+ import pandas as pd
233
+
234
+ # Canonical timezone for Brave Retail Insights
235
+ TZ = os.getenv("APP_TZ", "Africa/Harare")
236
+
237
+ # Backward-compatible alias for older references
238
+ _TZ = TZ
239
+
240
  def now_harare() -> pd.Timestamp:
241
+ """Return the current timestamp in Harare timezone."""
242
+ return pd.Timestamp.now(tz=TZ)
243
 
244
+ def week_bounds_from(ts: pd.Timestamp) -> tuple[pd.Timestamp, pd.Timestamp]:
245
+ """Return Monday–Sunday bounds for the given timestamp."""
246
+ monday = ts.tz_convert(TZ).normalize() - pd.Timedelta(days=ts.weekday())
247
  sunday = monday + pd.Timedelta(days=6, hours=23, minutes=59, seconds=59)
248
  return monday, sunday
249
 
250
+ def this_month_bounds(ts: pd.Timestamp) -> tuple[pd.Timestamp, pd.Timestamp]:
251
+ """Return start and end of the current month."""
252
  first_this = ts.normalize().replace(day=1)
253
+ next_month = first_this.replace(
254
+ year=first_this.year + 1, month=1
255
+ ) if first_this.month == 12 else first_this.replace(month=first_this.month + 1)
256
+ last_this = next_month - pd.Timedelta(seconds=1)
257
  return first_this, last_this
258
+
259
 
260
  def period_to_bounds(period: str) -> Tuple[pd.Timestamp, pd.Timestamp, str]:
261
  p = (period or "week").strip().lower()