Working with time zones in Python

tl;dr      DON’T WORK WITH TIME ZONES!!  

That is the general advice I would like to give you before reading this post. But, unfortunately, as much as you want to avoid it,  working with time zones is inevitable and at some point in your project, you will face the requirement to provide timezone support.

Recently, I worked on a Python project and one of the requirements was to allow users to select the timezone in which they want to view daily reports. This seemingly innocuous task soon turned into a nightmare. There are a zillion time zones plus daylight saving time to take care of plus different time formats and, then, there is Internet Explorer. This post attempts to list down some tips, tricks and quirks while dealing with time zones.

  1. Store data in the database in UTC : While storing any timezone related information, make sure that you store the data in UTC. This makes your application flexible in dealing with change in time zones as well as daylight saving time. The conversions to and from the user specified timezone are done at the application level and not at the database level.
  2. API endpoints should remain in UTCIf you are exposing an API, do not deal with the timezone requirements of the consumer of the API. All time related entities should be served in UTC. This prevents dealing with timezone changes as well as  complicated daylight saving issues.
  3. Provide date related information in the same format all through the application This should be a thumb rule in your application. All services and API’s should provide date related information in the same format. For eg. if you decide to use the format dd:MM:yy hh:mm:ss’,  try sticking to this format right through. Different formats lead to a lot of confusion and difficulty in generalizing solutions.
  4. Keep your local machine in the timezone of your server for local testing : Most of the cloud servers have their timezone set to UTC. When any JavaScript timezone utility is used in your code, the server timezone is considered. For this reason, make sure that your local machine timezone is set to your server’s timezone while testing locally. To change your local timezone on a Linux machine, type this command at the terminal
    sudo dpkg-reconfigure tzdata

Python timezone libraries

pytz is the best library to deal with time zones. It allows accurate and cross-platform timezone calculations using Python 2.4 or higher. It also solves complex daylight saving issues. This library is used to get the list of all time zones available, convert datetimes from one timezone to another plus many other timezone related utilities.

datetime is another module which is part of the standard Python library. It supplies methods for manipulating dates and times. It is primarily used to perform date and time arithmetic.

In JavaScript, moment.js, is the best library to parse, manipulate and display time zones.

Unit Testing

Writing unit tests is very important, as always, and especially when dealing with time zones. It is manually difficult to test your code with all the different time zones and permutations and combinations and hence, it is necessary that you have sufficient test coverage. To help you write tests, freezegun is a library that mocks the datetime module. All calls to datetime.datetime.now(), datetime.datetime.utcnow(), datetime.date.today(), time.time(), time.localtime(), time.gmtime(), and time.strftime() will return the time that has been frozen.

And just when you think you are done….

 

ie

Internet Explorer has its quirks and mood swings. As compared to the other browsers, IE does not support certain formats ( eg. it does not support dashes in the time format, needs slashes ) and may have other exceptions from the norm. Be sure, to test your application on IE well, especially when time zones and dates are concerned.

In the end, these were just few tips based on my experience. Do comment if you have had any learning while working with time zones!

Have a nice day or night, based on your timezone!

P.S : UTC is now the worldwide standard for regulating clocks and time measurement.  All other time zones are defined relative to UTC, and include offsets like UTC+0530 – hours to add or subtract from UTC to derive the local time.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: