Skip to main content

A date with Date, Time, Duration and Timezones


Have you ever wondered, when debugging an issue in a globally distributed environment, if the events are out of sequence? Chances are that all of us would have seen it at least once in our professional career.
All applications are required to deal with Date, Time and Duration. This is a very complex area, as the rules regarding time-keeping are not widely known and this causes tricky and funny (and sometimes critical ) issues. Adding to this is the additional complexity of Time zones (TZ) and the Daylight Savings! Phew!
Let’s refresh our understanding of how Time zones are arrived at.
Since a day is 24 hours long, the world may be split into 15 degree wide longitudinal slices (360 degrees/24 hours). Each slice represents one hour. But, life is not so simple. The International Date line creates an additional TZ and a few TZ’s differ by 30 mins /45 mins.
If Greenwich (0th meridian) has the current local time as 00:00 hrs, then Brussels is approximately one hour ahead of GMT and Praira (capital of Republic of Cabo Verde) is one hour behind. But GMT itself is a local TZ and quite a few countries use this as their TZ, then what is the standard?
Enter UTC or the Universal Coordinated Time. The worlds timing centres, around 70+ of them, maintain the atomic clock and keep it synchronized (coordinated) and hence the name UTC. (https://www.bipm.org/utils/en/pdf/time_ann_rep/Time_annual_report_2014/11_Table3_TAR14.pdf). UTC is based on a 24-hour clock and hence is always represented as such with a special designator Z (standing for Zulu)
An Example is 2019-07-19T04:53:30Z

When representing local times, the strings +hh:mm, +hhmm, or +hh or -hh:mm, -hhmm, or -hh can be suffixed to the time to indicate that the used local time zone is hh hours and mm minutes ahead/behind of UTC respectively.
Now since we have enough background into the Time, let's delve into the various scenarios that we may encounter
1.     Applications are deployed in different TZ and the servers are configured with local TZ. The date/time data captured is of that TZ where its deployed
2.     Applications are deployed in different TZ, but the database is a central one and there is a column in a table that takes the current system time of the operating system on which the database resides, and if that is a local time, then its captured
3.     Applications are accessed in different TZ and hence the capturing of input from users’ on client TZ
4.     Say we are transferring data or backing up and restoring in different TZ’s
As we can see, there could be various permutations and combinations that could result in the value of what we capture not being correct or correct in the context of capturing but incorrect in the context of analysis/usage/transferring.
What can we do about this? – a few basic principles to our rescue

1.     Always store the time in UTC from the application. You can capture in any local TZ but please convert and store it in UTC.
2.     If you are using a DB function to capture time, use the appropriate date/time function that provides time in UTC UTC irrespective of what the DB and the host OS TZ is
3.     Have your presentation layer translate the time in UTC  to the appropriate time for the user to view. if possible, provide an option for the user to choose the TZ in the profile section
4.     Use ISO 8601 standard
5.     Don’t write your own Date/Time parsing libraries. Use standardly available ones
6.     For existing applications, add the offset to UTC. This will help in later normalisation

Well, Things are not so simple. What if you want to calculate duration, say in seconds as your application is sensitive to seconds? You would normally find the difference between two dates. But alas! There is a something called Leap Seconds. It's an extra second added either on June 30th or December 31st based on the difference between the earth's rotation and the atomic clock. Sometimes, due to the earth slowing down, there is a difference and hence a second has to be added, and it has happened 27 times from 1972 to date.
Most of us wouldn’t need to worry about leap seconds, but good to be aware of it

To summarise, knowledge is power and awareness is the first step to change. Don't despair. Keep things simple and in UTC and you shall have enough time on hand to pursue your interests

(This was originally published in Lowe's India internal blog)

Comments

Popular posts from this blog

I'm Back This is my post after a gap of about a month. Been busy with fixes and IR's :(.. Unavoidabble, since this is the job that provides for my bread and butter[ rather iddly and sambar]... Of late, I've been affected by the conflict and death tolls in Lebanon...A couple of questions have been plaguging me for some time since this recent fight erupted.. 1. why should Lebanon harbour terrorists..? 2. why should Isreal continue strikes that cause lot of collateral damages..? 3. if this is acceptable to the world, then why don't we, follow suit and eliminate terrorists?? Pls let me know your opinions... Till then.. Oh! God....let the world be a safer place than what it is today...
The Rays.. Its been a long time since the rays touched the blogosphere..been occupied with some production issues and have been unable to post anything.. some happenings 1. Bombay Blasts(2?) 2. Agni 3. zizou Let us all spend a minute in prayer for the victims of the bombay blasts. Thinking abt it.. is it enough that we all condemn the blasts, give a warning to pakistan and we all carry on with our lives..What abt the victims themselves?? They are scarred for life.. It is time that the Indian Government emulates other contries in curbing terrisom.. Let us show the world that we are not cowards and we are not barking dogs.. we BITE... Failur of Agni It was one of the most important phases in the history of Indian Space Research Org.. But its allright if we fail.. The important point is that we should have learnt something from this failure. Hope the data collected is analyzed and is helpful in the next launch. well zizou.. what can we say..?? Not a fairy tale ending.. but never-the-les
The First Rays After being in the state of RAC (Reservation against cancellation.. for the uninitated to the jargons of Lalooways ..oops!! railways..), now I have a confirmed ticket to the blogosphere, thanks to my friend Hameed, who finally got things set up for me. I will be rambling anything and everything under the sun (so the name!!) that interests/affects me... So folks.. lets get ready to feel the warmth of the suriyan..