2
ナイーブなdatetime
オブジェクトとタイムゾーンを認識するdatetime
オブジェクトを比較しようとしています。タイムゾーン形式のファイルをPythonの素朴なdatetimeオブジェクトに渡す方法
私は変更する必要があります。
lastDate = start_date + ' ' + errorTime
lastDate = datetime.strptime(lastDate, '%Y-%m-%d %H:%M:%S')
time_diff = lastDate - FirstDate
TypeError: can't compare offset-naive and offset-aware datetimes
はまず...私は私の2つの日時オブジェクトのtzinfoのを確認...
>>>FirstDate.tzinfo
>>>tzfile(u'/usr/share/zoneinfo/Europe/London')
>>>lastDate.tzinfo
>>>
lastDate
がTZ認識していないので、これが期待されている:
私はその後pytz
を輸入し、ナイーブlastDate
DateTimeオブジェクトに変換:どのように私はナイーブを与えない...
>>>FirstDate.tzinfo
>>>tzfile(u'/usr/share/zoneinfo/Europe/London')
>>>lastDate.tzinfo
>>><DstTzInfo 'Europe/London' GMT0:00:00 STD>
私は困惑:私が持っている
lastDate = start_date + ' ' + errorTime
lastDate = datetime.strptime(lastDate, '%Y-%m-%d %H:%M:%S')
lastDate = pytz.timezone('Europe/London').localize(lastDate)
time_diff = lastDate - FirstDate
TypeError: Timestamp subtraction must have the same timezones or no timezones
が再びタイムゾーンの確認をdatetimeオブジェクトlastDate
tzfile?
注:私はdatetime
オブジェクトFirstDate
意識TZに一致するようにナイーブdatetime
オブジェクトlastDate
を変換する必要があります。私はFirstDate
のtzを修正することはできません。
こんにちは@Stephen私はこれを試して、今私は次のエラーが表示されます: 'AttributeError: 'tzfile'オブジェクトには属性 'localize''がありません – nrs90