0

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

WoJ
  • 27,165
  • 48
  • 180
  • 345
  • Not sure, but from docs you can pass `tzinfo=tz.tzlocal()` – dnit13 Sep 25 '17 at 16:54
  • @dnit13: you are correct. I did not realize that one could add `tzinfo` when parsing a string (though I should have tried). If you could please turn you comment into an answer I would be glad to accept it. – WoJ Sep 25 '17 at 18:14

2 Answers2

1

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]>
WoJ
  • 27,165
  • 48
  • 180
  • 345
dnit13
  • 2,478
  • 18
  • 35
1

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')
WoJ
  • 27,165
  • 48
  • 180
  • 345
Will
  • 928
  • 11
  • 10