初始化企业出行时间规划项目
This commit is contained in:
@@ -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}))")
|
||||
"""
|
||||
Reference in New Issue
Block a user