Tue 5 Aug 2014
The arrow project is an attempt to wrap Python’s time and datetime modules into a single API. It also claims to plug gaps in functionality in those modules, such as time spans, ISO-8601 and humanization. You can kind of think of arrow as a drop-in replacement for Python’s datetime and time modules, much like the requests project can be used instead of Python’s urllib. Arrow supports Python 2.6, 2.7 and 3.3 at the time of this writing.
To get started using arrow, you can just pip to install it:
pip install arrow
The arrow package is quite simple to use. Let’s look at a few common examples:
>>> import arrow >>> arrow.now() <Arrow [2014-07-25T15:41:23.934359-05:00]> >>> now = arrow.now() >>> now.ctime() 'Fri Jul 25 15:41:30 2014' >>> pacific = now.to("US/Pacific") >>> pacific <Arrow [2014-07-25T13:42:34.957275-07:00]> >>> pacific.timestamp 1406320954
Here we get the day’s date and time. Then we store it in a variable and change the time zone to Pacific Standard Time. We can also get the timestamp value, which is seconds since the epoch. Let’s look at a few more examples:
>>> day = arrow.get("2014-07-13") >>> day.format("MM-DD-YYYY") '07-13-2014' >>> day.humanize() u'12 days ago'
Here we choose a date and then reformat how it is displayed. If you call arrow’s humanize() method, it will tell you how many days ago it was. If you were to get what time it is right now via the now() method and then called humanize, you would get a different message back.
What I like most about this package is that it does wrap Python’s date and time modules quite well. It’s nice to be able to access each of their functions through one common interface. The author(s) have made date and time manipulation easier to use. I think it’s worth your time to give it a try. Have fun and happy coding!