commit 39499d8b3249d6d2142934085afb5c0f0088938c Author: unknown <45275340@qq.com> Date: Sun Nov 1 18:02:35 2026 +0800 初始化企业出行时间规划项目 diff --git a/task_company_2026-10.xlsx b/task_company_2026-10.xlsx new file mode 100644 index 0000000..15838e7 Binary files /dev/null and b/task_company_2026-10.xlsx differ diff --git a/时间规划函数.py b/时间规划函数.py new file mode 100644 index 0000000..a6712e2 --- /dev/null +++ b/时间规划函数.py @@ -0,0 +1,105 @@ +from datetime import datetime, time +from typing import Union + +import pandas + +# 常量定义 +SLOT_MINUTES = 15 +TOTAL_SLOTS = 24 * 60 // SLOT_MINUTES # 96 + + +def time_to_slot(t: Union[time, datetime, str]) -> int: + """ + 将固定时间转换为时段块编号 (0~95) + + Args: + t: 支持 datetime.time / datetime.datetime / "HH:MM"字符串 + + Returns: + 时段块编号 (0-95) + + Examples: + # >>> time_to_slot("00:00") # 0 + # >>> time_to_slot("14:00") # 56 + # >>> time_to_slot("14:29") # 57 (14:29 // 15 = 57) + # >>> time_to_slot("23:59") # 95 + """ + if isinstance(t, str): + parts = t.strip().split(":") + h, m = int(parts[0]), int(parts[1]) + elif isinstance(t, datetime): + h, m = t.hour, t.minute + elif isinstance(t, time): + h, m = t.hour, t.minute + else: + raise TypeError(f"Unsupported type: {type(t)}") + + if not (0 <= h <= 23 and 0 <= m <= 59): + raise ValueError(f"Invalid time: {h:02d}:{m:02d}") + + return (h * 60 + m) // SLOT_MINUTES + + +def get_current_slot() -> int: + """ + 获取当前本地时间所属的时段块编号 (0~95) + + Returns: + 当前时段块编号 + """ + now = datetime.now() + return time_to_slot(now) + +if __name__ == '__main__': + + test_timesq2 = ['21:30~21:45', "13:00~13:29"] + + for i in test_timesq2: + s_time, e_time = i.split('~') + s_slot = time_to_slot(s_time) + e_slot = time_to_slot(e_time) + print(s_slot) + print(e_slot) + pa = pandas.read_excel(r'C:\pyui软件项目\企业出行时间规划\task_company_2026-10.xlsx',sheet_name='Sheet1') + pa2 = pa.to_dict('records') + li = [] + for i in pa2: + crawl_time = (i['采集时段(必要)']) + s_time, e_time = crawl_time.split('~') + s_slot = time_to_slot(s_time) + e_slot = time_to_slot(e_time) + i['s_slot'],i['e_slot'] = s_slot,e_slot + li.append(i) + da = pandas.DataFrame(li) + da.to_excel('task_company_2026-11任务.xlsx') + + """ + test_times = [ + "00:00", # 一天开始 + "00:14", # slot 0 的右边界 + "00:15", # slot 1 的左边界 + "14:00", # 你的需求起点 + "14:14", # slot 56 的右边界 + "14:15", # slot 57 的左边界 + "14:29", # 你的需求终点 + "23:45", # slot 95 的左边界 + "23:59", # 一天结束 + ] + + test_timesq2 = ['21:30~21:45',"13:00~13:29"] + + for i in test_timesq2: + s_time,e_time = i.split('~') + s_slot = time_to_slot(s_time) + e_slot = time_to_slot(e_time) + print(s_slot) + print(e_slot) + + + for t_str in test_times: + slot = time_to_slot(t_str) + # 计算该slot对应的实际时间范围,方便验证 + start_h, start_m = divmod(slot * SLOT_MINUTES, 60) + end_m = start_m + SLOT_MINUTES + print(f" {t_str} → Slot {slot:>2} (覆盖 [{start_h:02d}:{start_m:02d}, {start_h:02d}:{end_m:02d}))") +""" \ No newline at end of file diff --git a/获取当前时段块.py b/获取当前时段块.py new file mode 100644 index 0000000..ad7caa2 --- /dev/null +++ b/获取当前时段块.py @@ -0,0 +1,28 @@ +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()) \ No newline at end of file