Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a timezone aware string to datetime in Python without dateutil?

I have to convert a timezone-aware string like "2012-11-01T04:16:13-04:00" to a Python datetime object.

I saw the dateutil module which has a parse function, but I don't really want to use it as it adds a dependency.

So how can I do it? I have tried something like the following, but with no luck.

datetime.datetime.strptime("2012-11-01T04:16:13-04:00", "%Y-%m-%dT%H:%M:%S%Z") 
like image 465
lxyu Avatar asked Nov 01 '12 17:11

lxyu


People also ask

How do you make a timezone aware datetime object in Python?

To make them timezone-aware, you must attach a tzinfo object, which provides the UTC offset and timezone abbreviation as a function of date and time. For zones with daylight savings time, python standard libraries do not provide a standard class, so it is necessary to use a third party library.

How do I make datetime now aware time zone?

Timezone aware object using datetime now(). time() function of datetime module. Then we will replace the value of the timezone in the tzinfo class of the object using the replace() function. After that convert the date value into ISO 8601 format using the isoformat() method.

How do I remove the timezone from a datetime string in Python?

To remove timestamp, tzinfo has to be set None when calling replace() function. First, create a DateTime object with current time using datetime. now(). The DateTime object was then modified to contain the timezone information as well using the timezone.


1 Answers

As of Python 3.7, datetime.datetime.fromisoformat() can handle your format:

>>> import datetime >>> datetime.datetime.fromisoformat('2012-11-01T04:16:13-04:00') datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000))) 

In older Python versions you can't, not without a whole lot of painstaking manual timezone defining.

Python does not include a timezone database, because it would be outdated too quickly. Instead, Python relies on external libraries, which can have a far faster release cycle, to provide properly configured timezones for you.

As a side-effect, this means that timezone parsing also needs to be an external library. If dateutil is too heavy-weight for you, use iso8601 instead, it'll parse your specific format just fine:

>>> import iso8601 >>> iso8601.parse_date('2012-11-01T04:16:13-04:00') datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=<FixedOffset '-04:00'>) 

iso8601 is a whopping 4KB small. Compare that tot python-dateutil's 148KB.

As of Python 3.2 Python can handle simple offset-based timezones, and %z will parse -hhmm and +hhmm timezone offsets in a timestamp. That means that for a ISO 8601 timestamp you'd have to remove the : in the timezone:

>>> from datetime import datetime >>> iso_ts = '2012-11-01T04:16:13-04:00' >>> datetime.strptime(''.join(iso_ts.rsplit(':', 1)), '%Y-%m-%dT%H:%M:%S%z') datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000))) 

The lack of proper ISO 8601 parsing is being tracked in Python issue 15873.

like image 187
Martijn Pieters Avatar answered Oct 15 '22 13:10

Martijn Pieters