from datetime import datetime # 每个时段的分钟数(15分钟一个时段,一天共96个) SLOT_MINUTES = 15 def get_current_time_slot() -> int: """ 自动获取当前时间对应的时段块编号 (0~95) Returns: 当前时段块编号 (0-95) Examples: # >>> # 假设当前时间为 14:29 # >>> get_current_time_slot() # 57 """ now = datetime.now() h, m = now.hour, now.minute print(h,m) # 防御性校验(datetime.now() 理论上不会越界,但保留以维持原有健壮性) if not (0 <= h <= 23 and 0 <= m <= 59): raise ValueError(f"Invalid time: {h:02d}:{m:02d}") return (h * 60 + m) // SLOT_MINUTES print(get_current_time_slot())