dateutil timezone question
larry.martell at gmail.com
Fri Dec 23 14:32:54 EST 2016
On Fri, Dec 23, 2016 at 2:18 PM, Chris Angelico <rosuav at gmail.com> wrote:
> On Sat, Dec 24, 2016 at 3:30 AM, Larry Martell <larry.martell at gmail.com> wrote:
>> I have a datetime that looks like this: '2016-11-11T18:10:09-05:00'
>> and when I pass it to dateutil.parser.parse I get back this:
>> datetime.datetime(2016, 11, 11, 18, 10, 9, tzinfo=tzoffset(None, -18000))
>> And I have other datetimes like this: '2016-04-27T00:00:00', which
>> went passed to dateutil.parser.parse of course does not return a
>> datetime with the tzinfo.
>> I need to compare these datetimes, and if I do that I get the dreaded
>> "can't compare offset-naive and offset-aware datetimes" error.
> Some of your timestamps have timezones and others don't. That's a
> fundamental problem. Are you absolutely certain that the ones without
> them are in UTC? If so, the easiest fix would be to slap a "Z" on them
> before you parse, which would give you exclusively aware datetimes.
What I ended up doing, which I think is somewhat kludgy is this:
if datestr is None or len(datestr) < 6:
if datestr[-6] == ':':
Then I pass all my dates through that before calling dateutil.parser.parse
More information about the Python-list