diff --git a/backend/services/calc/day_slot.py b/backend/services/calc/day_slot.py index b15cf4e..f73227d 100644 --- a/backend/services/calc/day_slot.py +++ b/backend/services/calc/day_slot.py @@ -6,15 +6,22 @@ SLOTS = ("morning", "day", "evening", "night") SLOT_LABEL = {"morning": "아침", "day": "낮", "evening": "저녁", "night": "밤"} NIGHT_AFTER_SUNSET = timedelta(hours=1) # 해 지고 이만큼 지나면 밤 NIGHT_END_HOUR = 5 +EVENING_EARLIEST_HOUR = 17 # 겨울엔 일몰 2시간 전이 15시대라, 사람들이 아직 낮으로 느끼는 오후 3시가 저녁이 되지 않게 + + +def evening_start(sunset_at: datetime) -> datetime: + """저녁이 시작하는 시각: 일몰 2시간 전, 단 17시보다 이르지 않게.""" + sunset = sunset_at.astimezone(KST) + return max(sunset - timedelta(hours=2), sunset.replace(hour=EVENING_EARLIEST_HOUR, minute=0, second=0, microsecond=0)) def slot_of(now: datetime, sunset_at: datetime) -> str: - """아침 05–11 · 낮 11–일몰 2시간 전 · 저녁 일몰 2시간 전–일몰 1시간 뒤 · 밤 일몰 1시간 뒤–05. 밤은 계절 따라 18시대~21시대에 온다.""" + """아침 05–11 · 낮 11–저녁 시작 · 저녁 일몰 2시간 전(17시보다 이르지 않게)–일몰 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 "day" if now < evening_start(sunset_at) else "evening" return "night" diff --git a/backend/services/calc/my_gak_calc.py b/backend/services/calc/my_gak_calc.py index d7b4a89..8ce14c4 100644 --- a/backend/services/calc/my_gak_calc.py +++ b/backend/services/calc/my_gak_calc.py @@ -2,7 +2,7 @@ from dataclasses import dataclass from types import SimpleNamespace from typing import Literal, Optional -from services.calc.day_slot import NIGHT_END_HOUR, SLOTS +from services.calc.day_slot import EVENING_EARLIEST_HOUR, NIGHT_END_HOUR, SLOTS Rain = Literal["dry", "wet"] Air = Literal["good", "normal"] @@ -59,11 +59,12 @@ _TEMP_MIN, _TEMP_MAX = -20, 40 def _slot_of_hour(h: int) -> str: h %= 24 + evening_from = max(_SUNSET_HOUR - 2, EVENING_EARLIEST_HOUR) if NIGHT_END_HOUR <= h < 11: return "morning" - if 11 <= h < _SUNSET_HOUR - 2: + if 11 <= h < evening_from: return "day" - if _SUNSET_HOUR - 2 <= h < _SUNSET_HOUR + 1: + if evening_from <= h < _SUNSET_HOUR + 1: return "evening" return "night" diff --git a/backend/services/feed_service.py b/backend/services/feed_service.py index 2f7c570..276f1a4 100644 --- a/backend/services/feed_service.py +++ b/backend/services/feed_service.py @@ -28,7 +28,7 @@ from router.v1.index.protocol import TrendPoint from services.air_service import GRADE_LABEL as AIR_GRADE_LABEL from services.air_service import pm10_grade, pm25_grade from services.bot_comment import fill_bot_comments -from services.calc.day_slot import NIGHT_AFTER_SUNSET, slot_of +from services.calc.day_slot import NIGHT_AFTER_SUNSET, evening_start, slot_of from services.calc.index_calc import rank_score from services.calc.moon import MoonNight, nearest, night, view_line from services.calc.sky_score import GRADE_LABEL, sky_title @@ -177,7 +177,7 @@ def feed_ttl_sec(now: datetime, sunset_at: datetime) -> int: day = now.date() at = lambda d, h: datetime.combine(d, time(h), tzinfo=KST) # noqa: E731 sunset = sunset_at.astimezone(KST) - changes = [at(day, _FEED_DAY_START), at(day, 5), at(day, 11), sunset - timedelta(hours=2), sunset, sunset + NIGHT_AFTER_SUNSET, + changes = [at(day, _FEED_DAY_START), at(day, 5), at(day, 11), evening_start(sunset), sunset, sunset + NIGHT_AFTER_SUNSET, at(day + timedelta(days=1), _FEED_DAY_START), next_vilage_available(now), latest_ultra_base(now) + timedelta(hours=1) + ULTRA_DELAY, latest_ncst_base(now) + timedelta(hours=1) + NCST_DELAY, now.replace(minute=0, second=0, microsecond=0) + timedelta(hours=1)] air = now.replace(minute=20, second=0, microsecond=0) diff --git a/backend/tests/test_day_slot.py b/backend/tests/test_day_slot.py index ddbb842..2e81bb7 100644 --- a/backend/tests/test_day_slot.py +++ b/backend/tests/test_day_slot.py @@ -4,7 +4,7 @@ from types import SimpleNamespace from common.database.model.models import WeatherHourly from common.enums import RainType, Sky from common.utils.kma_time import KST, kst_midnight -from services.calc.day_slot import slot_hours +from services.calc.day_slot import slot_hours, slot_of from services.calc.index_calc import Context from services.index_service import EARLY, WEATHER_SCORE_DEF, _shown_parts, current_slot, slot_results from services.weather_service import RainHour, with_ultra_rain @@ -36,16 +36,34 @@ def _defn(windows): def test_slot_hours_split_by_sunset(): + """일몰 18:00: 일몰 2시간 전은 16시지만 저녁은 17시부터, 밤은 일몰 1시간 뒤 19시부터.""" hours = slot_hours(DAY, SUNSET) assert hours["morning"] == set(range(5, 11)) - assert hours["day"] == set(range(11, 16)) - assert hours["evening"] == {16, 17, 18} + assert hours["day"] == set(range(11, 17)) + assert hours["evening"] == {17, 18} assert hours["night"] == set(range(19, 29)) +def test_evening_starts_no_earlier_than_17(): + """겨울(일몰 17:14)엔 일몰 2시간 전이 15시대라 오후 3시가 저녁이던 것: 저녁은 17시보다 이르게 시작하지 않는다. 밤(일몰 1시간 뒤~)은 그대로.""" + winter = datetime(2026, 12, 21, 17, 14, tzinfo=KST) + at = lambda h, m=0: datetime(2026, 12, 21, h, m, tzinfo=KST) # noqa: E731 + assert [slot_of(at(h, m), winter) for h, m in ((16, 59), (17, 0), (18, 13), (18, 14))] == ["day", "evening", "evening", "night"] + hours = slot_hours(date(2026, 12, 21), winter) + assert hours["day"] == set(range(11, 17)) + assert hours["evening"] == {17} + + +def test_evening_follows_sunset_when_it_sets_late(): + """일몰이 19시 넘으면 지금처럼 일몰 2시간 전부터 저녁.""" + summer = datetime(2026, 6, 21, 19, 57, tzinfo=KST) + assert slot_of(datetime(2026, 6, 21, 17, 56, tzinfo=KST), summer) == "day" + assert slot_of(datetime(2026, 6, 21, 17, 57, tzinfo=KST), summer) == "evening" + + def test_current_slot_before_5_is_previous_night(): assert current_slot(datetime(2026, 10, 8, 2, 0, tzinfo=KST), SUNSET) == EARLY - assert current_slot(datetime(2026, 10, 8, 16, 30, tzinfo=KST), SUNSET) == "evening" + assert current_slot(datetime(2026, 10, 8, 17, 30, tzinfo=KST), SUNSET) == "evening" def test_gak_scores_every_slot_and_time_match_is_binary(): @@ -213,10 +231,10 @@ def test_umbrella_now_follows_the_slot_the_screen_shows_even_at_a_slot_boundary( def test_umbrella_other_slots_have_no_raining_now_bonus(): - """13:30 에 낮 구간이 지금. 저녁 구간(16시~)은 16시에만 비가 와도 지금 비 보너스 없이 평균 × 100 → 약 12점.""" + """13:30 에 낮 구간이 지금. 저녁 구간(17시~)은 17시에만 비가 와도 지금 비 보너스 없이 평균 × 100 → 17~23시 7시간 중 1시간, 약 14점.""" now = datetime(2026, 10, 8, 13, 30, tzinfo=KST) - parts = slot_results(_umbrella_defn(), DAY, REGION, _rain_at({16}), {}, CTX, SUNSET, now) - assert 10 <= parts["evening"].score <= 13 + parts = slot_results(_umbrella_defn(), DAY, REGION, _rain_at({17}), {}, CTX, SUNSET, now) + assert 13 <= parts["evening"].score <= 15 def test_umbrella_rest_of_day_stops_at_midnight(): diff --git a/backend/tests/test_feed.py b/backend/tests/test_feed.py index cc9ef0b..de74b88 100644 --- a/backend/tests/test_feed.py +++ b/backend/tests/test_feed.py @@ -157,7 +157,7 @@ def test_slot_follows_sunset(): sunset = datetime(2026, 10, 2, 18, 10, tzinfo=KST) at = lambda h, m=0: datetime(2026, 10, 2, h, m, tzinfo=KST) # noqa: E731 - assert [slot_of(at(h, m), sunset) for h, m in ((4, 59), (5, 0), (11, 0), (16, 9), (16, 10), (19, 9), (19, 10), (23, 0))] == [ + assert [slot_of(at(h, m), sunset) for h, m in ((4, 59), (5, 0), (11, 0), (16, 59), (17, 0), (19, 9), (19, 10), (23, 0))] == [ "night", "morning", "day", "day", "evening", "evening", "night", "night"] summer = datetime(2026, 6, 21, 19, 57, tzinfo=KST) assert slot_of(datetime(2026, 6, 21, 20, 30, tzinfo=KST), summer) == "evening" diff --git a/backend/tests/test_index_slots.py b/backend/tests/test_index_slots.py index 2ac22f1..652469f 100644 --- a/backend/tests/test_index_slots.py +++ b/backend/tests/test_index_slots.py @@ -114,10 +114,10 @@ def test_running_muggy_morning_does_not_exceed_original_weather_score(): def test_now_slot_summary_shows_current_hour_not_middle_of_rest(): - """오후 4시 10분이면 지금 구간 줄은 남은 16~18시의 가운데(5시)가 아니라 지금(4시) 날씨로.""" + """오후 5시 10분이면 지금 구간 줄은 남은 17~19시의 가운데(6시)가 아니라 지금(5시) 날씨로.""" start = kst_midnight(SAT) hourly = _hourly(SAT) | _hourly(SAT + timedelta(days=1)) - sunset = start + timedelta(hours=17, minutes=30) - results = slot_results(RUNNING, SAT, REGION, hourly, {}, CTX, sunset, now=start + timedelta(hours=16, minutes=10)) - assert results["evening"].summary.startswith("오후 4시"), results["evening"].summary - assert results["night"].summary.startswith("오후 4시") is False + sunset = start + timedelta(hours=19) + results = slot_results(RUNNING, SAT, REGION, hourly, {}, CTX, sunset, now=start + timedelta(hours=17, minutes=10)) + assert results["evening"].summary.startswith("오후 5시"), results["evening"].summary + assert results["night"].summary.startswith("오후 5시") is False diff --git a/backend/tests/test_my_gak.py b/backend/tests/test_my_gak.py index 3f381b2..d16c075 100644 --- a/backend/tests/test_my_gak.py +++ b/backend/tests/test_my_gak.py @@ -113,6 +113,13 @@ def test_preset_copies_rules_that_fit_and_drops_the_rest(): assert p == {"slots": ["evening"], "rain": "dry"} +def test_preset_evening_starts_at_17_like_the_feed(): + """피드와 같은 규칙: 저녁은 17시보다 이르게 시작하지 않으니 16시대는 낮.""" + rules = {"rain": {"dir": "dry"}, "temp": {"type": "range", "lo": 18, "hi": 24}} + assert preset(_defn(rules, [{"from": 16, "to": 17}]))["slots"] == ["day"] + assert preset(_defn(rules, [{"from": 17, "to": 19}]))["slots"] == ["evening"] + + @pytest.mark.anyio async def test_presets_cover_every_gak_with_valid_rules(): async with client() as c: