Files

28 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from datetime import date, datetime, time, timedelta
from common.utils.kma_time import KST
SLOTS = ("morning", "day", "evening", "night")
SLOT_LABEL = {"morning": "아침", "day": "낮", "evening": "저녁", "night": "밤"}
NIGHT_AFTER_SUNSET = timedelta(hours=1) # 해 지고 이만큼 지나면 밤
NIGHT_END_HOUR = 5
def slot_of(now: datetime, sunset_at: datetime) -> str:
"""아침 05–11 · 낮 11–일몰 2시간 전 · 저녁 일몰 2시간 전–일몰 1시간 뒤 · 밤 일몰 1시간 뒤–05. 밤은 계절 따라 18시대~21시대에 온다."""
h = now.hour
if NIGHT_END_HOUR <= h < 11:
return "morning"
if h >= 11 and now < sunset_at + NIGHT_AFTER_SUNSET:
return "day" if now < sunset_at - timedelta(hours=2) else "evening"
return "night"
def slot_hours(day: date, sunset_at: datetime) -> dict[str, set[int]]:
"""그날 5시~다음 날 5시를 구간별 시각으로 나눈다. 24 이상은 다음 날 시각. 한 시간은 그 30분 시점의 구간에 넣는다."""
out: dict[str, set[int]] = {s: set() for s in SLOTS}
start = datetime.combine(day, time(0), tzinfo=KST)
for h in range(NIGHT_END_HOUR, 24 + NIGHT_END_HOUR):
out[slot_of(start + timedelta(hours=h, minutes=30), sunset_at)].add(h)
return out