# 过滤周末
def reF(day):
if int(day.strftime('%w')) in [0, 6]:
return 0
else:
return 1
# 时间差
def reC(date1, date2):
return date2 - date1
# 计算天数 默认一天等与11小时
def reD(start_date, index):
m = 0
for x in range(index):
if x > 0: # 当天不算
day = start_date + timedelta(days=x)
m += reF(day)
return m * 11
# 计算小于1天的小时数
def reE(conut, d1, d2):
if conut > 0:
m = d1.replace(hour=18, minute=0, second=0, microsecond=0)
n = d2.replace(hour=7, minute=0, second=0, microsecond=0)
d = (d2 - n).total_seconds() + (m - d1).total_seconds()
return d / 60 / 60
else:
return 0
if __name__ == '__main__':
d1 = datetime.strptime('2019-10-25 14:41:20', '%Y-%m-%d %H:%M:%S')
d2 = datetime.strptime('2019-10-26 15:41:20', '%Y-%m-%d %H:%M:%S')
cha = reC(d1, d2)
chaday = cha.days # 差几天
chaseconds = cha.total_seconds() # 差多少秒
sum = 0
if chaday > 0:
sum = reD(d1, chaday)
c = chaseconds - chaday * 24 * 60 * 60
print('%d - %d = %d' % (chaseconds, chaday * 24 * 60 * 60, c))
print(reE(c, d1, d2))
sum += int(reE(c, d1, d2))
else:
# if 判断下 结束时间不能在周末
sum = int(chaseconds / 60 / 60)
print(sum)