You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
2.1 KiB
38 lines
2.1 KiB
|
2 weeks ago
|
import datetime
|
||
|
|
from chinese_calendar import (is_holiday, is_workday)
|
||
|
|
|
||
|
|
|
||
|
|
def get_data_datetime(show_date):
|
||
|
|
show_date_obj = datetime.datetime.strptime(show_date, '%Y-%m-%d')
|
||
|
|
target_start_datetime_obj = show_date_obj + datetime.timedelta(hours=6)
|
||
|
|
target_end_datetime_obj = show_date_obj + datetime.timedelta(hours=29, minutes=59, seconds=59)
|
||
|
|
data_start_datetime_obj = show_date_obj + datetime.timedelta(days=-7) + datetime.timedelta(hours=6)
|
||
|
|
data_end_datetime_obj = show_date_obj + datetime.timedelta(hours=5, minutes=59, seconds=59)
|
||
|
|
history_start = datetime.datetime.strftime(data_start_datetime_obj, '%Y-%m-%d %H:%M:%S')
|
||
|
|
history_end = datetime.datetime.strftime(data_end_datetime_obj, '%Y-%m-%d %H:%M:%S')
|
||
|
|
target_start = datetime.datetime.strftime(target_start_datetime_obj, '%Y-%m-%d %H:%M:%S')
|
||
|
|
target_end = datetime.datetime.strftime(target_end_datetime_obj, '%Y-%m-%d %H:%M:%S')
|
||
|
|
return history_start, history_end, target_start, target_end
|
||
|
|
|
||
|
|
|
||
|
|
def get_date_type(show_date, template_date):
|
||
|
|
show_date_obj = datetime.datetime.strptime(show_date, '%Y-%m-%d')
|
||
|
|
template_date_obj = datetime.datetime.strptime(template_date, '%Y-%m-%d')
|
||
|
|
result = dict()
|
||
|
|
# 处理参考数据日期
|
||
|
|
history_date_list = [(show_date_obj - datetime.timedelta(days=i)) for i in range(7, 0, -1)]
|
||
|
|
history_date = '、'.join(
|
||
|
|
[f'{datetime.date.strftime(d, "%Y-%m-%d")}({"节假日" if is_holiday(d) else "工作日"})' for d in
|
||
|
|
history_date_list])
|
||
|
|
# 处理目标日期
|
||
|
|
show_date = f'{datetime.date.strftime(show_date_obj, "%Y-%m-%d")}({"节假日" if is_holiday(show_date_obj) else "工作日"})'
|
||
|
|
show_date_is_holiday = is_holiday(show_date_obj)
|
||
|
|
# 处理模板日期
|
||
|
|
template_date = f'{datetime.date.strftime(template_date_obj, "%Y-%m-%d")}({"节假日" if is_holiday(template_date_obj) else "工作日"})'
|
||
|
|
template_date_is_holiday = is_holiday(template_date_obj)
|
||
|
|
return history_date, show_date, show_date_is_holiday, template_date, template_date_is_holiday
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
get_date_type("2026-06-13", "2026-06-01")
|