| 1234567891011121314151617181920212223242526272829303132333435363738 |
- """按自然日(YYYY-MM-DD)解析为 [day_start, day_end) 的 create_time 筛选条件。"""
- from datetime import datetime, timedelta
- def current_month_bounds(now: datetime | None = None) -> tuple[datetime, datetime]:
- """当前自然月 [month_start, next_month_start),与服务器本地时间一致(项目约定 Asia/Shanghai)。"""
- if now is None:
- now = datetime.now()
- month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
- if now.month == 12:
- month_end = datetime(now.year + 1, 1, 1)
- else:
- month_end = datetime(now.year, now.month + 1, 1)
- return month_start, month_end
- def parse_create_time_day_bounds(apply_date: str | None) -> tuple[datetime, datetime] | None:
- if not apply_date or not str(apply_date).strip():
- return None
- try:
- day_start = datetime.strptime(str(apply_date).strip(), "%Y-%m-%d")
- return day_start, day_start + timedelta(days=1)
- except ValueError:
- return None
- def resolve_apply_create_time(apply_date: str | None, now: datetime | None = None) -> datetime:
- """申请日 YYYY-MM-DD → create_time;当天用当前时刻,其它日期用当日 0 点。"""
- if now is None:
- now = datetime.now()
- bounds = parse_create_time_day_bounds(apply_date)
- if not bounds:
- return now
- day_start, _ = bounds
- if day_start.date() == now.date():
- return now
- return day_start
|