12

I have

import arrow
s = '2015/12/1 19:00:00'
tz = 'Asia/Hong_Kong'

How can I parse this with Arrow such that I get an Arrow object with the time zone tz? The following defaults to UTC time.

In [30]: arrow.get(s, 'YYYY/M/D HH:mm:ss')
Out[30]: <Arrow [2015-12-01T19:00:00+00:00]>

I know the .to function but that converts a time zone and but doesn't allow me to change to time zone.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
mchangun
  • 9,814
  • 18
  • 71
  • 101
  • 1
    In arrow doc i see that the constructor works like : "class arrow.arrow.Arrow(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None)". Is that enough for your problem? It actually returns an arrow object with you desired tz. – Bestasttung Jan 22 '15 at 11:24

4 Answers4

18

Try this:

arrow.get(s, 'YYYY/M/D HH:mm:ss', tzinfo=tz)

If you are also using dateutil, this works as well:

arrow.get(s, 'YYYY/M/D HH:mm:ss', tzinfo=dateutil.tz.gettz(tz))

So does this:

arrow.get(s, 'YYYY/M/D HH:mm:ss').replace(tzinfo=dateutil.tz.gettz(tz))
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • This works! Just out of curiosity, is it possible to do the same with pytz? I get some weird results: `In [7]: arrow.get(s, 'YYYY/M/D HH:mm:ss').replace(tzinfo=pytz.timezone(tz)) Out[7]: ` – mchangun Jan 23 '15 at 01:10
  • 1
    Not sure, but I think arrow is better aligned with dateutil than pytz. – Matt Johnson-Pint Jan 23 '15 at 01:27
  • @mchangun: the correct way to parse the time if you don't want to ignore errors silently: `pytz.timezone('Asia/Hong_Kong').localize(datetime.strptime(s, '%Y/%m/%d %H:%M:%S'), is_dst=None)` – jfs Jan 23 '15 at 11:18
  • 1
    @mchangun: btw, I see that even after two years of development [`arrow` still fails to convert utc datetime to a named timezone, convert utc time to local timezone, and round-trip timestamp -> named timezone -> utc](https://gist.github.com/zed/4127162) – jfs Jan 23 '15 at 12:35
  • @J.F.Sebastian Do you have a recommendation for a better date time library? – mchangun Jan 27 '15 at 08:44
  • 1
    @mchangun: "better" depends on your application. I use `pytz` but it might too low-level for most applications. Though all you need is to understand why pytz introduces `localize()`, `normalize()` methods, to use it correctly. – jfs Jan 27 '15 at 08:49
  • 1
    @EricDuminil - Updated my answer. Thanks! – Matt Johnson-Pint Jan 27 '22 at 16:33
  • Excellent. Thanks. Sorry for the harsh comment. I was disappointed by the API, not by your answer. It looks good now – Eric Duminil Jan 27 '22 at 16:53
  • No problem. It was a very old answer. Not sure if that was the only API at the time or not. Also, did you know that Python 3.9+ has zoneinfo built in now? You probably don't need time zone support from a library anymore. – Matt Johnson-Pint Jan 27 '22 at 16:56
  • @MattJohnson-Pint: I'll check the new API. I wanted to use arrow because it makes it easy to "humanize" a datetime, something like "in 2 hours" or "3 days ago". – Eric Duminil Jan 28 '22 at 11:48
12

I'm not qualified yet to add a comment and would just like to share a bit simpler version of the answer with timezone str expression.

s = '2015/12/1 19:00:00'
tz = 'Asia/Hong_Kong'
arrow.get(s, 'YYYY/M/D HH:mm:ss').replace(tzinfo=tz)

or simply local timezone:

arrow.get(s, 'YYYY/M/D HH:mm:ss').replace(tzinfo='local')

or specified ISO-8601 style:

arrow.get(s, 'YYYY/M/D HH:mm:ss').replace(tzinfo='+08:00')
Quake Lai
  • 121
  • 2
  • 4
1

This is working for me as of 0.10.0:

import arrow
s = '2015/12/1 19:00:00'
tz = 'Asia/Hong_Kong'

arrow.get(s, 'YYYY/M/D HH:mm:ss', tzinfo=tz)
# <Arrow [2015-12-01T19:00:00+08:00]>

However, arrow.get('2018-01-29 14:46', tzinfo='US/Central') (i.e. without the format string) ignores the tzinfo parameter.

Raijinili
  • 13
  • 3
0

Per the current documentation, you can also provide a default timezone for arrow.get(), e.g.:

s = '2015/12/1 19:00:00'
tz = 'Asia/Hong_Kong'
arrow.get(s, tzinfo=tz)

However, as of right now (version 0.12.1) there is a shortcoming where that doesn't work for string-based date parsing. Fortunately, this has been fixed, so the next release of Arrow will integrate this fix.

fluffy
  • 5,212
  • 2
  • 37
  • 67