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

1.3k views Asked by At

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())

2

There are 2 answers

0
dnit13 On BEST ANSWER

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]>
0
Will On

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')