Python arrow
module enables date-time manipulations. It helps create instances and manipulate the timestamp accordingly.
It shows a user-friendly approach to deal with the date-time conversions.
Features:
- Arrow module is supported by Python 2.7 and higher versions.
- Time-zone aware
- Parses the string automatically
- Full level implementation
Installing the arrow module:
pip install arrow

Access the current timings of particular timezone
Example: To print the current timings of UTC, IST, and local timezone.
import arrow
utc = arrow.utcnow()
print('UTC Time =', utc)
ist = arrow.now('Asia/Calcutta')
print('IST Time =', ist)
print('tzinfo =', ist.tzinfo)
local_time = arrow.now()
print('Local Time =', local_time)
Output:

Conversion of timezone
Python arrow module provides to()
function to convert time-zones.
import arrow
ist = arrow.now('Asia/Calcutta')
print('IST Time =', ist)
pst = ist.to('US/Pacific')
print('PST Time =', pst)
Output:

How to get the date from timestamp?
import arrow
local = arrow.now()
print('Current Local Time =', local)
print('Current Local Timestamp =', local.timestamp)
date = arrow.get(local.timestamp)
print('Date from Timestamp =', date)
Output:

Formatting Date using arrow module
The format
method is used to manipulate and format the given date according to the user’s choice.
Example 1: To format date in YYYY-MM-DD format
import arrow
local = arrow.now()
result = local.format('YYYY-MM-DD')
print(result)
Output:
2020-01-02
Example 2: To format date in YYYY-MM-DD HH:mm:ss format
import arrow
local = arrow.now()
result = local.format('YYYY-MM-DD HH:mm:ss')
print(result)
Output:
2020-01-02 14:12:11
Parsing of Date to String
import arrow
date = arrow.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss')
print(date)
Output:
2013-05-05T12:30:45+00:00
How to instantiate date from passed arguments?
import arrow
date = arrow.get(2020, 1, 2)
print(date)
Output:
2020-01-02T00:00:00+00:00
Performing manipulations on date and time
The replace()
and shift()
methods are used to avail the future and past dates in accordance with the current date.
Example:
import arrow
utc = arrow.utcnow()
print('Current UTC= ', utc)
utc_updated1 = utc.replace(year=2017, month=9)
print('Updated UTC= ', utc_updated1)
utc_updated2 = utc.shift(years=-4, weeks=3)
print('Updated UTC= ', utc_updated2)
Output:

Representation of Date-Time in Human-Friendly format
The humanize()
method enables us to provide a human-friendly representation of the date/time in accordance with the current date/time.
humanize()
method enables a user to know the amount of time elapsed since the given time.
Example:
import arrow
local = arrow.now()
print(local)
result = local.humanize()
print("Time elapsed: ")
print(result)
Output:
2020-01-02T14:34:40.447432+05:30
Time elapsed:
just now
Conclusion
Thus, in this article, we have understood the functionalities presented by the arrow module. It’s a simple module to work with the date and time having timezone support.
References
- Python arrow module
- Python arrow module Documentation