在周末和节假日以及它们的前一天17点后不执行某某操作的python写法
我自己实现的方法,需要每年手动更新一次,但不涉及到对外接口的调用
#先预设好一些特殊日期
#法定节假日
law_holidays = ['2019-04-05','2019-04-06','2019-04-07','2019-05-01','2019-05-02','2019-05-03','2019-05-04',
'2019-06-07','2019-06-08','2019-06-09','2019-09-13','2019-09-14','2019-09-15',
'2019-10-01','2019-10-02','2019-10-03','2019-10-04','2019-10-05','2019-10-06','2019-10-07'
]
#法定节假日前一天
before1day_law = ['2019-04-04','2019-04-30',
'2019-06-06','2019-09-12',
'2019-09-30'
]
#要工作的周末
work_weekends = ['2019-04-28','2019-05-05','2019-09-29','2019-10-12']
#要工作的周六
work_Saturday = ['2019-10-12']
#服务器是UTC时间,转换成北京时间
china_datetime = datetime.datetime.now() + datetime.timedelta(hours=8)
#时间对象转成字符串
current_date = china_datetime.strftime('%Y-%m-%d')
#取小时
current_time = int(china_datetime.strftime('%H'))
#取星期几,0是星期一,后面以此类推
current_w = china_datetime.weekday()
#一天后
next_date = (china_datetime+datetime.timedelta(days=1)).strftime("%Y-%m-%d")
if current_date in law_holidays:
y= 0
elif current_date in before1day_law and current_time >= 17:
y= 0
elif current_date in work_weekends:
y= random.randint(0,1)
elif current_w in (5,6):
y= 0
elif current_w == 4 and current_time >= 17 and next_date not in work_Saturday:
y= 0
else:
y= random.randint(0,1)
#先预设好一些特殊日期
#法定节假日
law_holidays = ['2019-04-05','2019-04-06','2019-04-07','2019-05-01','2019-05-02','2019-05-03','2019-05-04',
'2019-06-07','2019-06-08','2019-06-09','2019-09-13','2019-09-14','2019-09-15',
'2019-10-01','2019-10-02','2019-10-03','2019-10-04','2019-10-05','2019-10-06','2019-10-07'
]
#法定节假日前一天
before1day_law = ['2019-04-04','2019-04-30',
'2019-06-06','2019-09-12',
'2019-09-30'
]
#要工作的周末
work_weekends = ['2019-04-28','2019-05-05','2019-09-29','2019-10-12']
#要工作的周六
work_Saturday = ['2019-10-12']
#服务器是UTC时间,转换成北京时间
china_datetime = datetime.datetime.now() + datetime.timedelta(hours=8)
#时间对象转成字符串
current_date = china_datetime.strftime('%Y-%m-%d')
#取小时
current_time = int(china_datetime.strftime('%H'))
#取星期几,0是星期一,后面以此类推
current_w = china_datetime.weekday()
#一天后
next_date = (china_datetime+datetime.timedelta(days=1)).strftime("%Y-%m-%d")
if current_date in law_holidays:
y= 0
elif current_date in before1day_law and current_time >= 17:
y= 0
elif current_date in work_weekends:
y= random.randint(0,1)
elif current_w in (5,6):
y= 0
elif current_w == 4 and current_time >= 17 and next_date not in work_Saturday:
y= 0
else:
y= random.randint(0,1)
留言
張貼留言