# The beginning of the story

I was working on a project that has to deal with date & time within different timezones. The target system is written in Python. So I just googled it and spent a whole night to learn the techniques and theories of timezone.

I was so surprised that the theory of timezone is so complicated and, yet, very interesting. I have the naive concept that all of the complexity of the current status of the timezone over the globe is a miniaturized picture of human history. We created all the trouble and we are indeed the troublemaker! :)

Let me summarize what I’ve learned and the important points.

Most of the time zones on land are offset from the Coordinated Universal Time (UTC) by a whole number of hours (UTC−12 to UTC+14). ( Refer to Time zone)

In fact, there is not a simple table that lists all the timezones and there offsets from UTC. You may be looking at some fancy tables like this one “List of tz database time zones”. But you may be disappointed that things just work that way. There are mainly two reasons.

First, there exists such a magical rule called DST, Daylight Saving Time. Lots of the western people may be familiar with the concept of DST. But it is hard for me, cause I’m an eastern guy. You have to specify a specific time and date in a year to fully decide what the offset from the UTC should be for a given timezone.

Second, things with DST get worse when you looking back in time. The rules of DST changes frequently in some areas of the land of Planet Earth. So in addition to the specific day ant time, you have to be very specific about the actual year you are interested in when you would like to deal with the DST rules.

The details of timezone are maintained by Arthur David Olson of IANA (Internet Assigned Numbers Authority) and the product is the tz database.

As for python, there is a package called “pytz” that helps you to deal with issues with timezones. pytz brings the Olson tz database into Python. I think pytz is shipped with python 2.7 and python3.5 by default.

# The datetime package of python

Time flew when I was playing with pytz. Then I thought that since I had spent so much time on the timezone issue why shouldn’t I continue to learn the data & time functionalities provided by python as well? So I did it.

After studying the content of the associated documentation of python, I obtained a relatively clearer picture of the techniques we could adopt regarding date & time manipulation with python. Python has a datetime package which is really handy when you want to deal with date and time.

# The notes

I composed a short python script (Yeah, I know, a lot of print() calls) to demonstrate the use of the datatime and pytz packages. This script serves as my learning notes. The functionalities that I used are those I suppose to be important and useful for everyday practice.

The following is the output of the simple script.

================== Beginning of the output. ================

====================== End of the output. ==================

# Acknowledgment

The cover image is copied from the Wikipedia page of the tz database

# The source code

The source code is also available at my GitHub Repo.