Files
NJUPT-Suan-API/router/enhance/week.py
MangoFanFanw b284c3c260 Python 后端提交
Python 后端(FastAPI + FastMCP + ...)的初始版本号设定为 0.1.0,这是 uv 在 pypriject.toml
里给我自动设置的,我觉得有道理。
2026-04-21 13:38:46 +08:00

30 lines
933 B
Python
Raw 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, timedelta
def get_semester_week_info(start: date, target: date) -> tuple[int, int]:
"""
给定学期开始日期(第一周周一)和另一指定日期,计算指定的日期是第几周的星期几。
Args:
start: 学期开始的日期date
target: 指定日期date
Returns:
包含两个数字的元组,分别为第几周和星期几。
"""
return (target - start).days // 7 + 1, target.isoweekday()
def get_week_day_info(target: date) -> tuple[date, date]:
"""
给定一个指定日期,获取该日所在的星期的周一和周日的日期。
Args:
target: 指定日期date
Returns:
包含两个 date 的元组,分别为周一和周日。
"""
weekday_int = target.weekday()
return target - timedelta(days=weekday_int), target + timedelta(days=6 - weekday_int)