aps-agent/server/timeutil.py

37 lines
1.8 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.

# ============================================================
# 日期时间工具(moduleId: server-timeutil, 可重生 ✅)
# 与 legacy aps-frontend/js/data.js 的 $utils 语义一致:
# 字符串格式统一 "YYYY-MM-DD" 与 "YYYY-MM-DD HH:MM"
# ============================================================
from __future__ import annotations # 前向类型引用
from datetime import datetime, timedelta # 标准日期时间类型
def fmt_date(d: datetime) -> str:
"""格式化为日期字符串 YYYY-MM-DD(与 data.js fmtDate 对齐)。"""
return d.strftime("%Y-%m-%d") # 固定零填充格式
def fmt_dt(d: datetime) -> str:
"""格式化为日期时间字符串 YYYY-MM-DD HH:MM(与 data.js fmtDateTime 对齐)。"""
return d.strftime("%Y-%m-%d %H:%M") # 分钟精度(排产粒度 15 分钟足够)
def parse_dt(s: str) -> datetime:
"""解析日期/日期时间字符串(兼容两种格式,与 data.js parseDate 对齐)。"""
if len(s) <= 10: # 仅日期(10 个字符)
return datetime.strptime(s, "%Y-%m-%d") # 解析为当日零点
return datetime.strptime(s, "%Y-%m-%d %H:%M") # 含时分的完整解析
def add_minutes(d: datetime, minutes: float) -> datetime:
"""时间加分钟(与 data.js addMinutes 对齐)。"""
return d + timedelta(minutes=minutes) # timedelta 直接支持浮点分钟
def today0() -> datetime:
"""今天零点(与 data.js today 对齐),作为种子/排产的相对基准。"""
now = datetime.now() # 当前本地时间
return now.replace(hour=0, minute=0, second=0, microsecond=0) # 归零到当日 00:00