TypeError: can't compare offset-naive and offset-aware times
TypeError: can't compare offset-naive and offset-aware times
我有这个功能可以查看一周中的特定时间和日期。
它应该从周日 7:00PM 到周五 8:00PM 打印 1
并从星期五 8:00PM 到星期日 7:00PM
打印 0
我在周五 11:00AM 和 2:00PM 左右检查了函数,我在标题中收到了错误消息。
谁能准确解释错误的含义?我怎么可能解决它?它应该总是检查东部标准时间的时间
import pytz
from datetime import datetime, time, date
est = pytz.timezone('EST')
Mon = 0
Tue = 1
Wed = 2
Thur = 3
Fri = 4
Sat = 5
Sun = 6
weekdays = [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
首先,如果您在可识别 tz 的日期时间对象上调用 time()
方法,生成的 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)
这是帮我排序的:
dt_obj = dt_obj.replace(tzinfo=None)
见python – Can’t subtract offset-naive and offset-aware datetimes ... answer 1
我有这个功能可以查看一周中的特定时间和日期。 它应该从周日 7:00PM 到周五 8:00PM 打印 1 并从星期五 8:00PM 到星期日 7:00PM
打印 0我在周五 11:00AM 和 2:00PM 左右检查了函数,我在标题中收到了错误消息。
谁能准确解释错误的含义?我怎么可能解决它?它应该总是检查东部标准时间的时间
import pytz
from datetime import datetime, time, date
est = pytz.timezone('EST')
Mon = 0
Tue = 1
Wed = 2
Thur = 3
Fri = 4
Sat = 5
Sun = 6
weekdays = [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
首先,如果您在可识别 tz 的日期时间对象上调用 time()
方法,生成的 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)
这是帮我排序的:
dt_obj = dt_obj.replace(tzinfo=None)
见python – Can’t subtract offset-naive and offset-aware datetimes ... answer 1