dateutil timezone question

Larry Martell larry.martell at
Fri Dec 23 14:32:54 EST 2016

On Fri, Dec 23, 2016 at 2:18 PM, Chris Angelico <rosuav at> wrote:
> On Sat, Dec 24, 2016 at 3:30 AM, Larry Martell <larry.martell at> 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:

def add_tz(datestr):
    if datestr is None or len(datestr) < 6:
        return datestr
    if datestr[-6] == ':':
        return datestr+'-00:00'
        return datestr

Then I pass all my dates through that before calling dateutil.parser.parse

More information about the Python-list mailing list