Home / Blog / Data Science / Python Corner

Python Corner

  • July 10, 2024
  • 5790
  • 51
Author Images

Meet the Author : Mr. Bharani Kumar

Bharani Kumar Depuru is a well known IT personality from Hyderabad. He is the Founder and Director of Innodatatics Pvt Ltd and 360DigiTMG. Bharani Kumar is an IIT and ISB alumni with more than 17 years of experience, he held prominent positions in the IT elites like HSBC, ITC Infotech, Infosys, and Deloitte. He is a prevalent IT consultant specializing in Industrial Revolution 4.0 implementation, Data Analytics practice setup, Artificial Intelligence, Big Data Analytics, Industrial IoT, Business Intelligence and Business Management. Bharani Kumar is also the chief trainer at 360DigiTMG with more than Ten years of experience and has been making the IT transition journey easy for his students. 360DigiTMG is at the forefront of delivering quality education, thereby bridging the gap between academia and industry.

Read More >

Let's look at the new Python functionality for the "date-time" library. Prior to Python 3.9, the date-time library in prior versions of the language did not provide enough time zone support; however, in Python 3.9, this date-time library has been enhanced and now produces better results.

The "date-time" module found in the previous versions of Python's standard libraries provides significant support for working with dates and times. However, prior iterations of Python lacked a little in terms of functionality for interacting with different time zones. As of right now, using a third-party library named "dateutil" is the preferred method for interacting with time zones

The fact that we have to create time zone rules on our own has proven to be the main obstacle to dealing with time zones in plain Python. Setting time zones is supported in a date-time, although only UTC is provided right now.

Accessing the time zones:

We will use the UTC timestamp from date-time library as shown in the below python code snippet.

Python Corner

In the above code snippet, we have got the time stamp in the time zone aware. It has an attached time zone as specified by “tzinfo”. The timestamps without any time zone information provided are called “naïve”.

Paul Ganssle is the maintainer of “dateutil” for many years. He joined the Python core developers in the year 2019 and helped to add a new “zoneinfo” standard library which makes working with time zones much more convenient way.

“zoneinfo” will provide the access to the Internet Assigned Numbers Authority (IANA) Time Zone Database. The IANA updates its database several times each year, and it’s the most authoritative source for time zone information.

Using “zoneinfo”, we can get the object describing any time zone which is available in the database and a sample code snippet for the same is shown below:

Python Corner

We can access the time zone using one of the several keys. In the above case, we have used “America/Vancouver”.

THE “zoneinfo” library uses an IANA time zone database residing on our local computer. It is possible on Windows in particular that we don’t have any such database or that “zoneinfo” won’t be able to locate it. If we get an error as shown in the following python code snippet, then “zoneinfo” hasn’t been able to locate a time zone database.

Python Corner

The above issue has been resolved in the newer versions of python and it has been introduced in python 3.9, to resolve the above error in Python we should download a library that is available on PyPI as “data” which can implement the IANA Time Zone Database. We can install the same by giving the command “pip install data”.

We can make time zone aware timestamps using the “tz” or “info” arguments to datetime functions as shown in the following python code snippet:

  `

Having the time zone recorded with the timestamp is great for keeping the records that can be used for future use. It also makes it convenient to convert between time zones as shown in the following python code snippet:

Python Corner

In the above python code snippet, we can see that the time in Oslo is nine hours later than in Vancouver.

Investing Time Zone:

There are many different time zones maintained in the IANA Time Zone Database, which is extremely large. The Python method "zoneinfo.available_timezones()" may be used to list all of the time zones that are currently in use. A snippet of code for this function is displayed in the image below.

Python Corner

With your installation of the package on our local workstations, the number of time zones in the database may change. We can observe that there are 609 time zone names provided in the aforementioned example. We can examine each of these time zones in more detail since they individually record historical changes that have taken place.

Kiritimati, often known as Christmas Island, is now located in UTC+14, the world's furthest western time zone. That wasn't always the case, though. The island was in UTC-10 before 1995 and was located on the opposite side of the International Date Line. Kiritimati totally bypassed December 31, 1994, to cross the dateline.

By taking a deeper look at the "Pacific/Kiritimati" time zone object in the following python code example, we may determine how this occurred:

Python Corner

On December 30, 1994, on Kiritimati, the new year began one hour after the time displayed at 23:00. According to the example given above, December 31, 1994 never happened.

The following line of Python code demonstrates how the offset from UTC has been altered:

Python Corner

". UTC offset ()" in the Python code sample above will produce a time delta. The best technique to determine how many hours a particular time delta represents is to divide it by a time delta that represents one hour.

Using Best Practices of the time zones:

Working with time zones can be a little tricky. However, with the availability of the “zoneinfo” standard library with python, it’s gotten a bit easier. The following are a few suggestions to keep in mind when working with dates and times:

Civil times: these are like the time of a meeting, a train departure, or a concert, and are best stored in their native time zone. We can often do this by storing a naive time stamp together with the IANA key of the time zone. One of the best examples of a civil time stored as a string would be "2020-10-05T14:00:00, Europe/Oslo". Having information about the time zone ensures that we can always recover the information, even if the time zones themselves change.

Timestamps: these will represent specific moments in time and typically record an order of events. Computer logs are the best example of this. We don’t want our logs to be jumbled up just because our time zone changes from Daylight Saving Time to standard time. Usually, we would store these kinds of timestamps as naive date times in UTC.

We should be aware of maintaining our local time zone database in sync because the IANA time zone database is constantly updated. If we are running any apps that are time zone-sensitive, this is especially crucial.

We can often rely on our system to update the local database on Mac and Linux. If we rely on the "data" package, we must keep in mind to update it sometimes. We shouldn't, in particular, keep it permanently fixed to one version.

We have clear access to a certain time zone thanks to names like "America/Vancouver". However, it's preferable to utilise conventional time zone designations when informing our consumers of date times that are time zone-aware. The following bit of Python code demonstrates how to get them using the function ".tzname()" on a time zone object:

For ".tzname()" to provide the desired result, a timestamp is necessary. As can be seen in the following sample of Python code, the name of the time zone may vary at various points in time, such as during Daylight Saving Time:

Python Corner

Given that Vancouver is in Pacific Standard Time (PST) during the winter and Pacific Daylight Time (PDT) during the summer in the python code snippet above, the timestamp is during the winter season.

Python Corner

Additionally included in Python 3.9 and subsequent versions is the "zoneinfo" package. However, if we are still using an older version of Python, we may still benefit from "zoneinfo" by installing a backport that is offered by PyPI and can be found by typing "python -m pip install backports.zoneinfo".

Data Science Placement Success Story

Data Science Training Institutes in Other Locations

Navigate to Address

360DigiTMG - Data Science, Data Scientist Course Training in Bangalore

No 23, 2nd Floor, 9th Main Rd, 22nd Cross Rd, 7th Sector, HSR Layout, Bengaluru, Karnataka 560102

1800-212-654-321

Get Direction: Data Science Course

Read
Success Stories
Make an Enquiry