feat: 每月定时开始学习并改为本地计算心跳摘要

This commit is contained in:
2026-11-15 01:30:24 +08:00
parent e9a1e9b315
commit caa79d79b7
14 changed files with 510 additions and 35 deletions
+80
View File
@@ -0,0 +1,80 @@
"""每月自动学习开始时刻。精确到秒,按北京时间判断当月是否已到点。"""
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)
return {
"day": day,
"hour": hour,
"minute": minute,
"second": second,
"label": label_of(account),
}
def window_open(account, now: datetime | None = None) -> bool:
"""当前是否已过该账号本月开始时刻。2 月 31 日等按当月最后一天。"""
current = now or datetime.now(CN_TZ)
if current.tzinfo is None:
current = current.replace(tzinfo=CN_TZ)
else:
current = current.astimezone(CN_TZ)
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]
start = datetime(
current.year,
current.month,
min(day, last),
hour,
minute,
second,
tzinfo=CN_TZ,
)
return current >= start