import datetime
import chinese_calendar
import time
import pandas as pd
def timestamp_to_str(timestamp=None, format='%Y-%m-%d %H:%M:%S'):
if timestamp:
time_tuple = time.localtime(timestamp)
result = time.strftime(format, time_tuple)
return result
else:
return time.strptime(format)
def get_normal_special_day_list(day_nums=20):
normal_day_list_start = []
special_day_list_start = []
normal_day_list_end = []
special_day_list_end = []
today = datetime.date.today()
today_time = int(time.mktime(today.timetuple()))
oneday_time = 86400
for i in range(day_nums):
today_time -= oneday_time
today_time_str = timestamp_to_str(today_time)
today_end_time_str = timestamp_to_str(today_time + oneday_time - 1)
t = pd.Timestamp(today_time_str)
if chinese_calendar.is_holiday(t.date()) == True:
special_day_list_start.append(today_time_str)
special_day_list_end.append(today_end_time_str)
else:
normal_day_list_start.append(today_time_str)
normal_day_list_end.append(today_end_time_str)
return normal_day_list_start, special_day_list_start, normal_day_list_end, special_day_list_end
normal_day_list_start, special_day_list_start, normal_day_list_end, special_day_list_end = get_normal_special_day_list()
print(normal_day_list_start)
print(normal_day_list_end)
print(special_day_list_start)
print(special_day_list_end)
结果如下:
['2022-03-18 00:00:00', '2022-03-17 00:00:00', '2022-03-16 00:00:00', '2022-03-15 00:00:00', '2022-03-14 00:00:00', '2022-03-11 00:00:00', '2022-03-10 00:00:00', '2022-03-09 00:00:00', '2022-03-08 00:00:00', '2022-03-07 00:00:00', '2022-03-04 00:00:00', '2022-03-03 00:00:00', '2022-03-02 00:00:00', '2022-03-01 00:00:00']
['2022-03-18 23:59:59', '2022-03-17 23:59:59', '2022-03-16 23:59:59', '2022-03-15 23:59:59', '2022-03-14 23:59:59', '2022-03-11 23:59:59', '2022-03-10 23:59:59', '2022-03-09 23:59:59', '2022-03-08 23:59:59', '2022-03-07 23:59:59', '2022-03-04 23:59:59', '2022-03-03 23:59:59', '2022-03-02 23:59:59', '2022-03-01 23:59:59']
['2022-03-20 00:00:00', '2022-03-19 00:00:00', '2022-03-13 00:00:00', '2022-03-12 00:00:00', '2022-03-06 00:00:00', '2022-03-05 00:00:00']
['2022-03-20 23:59:59', '2022-03-19 23:59:59', '2022-03-13 23:59:59', '2022-03-12 23:59:59', '2022-03-06 23:59:59', '2022-03-05 23:59:59']
|