我有这个功能来检查一周中的特定时间和日期。它应该从周日晚上 7:00 到周五晚上 8:00 打印 1 并从周五晚上 8:00 到周日晚上 7:00 打印 0我在周五上午 11:00 和下午 2:00 左右检查了函数,我在标题中收到了错误消息。有人可以准确解释错误的含义吗?我怎么可能解决它?它应该总是检查东部标准时间的时间import pytzfrom datetime import datetime, time, dateest = pytz.timezone('EST')Mon = 0Tue = 1Wed = 2Thur = 3Fri = 4 Sat = 5 Sun = 6weekdays = [Mon, Tue, Wed, Thur]edgecases = [Sun, Fri]weekend = [Sat]curr_day = datetime.now(tz=est).date().weekday()curr_time = datetime.now(tz=est).time()def checktime(curr_day, curr_time): if curr_day in weekdays or (curr_day == Sun and curr_time > time(19,00,tzinfo=est)) or (curr_day == Fri and curr_time < time(20,00,tzinfo=est)): print(1) elif curr_day in weekend or (curr_day == Fri and curr_time >= time(20,00,tzinfo=est)) or (curr_day == Sun and curr_time <= time(19,00,tzinfo=est)): print(0)回溯错误:Traceback (most recent call last): File ".\testingtime.py", line 73, in <module> checktime(curr_day, curr_time) File ".\testingtime.py", line 67, in checktime if curr_day in weekdays or (curr_day == Sun and curr_time > time(19,00,tzinfo=est)) or (curr_day == Fri and curr_time < time(20,00,tzinfo=est)):TypeError: can't compare offset-naive and offset-aware times
2 回答
DIEA
TA贡献1820条经验 获得超2个赞
其一,如果您time()在可识别 tz 的日期时间对象上调用该方法,则生成的time对象将不再携带时区信息 - 因为假设没有日期就没有意义。其次,由于您与静态时间相比,您在那里不需要时区。因此,您可以将您的功能简化为
def checktime(curr_day, curr_time):
if curr_day in weekdays or (curr_day == Sun and curr_time > time(19,00)) or (curr_day == Fri and curr_time < time(20,00)):
print(1)
elif curr_day in weekend or (curr_day == Fri and curr_time >= time(20,00)) or (curr_day == Sun and curr_time <= time(19,00)):
print(0)
忽然笑
TA贡献1806条经验 获得超5个赞
添加回答
举报
0/150
提交
取消