ZULU MODULE IN PYTHON
Working with dates and times in programming can be cumbersome. Click here to know why. Luckily, you rarely need to implement these entangled features from the first line nowadays, since open-source modules are accessible and always there to assist you.
1. datetime module: It provides classes for controlling dates and times.
The datetime module gives three classes that majority people will use:
- datetime.date is a glorified date that accepts the Gregorian calendar and broadens unendingly into the future and past. This object stores the year, month, and day as attributes.
- datetime.time is a glorified time that accepts that there are 86,400 seconds out of every day with no leap seconds. This object stores the hour, minute, second, microsecond, and time zone data.
- datetime.datetime is a mix of the date class and the time class. It has all the characteristics of the two classes.
There are many other functions available in this module, you can get complete details from the documentation.
2. arrow module :
Python Arrow module is a replacement library for datetime. It is Time zone aware & UTC(Coordinated Universal Time) by default. It offers support for creating dates from arguments, timestamp etc. Without much of a stretch we can perform date time controls, changing over one time-region to another, move time to get future or past date, position date to string and parse a string to make date instance.
- arrow.now : It is used to get local time, date and timestamps.
- arrow.utcnow : This gets the coordinated universal time long with the date and time stamp.
- arrow.get : It is used to obtain a date from a timestamp, while parsing string to date and instantiating date from arguments.
- to() : This function converts one timezone to another.
- replace() and shift() : These functions can be used to get future and past dates.
Visit here to know how to use these functions in code.
Click here for detail documentation of the arrow module.
3. zulu module
Zulu module can be considered as an improved version for the popular datetime module and the arrow module. In this, all datetime objects changed over into UTC and also stored as UTC. It undertakes multiple string formatting using strptime and strftime directives and Unicode date patterns. This module can be installed by the command
pip3 install zulu
Here are some features of the Zulu Module:
- It converts all the datetime objects and stores it as UTC .
- ISO8601 formatted strings and POSIX timestamps are parsed by default.
Take a look at the python code below :
import zulu# Zulu objects print(“when we pass timezone: ”,zulu.parse(‘2020–06–02T16:10:15.708064+00:00’))print(“when only date is passed:”,zulu.parse(‘2020–06–02’))print(“when we pass date and time both:”,zulu.parse(‘2020–06–02 16:10’))print(“when ISO8601 is passed as formats:”,zulu.parse(‘2020–06–02T16:10:15.708064+00:00’,zulu.ISO8601))print(“when we use Default timezone:”,zulu.parse(‘2020–06–02’,default_tz =’US/Eastern’))
The output for above mentioned code:
- The Time zone representation applied when casting to native datetime object and while string output formatting only.
- Provision of Drop-in replacement for native datetime objects.
- It supports Python 3.4+.
Take a tour of some commonly used Zulu classes and their implementations below.
import zulu
zulu.now()
# <Zulu [2020-06-02T19:33:18.137493+00:00]>
dt = zulu.parse('2020-06-02T19:33:18.137493+00:00')
# <Zulu [2020-06-02T19:33:18.137493+00:00]>
dt = zulu.create(2020, 6, 02, 19, 33, 18, 137493)
# <Zulu [2020-06-02T19:33:18.137493+00:00]>
dt.isoformat()
# '2020-06-02T19:33:18.137493+00:00'
dt.timestamp()
# 1469475198.137493
dt.naive
# datetime.datetime(2020, 6, 02, 19, 33, 18, 137493)
dt.datetime
# datetime.datetime(2020, 6, 02, 19, 33, 18, 137493, tzinfo=<UTC>)
dt.format('%Y-%m-%d')
# 2020-06-02
dt.format('YYYY-MM-dd')
# 2020-06-02
dt.format("E, MMM d, ''YY")
# "Mon, Jun 02, '20"
dt.format("E, MMM d, ''YY", locale='de')
# "Mo., Juni 02, '20"
dt.format("E, MMM d, ''YY", locale='fr')
# "lun., juin. 02, '16"
dt.shift(hours=-5, minutes=10)
# <Zulu [2020-06-02T14:43:18.137493+00:00]>
dt.replace(hour=14, minute=43)
# <Zulu [2020-06-02T14:43:18.137493+00:00]>
dt.start_of('day')
# <Zulu [2020-06-02T00:00:00+00:00]>
dt.end_of('day')
# <Zulu [2020-06-02T23:59:59.999999+00:00]>
dt.span('hour')
# (<Zulu [2020-06-02T19:00:00+00:00]>, <Zulu [2020-06-02T19:59:59.999999+00:00]>)
dt.time_from(dt.end_of('day'))
# '4 hours ago'
dt.time_to(dt.end_of('day'))
# 'in 4 hours'
list(zulu.range('hour', dt, dt.shift(hours=4)))
# [Zulu [2020-06-02T19:33:18.137493+00:00]>,
# Zulu [2020-06-02T20:33:18.137493+00:00]>,
# Zulu [2020-06-02T21:33:18.137493+00:00]>,
# Zulu [2020-06-02T22:33:18.137493+00:00]>]
list(zulu.span_range('minute', dt, dt.shift(minutes=4)))
# [(Zulu [2020-06-02T19:33:00+00:00]>, Zulu [2020-06-02T19:33:59.999999+00:00]>),
# (Zulu [2020-06-02T19:34:00+00:00]>, Zulu [2020-06-02T19:34:59.999999+00:00]>),
# (Zulu [2020-06-02T19:35:00+00:00]>, Zulu [2020-06-02T19:35:59.999999+00:00]>),
# (Zulu [2020-06-02T19:36:00+00:00]>, Zulu [2020-06-02T19:36:59.999999+00:00]>)]
zulu.parse_delta('1w 3d 2h 32m')
# <Delta [10 days, 2:32:00]>
zulu.parse_delta('2:04:13:02.266')
# <Delta [2 days, 4:13:02.266000]>
zulu.parse_delta('2 days, 5 hours, 34 minutes, 56 seconds')
# <Delta [2 days, 5:34:56]>
Let’s take a look at the advantages of this module over the popular datetime module.
- Zulu has python-dateutil time zone support. Also it has a coterie of extended datetime features such as parse(), format(), shift().
Take a look at the code provided below :
import zuludt = zulu.parse(‘2020–06–02T16:10:15.708064+00:00’)print(“The Datetime string without timezone is:”,dt.format(‘% m/% d/% y % H:% M:% S % z’))print(“The Datetime string without timezone is:”,dt.format(‘MM / dd / YY HH:mm:ss Z’))print(“The Datetime string when timezone is given is:”,dt.format(‘% Y-% m-% d % H:% M:% S % z’,tz =’US/Eastern’))print(“The Datetime string when timezone is given as local is:”,dt.format(‘%Y-%m-%d %H:%M:%S %z’,tz =’local’))#shift() used here dt = zulu.parse(‘2020–06–02T16:10:15.708064+00:00’)shifted1 = dt.shift(hours =-5, minutes = 10)print(“The shifted time is:”, shifted1)shifted2 = dt.shift(minutes = 55, seconds = 11,microseconds = 10)print(“The new shifted time is:”, shifted2)
- It Parses ISO8601 and timestamps naturally with no additional arguments.
- It is Simpler to reason about Zulu objects as they are just UTC datetimes.
- Zulu provides Clear description between UTC and other time zones where time zone representation can only be used for displaying datetime or transformation to native datetime.
- It Supports more string parsing/formatting choices including Unicode date designs as well as strptime/strftime orders.
Let’s take a look at the advantages of this module over the arrow module.
- Zulu is a drop-in swap for local datetimes (acquired from datetime.datetime). Zulu is always a datetime hence it is not essential to convert using arrow.datetime.
- It provides stricter parsing to stay away from quiet errors. For instance, one may expect
arrow.get(‘06/03/2020’, ‘MM/DD/YY’)
to fail (“input does not match format” error) but it readily returns <Arrow[2020–06–03T00:00:00+00:00) while
zulu.parse(‘06/03/2020’, ‘%m/%d/%y’)
returns zulu.parser.ParseError: Value “06/03/2020” does not match any format in [‘%m/%d/%y’].
- It also Avoids timezone/DST shifting bugs by possibly managing UTC datetimes while applying timedeltas or performing different calculations.
- It Supports Unicode date patterns for string parsing/formatting and strptime/strftime.
Some Important Links for this module:
• Project: https://github.com/dgilland/zulu
• Documentation: https://zulu.readthedocs.io/
• PyPI: https://pypi.python.org/pypi/zulu/
• TravisCI: https://travis-ci.org/dgilland/zulu
Thank you for reading this till the end.
Thanks a lot !!