Python Arrow module is a replacement library for datetime. It allows easy creation of date and time instances with timezone awareness. It’s a simple module with a human-friendly approach to creating, manipulating, formatting and converting dates, times, and timestamps.
Python Arrow Module
We can install python arrow module using PIP command.
pip install arrow
Python arrow module is Timezone-aware & UTC by default. It provides support for creating dates from arguments, timestamp etc. We can easily perform date time manipulations, converting one timezone to another, shift time to get future or past date, format date to string and parse a string to create date instance.
Python Arrow Example
Let’s see how to use arrow module to get current UTC time, IST time and local time.
utc_time = arrow.utcnow()
print('Current UTC Time =', utc_time)
ist_time = arrow.now('Asia/Calcutta')
print('Current IST Time =', ist_time)
print('tzinfo =', ist_time.tzinfo)
local_time = arrow.now()
print('Current Local Time =', local_time)
Output:
Current UTC Time = 2018-09-26T06:16:54.724068+00:00
Current IST Time = 2018-09-26T11:46:54.724375+05:30
tzinfo = tzfile('/usr/share/zoneinfo/Asia/Calcutta')
Current Local Time = 2018-09-26T11:46:54.724472+05:30
Converting Timezone
We can use to() function to convert one timezone to another.
pst_time = ist_time.to('US/Pacific')
print('Current PST Time =', pst_time)
Output:
Current PST Time = 2018-09-25T23:16:54.724375-07:00
Date to Timestamp to Date
print('Current Local Timestamp =', local_time.timestamp)
dt = arrow.get(1537941232)
print('Date from Timestamp =', dt)
Output:
Current Local Timestamp = 1537942614
Date from Timestamp = 2018-09-26T05:53:52+00:00
Date to Formatted String
print('Formatted Date =', local_time.format())
print('Specific Formatted Date =', local_time.format('YYYY-MM-DD HH:mm:ss ZZ'))
Output:
Formatted Date = 2018-09-26 11:46:54+05:30
Specific Formatted Date = 2018-09-26 11:46:54 +05:30
Parse String to Date
dt = arrow.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss')
print(type(dt))
print(dt)
Output:
<class 'arrow.arrow.Arrow'>
2013-05-05T12:30:45+00:00
Instantiating Date from Arguments
dt = arrow.get(2018, 9, 26)
print(dt)
Output: 2018-09-26T00:00:00+00:00
Date Time Manipulations
We can use replace() and shift() function to get future and past dates.
utc_time = arrow.utcnow()
print('Current UTC Time =', utc_time)
utc_time_updated = utc_time.replace(year=2019, month=6)
print('Updated UTC Time =', utc_time_updated)
utc_time_updated = utc_time.shift(years=-2, weeks=4)
print('Updated UTC Time =', utc_time_updated)
Output:
Current UTC Time = 2018-09-26T06:16:54.727167+00:00
Updated UTC Time = 2019-06-26T06:16:54.727167+00:00
Updated UTC Time = 2016-10-24T06:16:54.727167+00:00
Relative Date to Human Readable Format
past = arrow.utcnow().shift(hours=-1)
print(past.humanize())
future = arrow.utcnow().shift(hours=+1)
print(future.humanize())
print(future.humanize(locale='de_DE'))
print(future.humanize(past))
Output:
an hour ago
in an hour
in einer Stunde
in 2 hours
Creating Arrow instance from datetime
from datetime import datetime
dt = datetime.now()
arrow_dt = arrow.Arrow.fromdate(dt)
print(dt)
print(arrow_dt)
Output:
2018-09-26 12:34:57.532227
2018-09-26T00:00:00+00:00
That’s all for a brief introduction of python arrow module. There are many other functions available in this module, you can get complete details from their documentation.
Summary
Python arrow module is a drop-in replacement for built-in datetime module. It’s timezone aware and there are many utility functions to help us in date time operations. It’s very similar to python pendulum module.