g_solution / app /services /policy_window.py
minhvtt's picture
Upload 23 files
5b977f1 verified
Raw
History Blame Contribute Delete
1.53 kB
from datetime import datetime
from typing import Any
_DAY_INDEX = {
"mon": 0,
"tue": 1,
"wed": 2,
"thu": 3,
"fri": 4,
"sat": 5,
"sun": 6,
}
def _parse_hhmm(value: str) -> tuple[int, int]:
hour_text, minute_text = value.split(":", 1)
return int(hour_text), int(minute_text)
def _minutes_of_day(moment: datetime) -> int:
return moment.hour * 60 + moment.minute
def is_restriction_active(windows: list[dict[str, Any]], moment: datetime) -> bool:
if not windows:
return False
weekday = moment.weekday()
now_minutes = _minutes_of_day(moment)
for window in windows:
day = window.get("day_of_week")
start = str(window.get("start", "00:00"))
end = str(window.get("end", "00:00"))
if day not in _DAY_INDEX:
continue
start_h, start_m = _parse_hhmm(start)
end_h, end_m = _parse_hhmm(end)
start_minutes = start_h * 60 + start_m
end_minutes = end_h * 60 + end_m
window_day = _DAY_INDEX[day]
if start_minutes <= end_minutes:
if weekday == window_day and start_minutes <= now_minutes <= end_minutes:
return True
continue
# Overnight window: e.g. 22:00 -> 02:00
if weekday == window_day and now_minutes >= start_minutes:
return True
if weekday == (window_day + 1) % 7 and now_minutes <= end_minutes:
return True
return False