WoJ
WoJ

Reputation: 30035

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

Upvotes: 0

Views: 1466

Answers (2)

Will
Will

Reputation: 1068

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

Upvotes: 1

dnit13
dnit13

Reputation: 2496

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]>

Upvotes: 1

Related Questions