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.
1 2 3 4 |
pip install arrow <img class="alignnone wp-image-22570 size-full" src="http://all-learning.com/wp-content/uploads/2018/09/Python-Arrow-Module.png" alt="Python Arrow Module" width="1456" height="628" /> |
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.
1 2 3 4 5 6 7 8 9 |
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:
1 2 3 4 5 6 |
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.
1 2 3 4 |
pst_time = ist_time.to('US/Pacific') print('Current PST Time=", pst_time) |
Output:
1 2 3 |
Current PST Time = 2018-09-25T23:16:54.724375-07:00 |
Date to Timestamp to Date
1 2 3 4 5 |
print("Current Local Timestamp =', local_time.timestamp) dt = arrow.get(1537941232) print('Date from Timestamp =', dt) |
Output:
1 2 3 4 |
Current Local Timestamp = 1537942614 Date from Timestamp = 2018-09-26T05:53:52+00:00 |
Date to Formatted String
1 2 3 4 |
print('Formatted Date=", local_time.format()) print("Specific Formatted Date=", local_time.format("YYYY-MM-DD HH:mm:ss ZZ')) |
Output:
1 2 3 4 |
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
1 2 3 4 5 |
dt = arrow.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss') print(type(dt)) print(dt) |
Output:
1 2 3 4 |
<class 'arrow.arrow.Arrow'> 2013-05-05T12:30:45+00:00 |
Instantiating Date from Arguments
1 2 3 4 |
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.
1 2 3 4 5 6 7 8 |
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:
1 2 3 4 5 |
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
1 2 3 4 5 6 7 8 |
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:
1 2 3 4 5 6 |
an hour ago in an hour in einer Stunde in 2 hours |
Creating Arrow instance from datetime
1 2 3 4 5 6 7 |
from datetime import datetime dt = datetime.now() arrow_dt = arrow.Arrow.fromdate(dt) print(dt) print(arrow_dt) |
Output:
1 2 3 4 |
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.