亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

TypeError:無法比較原始偏移量和偏移量感知時間

TypeError:無法比較原始偏移量和偏移量感知時間

慕俠2389804 2023-03-16 15:30:54
我有這個功能來檢查一周中的特定時間和日期。它應該從周日晚上 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)


查看完整回答
反對 回復 2023-03-16
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

這為我排序了:

dt_obj = dt_obj.replace(tzinfo=None)

請參見python – 無法減去原始偏移和偏移感知日期時間 ... 答案 1


查看完整回答
反對 回復 2023-03-16
  • 2 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號