Files
EnuivTools/backend/euniv/study_window.py
T

115 lines
3.9 KiB
Python

"""每月自动学习开始时刻。精确到秒,按北京时间判断当月是否已到点。"""
from __future__ import annotations
import calendar
from datetime import datetime, timedelta, timezone
CN_TZ = timezone(timedelta(hours=8))
class StudyWindowError(ValueError):
"""开始时刻不符合要求。"""
def validate_start(day: object, hour: object, minute: object, second: object) -> tuple[int, int, int, int]:
for name, value, low, high in (
("日", day, 1, 31),
("时", hour, 0, 23),
("分", minute, 0, 59),
("秒", second, 0, 59),
):
if not isinstance(value, int) or isinstance(value, bool) or not low <= value <= high:
raise StudyWindowError(f"每月开始{name}须为 {low} 到 {high} 的整数")
return int(day), int(hour), int(minute), int(second)
def apply(account, day: object, hour: object, minute: object, second: object) -> None:
d, h, m, s = validate_start(day, hour, minute, second)
account.study_start_day = d
account.study_start_hour = h
account.study_start_minute = m
account.study_start_second = s
def label_of(account) -> str:
day = int(getattr(account, "study_start_day", 1) or 1)
hour = int(getattr(account, "study_start_hour", 0) or 0)
minute = int(getattr(account, "study_start_minute", 0) or 0)
second = int(getattr(account, "study_start_second", 0) or 0)
return f"每月{day}日 {hour:02d}:{minute:02d}:{second:02d}"
def view_of(account) -> dict:
day = int(getattr(account, "study_start_day", 1) or 1)
hour = int(getattr(account, "study_start_hour", 0) or 0)
minute = int(getattr(account, "study_start_minute", 0) or 0)
second = int(getattr(account, "study_start_second", 0) or 0)
enabled = getattr(account, "study_schedule_enabled", True)
return {
"day": day,
"hour": hour,
"minute": minute,
"second": second,
"enabled": False if enabled is False else True,
"label": label_of(account),
}
def as_cn(now: datetime | None = None) -> datetime:
current = now or datetime.now(CN_TZ)
if current.tzinfo is None:
return current.replace(tzinfo=CN_TZ)
return current.astimezone(CN_TZ)
def month_key(now: datetime | None = None) -> str:
current = as_cn(now)
return f"{current.year:04d}-{current.month:02d}"
def start_at(account, now: datetime | None = None) -> datetime:
"""该账号在 now 所在月的开始时刻。2 月 31 日等按当月最后一天。"""
current = as_cn(now)
day, hour, minute, second = validate_start(
int(getattr(account, "study_start_day", 1) or 1),
int(getattr(account, "study_start_hour", 0) or 0),
int(getattr(account, "study_start_minute", 0) or 0),
int(getattr(account, "study_start_second", 0) or 0),
)
last = calendar.monthrange(current.year, current.month)[1]
return datetime(
current.year,
current.month,
min(day, last),
hour,
minute,
second,
tzinfo=CN_TZ,
)
def window_open(account, now: datetime | None = None) -> bool:
"""当前是否已过该账号本月开始时刻。"""
current = as_cn(now)
return current >= start_at(account, current)
def next_month_start(now: datetime | None = None) -> datetime:
current = as_cn(now)
if current.month == 12:
return datetime(current.year + 1, 1, 1, tzinfo=CN_TZ)
return datetime(current.year, current.month + 1, 1, tzinfo=CN_TZ)
def next_fire_at(account, now: datetime | None = None) -> datetime | None:
"""下次定时触发。本月已到点则等到下月;关闭定时则没有下一次。"""
enabled = getattr(account, "study_schedule_enabled", True)
if enabled is False:
return None
current = as_cn(now)
this = start_at(account, current)
if current < this:
return this
return start_at(account, next_month_start(current))