How to use arrow's string parsing, and simultaneously set a timezone?


I would like, using arrow, to parse dates from strings. I do it via the documented way:

>>> arrow.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss')
<Arrow [2013-05-05T12:30:45+00:00]>

The string is parsed with the timezone +00:00. Is it possible to force another timezone for this string?

Converting to the local timezone afterwards

>>> arrow.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss').to('local')
<Arrow [2013-05-05T14:30:45+02:00]>

is not the right solution, as the date is first parsed to +00:00, then converted to another timezone – and the hour is modified accordingly (which is the expected behaviour for .to())

Asked By: WoJ



Passing tzinfo=tz.tzlocal() in get method will do it:

>>> import arrow
>>> from dateutil import tz
>>> arrow.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss', tzinfo=tz.tzlocal())
<Arrow [2013-05-05T12:30:45+02:00]>
Answered By: dnit13

With version 1.2.3+, you can just use 'local' for tzinfo:

arrow.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss', tzinfo='local')
Answered By: Will
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.