Spaces:
Runtime error
Runtime error
Update src/utils/time_utils.py
Browse files- src/utils/time_utils.py +31 -5
src/utils/time_utils.py
CHANGED
|
@@ -5,6 +5,10 @@ import pytz
|
|
| 5 |
OMAN_TZ = pytz.timezone("Asia/Muscat")
|
| 6 |
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def utc_now_iso() -> str:
|
| 9 |
"""
|
| 10 |
Returns current UTC time in ISO-8601 format.
|
|
@@ -13,29 +17,51 @@ def utc_now_iso() -> str:
|
|
| 13 |
return datetime.utcnow().replace(microsecond=0).isoformat() + "Z"
|
| 14 |
|
| 15 |
|
| 16 |
-
def
|
| 17 |
"""
|
| 18 |
-
Returns current
|
| 19 |
"""
|
| 20 |
return datetime.now(OMAN_TZ)
|
| 21 |
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def local_date_str() -> str:
|
| 24 |
"""
|
| 25 |
Returns local date (Asia/Muscat) as YYYY-MM-DD.
|
| 26 |
"""
|
| 27 |
-
return
|
| 28 |
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
"""
|
| 32 |
Business day closes at 06:00 Asia/Muscat.
|
|
|
|
| 33 |
- Before 06:00 → business day = yesterday
|
| 34 |
- After 06:00 → business day = today
|
| 35 |
"""
|
| 36 |
if now is None:
|
| 37 |
-
now =
|
| 38 |
|
| 39 |
if now.hour < 6:
|
| 40 |
return (now.date() - timedelta(days=1)).isoformat()
|
| 41 |
return now.date().isoformat()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
OMAN_TZ = pytz.timezone("Asia/Muscat")
|
| 6 |
|
| 7 |
|
| 8 |
+
# -------------------------------------------------
|
| 9 |
+
# Core time helpers
|
| 10 |
+
# -------------------------------------------------
|
| 11 |
+
|
| 12 |
def utc_now_iso() -> str:
|
| 13 |
"""
|
| 14 |
Returns current UTC time in ISO-8601 format.
|
|
|
|
| 17 |
return datetime.utcnow().replace(microsecond=0).isoformat() + "Z"
|
| 18 |
|
| 19 |
|
| 20 |
+
def muscat_now() -> datetime:
|
| 21 |
"""
|
| 22 |
+
Returns current time in Asia/Muscat timezone.
|
| 23 |
"""
|
| 24 |
return datetime.now(OMAN_TZ)
|
| 25 |
|
| 26 |
|
| 27 |
+
# Backward-compatible alias
|
| 28 |
+
local_now = muscat_now
|
| 29 |
+
|
| 30 |
+
|
| 31 |
def local_date_str() -> str:
|
| 32 |
"""
|
| 33 |
Returns local date (Asia/Muscat) as YYYY-MM-DD.
|
| 34 |
"""
|
| 35 |
+
return muscat_now().strftime("%Y-%m-%d")
|
| 36 |
|
| 37 |
|
| 38 |
+
# -------------------------------------------------
|
| 39 |
+
# Business day & closing logic
|
| 40 |
+
# -------------------------------------------------
|
| 41 |
+
|
| 42 |
+
def business_day_for_close(now: datetime | None = None) -> str:
|
| 43 |
"""
|
| 44 |
Business day closes at 06:00 Asia/Muscat.
|
| 45 |
+
|
| 46 |
- Before 06:00 → business day = yesterday
|
| 47 |
- After 06:00 → business day = today
|
| 48 |
"""
|
| 49 |
if now is None:
|
| 50 |
+
now = muscat_now()
|
| 51 |
|
| 52 |
if now.hour < 6:
|
| 53 |
return (now.date() - timedelta(days=1)).isoformat()
|
| 54 |
return now.date().isoformat()
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# Backward-compatible alias
|
| 58 |
+
business_date_for_closing = business_day_for_close
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def should_auto_close(now: datetime | None = None) -> bool:
|
| 62 |
+
"""
|
| 63 |
+
Returns True if auto-closing logic should run.
|
| 64 |
+
"""
|
| 65 |
+
if now is None:
|
| 66 |
+
now = muscat_now()
|
| 67 |
+
return now.hour >= 6
|