Trying to parse the datetime string with timezone info and get the utc offset
from dateutil.parser import parse as parse_date
s = '2017-08-28 06:08:20,488 CDT'
dt = parse_date(s)
print(dt.utcoffset()) # prints `None`
Why is utcoffset returning None
rather than -5
as offset?
From the datetime docs:
In your code,
dt.tzinfo
isNone
, so the timezone information was not parsed byparse_date
into thedt
. Your datetimedt
is "naive" (has no timezone information).As per the dateutil docs, You can pass your own timezone information to
parse_date
as either atzoffset
ortzfile
:Or you can encode the timezone offset into the string: