Timezone and Python – The Problem with Time & Timezones

Tech News

Written by:

Reading Time: 6 minutes

Every programmer will deal with time zones once, and we can’t offer much advice.

We can provide a cautionary tale. We want to tell you why you really should never deal with time zones, but only if you can help.

Let’s imagine someone has built an application that lets you calculate something is in the past. After typing date and time, and it gives you the number of seconds, and you look at that and think “OK.”

That kind of works for us. But let’s add a little box that enables you to change the time zone. If you compare between now in New York and five days ago in London, you can work that out.

The little drop-down lets you change which hour forward or backward of Greenwich. Soon after it gets a bit popular, they’ll get a call from Australia, and they say, “Hello!”, we’re 9½ hours ahead of Greenwich. The programmer will go “really?”. OK, we’ll add a particular case for you.

But a little bit later, someone from Nepal calls and says, “Hello,” we’re 5¼ hours ahead of Greenwich, and you say, “really?”. Yeah, we’ve been that for ages! Yeah, 5¼ hours! Great. OK, and you put in a particular case, and maybe you’ll look up the list of time zones.

The Canonical list tells you what everything is, and you’ll make sure you’ve covered every time zone in the world. Then autumn will come along, and you’ll get a call from England and England will say, “Excuse me we’re an hour out at the minute, what’s going on?”

Hold on, and the clock’s just changed! That’s fine.

Now dealt with a note, when daylight savings changes for us, and we’ve put that in, and England will say, don’t do this, see daylight saving will change to a week earlier.

It’s different and depends on where someone lives. We can shift our clock’s back a week before, but at that point, the programmer will hold their head in their hands and realize what they’ve gotten.

Put that in, and deal with each country’s time-shifting. Daylight saving time is on a different day. You’ll look at the file that tells us how to do that, and copy it all.

Now you get a call from the southern hemisphere they say, we’re not shifting back in the autumn slightly we shift forward, and our spring is in November. We generally start looking longingly at their intoxicant of choice, and you’ll code that in as well.

Also Read:   How HR Technology is Improving and Streamlining the Recruitment Process

Now you get a call from Samoa. Out in the Pacific on the international dateline, and they say Hello! We skipped a day, another year. The programmer Said, “what?” We jumped a day!

Now you went from December 29th, 2019 to December 31st. You shifted from one side of the international date line from being hours and hours behind Greenwich to be hours and hours ahead of Greenwich. Can you take account of what you worked out and how many days, and seconds things are in the past?

OK, there’s a file that tells you when a country changed its time zone and turns out that what somewhat happens. But you’ve announced to schedule it as long as you keep that file updated, and code that into your program’s logic as well.

Have you noticed we’ve started talking as if it was you or us? Because we’ve done this before and it’s frustrating, and you make sure you subscribe to the list or when countries are going to change their time zones.

Now you look back, and you notice that during World War II, England had double British Summer Time, which went entirely onto BST. You just added an extra hour, so now it is two hours ahead of Greenwich.

Like, sometimes in a year, because governments change over and then this mythical programmer gets a call from Libya. Who is 2013 with only a couple of days’ notice decided that they weren’t going to put the clocks back?

With enough opinion, no one could get an update in time, which means that every Libyan computer’s operating system they ran, will be an hour out. You read a news article about that, and you hurriedly code that in as well.

Now, you get a call from the West Bank where the Israeli population is on a different time zone to the Palestinian people are in a different time zone.

You have two categories of people in one location who are pursuing different time zones. They have to ask themselves whether the time zone on which they depend on who they are and where they are. There’s no way to code that in the program.

Also Read:   Best cheap Chromebook deals July 2022

Now you get a call from the historian. They Said, we are trying to calculate time from back in the 18th century. We have changed our calendar from Julian to Gregorian. It’s not that we lost three weeks. Instead, it’s about we have skipped from this date to another date and missed others.

Can you code it so that it works that out for us? It’s beautiful because someone has already told you when those dates are, and you can code that into your program’s logic as well. Still, now it’s looking long and complicated, and it’s a tangled mess of spaghetti code that somehow works.

Now you get a call from Russia, Who says, we only changed to the Gregorian calendar in the 20th century. It turns out the dates you’ve skipped changing, which depends on your location, and can you deal with that as well?

Now you get a call from British historian, Who says, that until they think it was the 16th century, the year started on March 25th to blow your mind there. From March 24th, 924, and then it would be March 25th, 925, and very next day, you go from December 31st, 924 to January 1st, 924, because it is going in that order.

It isn’t very easy!

Now you get the call from the astrophysicist, Who says, we just had a leap second, at this point, you go like “what!” Leap second. This is because of the Earth rotation, which is not constant.

It slows down, speeds up, as the tectonic plates move, and magnetic field shifts. So occasionally, the International Astronomical Union can work out on the need for a leap second.

It goes 23:59:58, and then 23:59:59 and then instead of going [00:00:00] any sensible time zone would, it goes 23:59:60. Everything breaks suddenly because now you have 61 seconds in a minute.

It all depends on the implementation of the clock to get one second to get sync with the world, or it repeats a second. The way you meant to deal with this is with something called the Unix Timestamp and Numberphile.

Also Read:   These cheeky handmade mirrors are perfect for TikTok

Now you have the number that started on the first exact second of 1970 and incremented one second, per second. Constantly. Tick. Tick. Tick.

That’s great because what you meant to do is take whatever date has given to you. You have calculated that as a Unix Timestamp, and you put that into your database.

That’ll deal with leap seconds EXCEPT IT DOESN’T! Of course, Because you have Universal Coordinated Time, which includes leap seconds! It repeats occasionally, and it just contains 23:59:60.

Now you have astronomical time which does not include leap seconds and has steadily been getting out for sync with the world.

Now you need to look at the stars and design telescopes around it. What you have learned after dealing with the time zones is what you do. You put away the code and don’t try or write anything to deal with this.

The people who have coded the spaghetti, will you thank them for making it open-source. Give them credit, and take what they have made, put it in the program, and never look at it again.

Google has an excellent approach to leap seconds that they invented themselves. They do something called a “leap smear.”

They have 61 seconds in a minute because they have a clock tick back a second. It can be harmful to large agencies to synchronize everything precisely and have trusted one bit of data they have stored before another.

The essential smear second out the whole day. They can increase their clock by a microsecond at a time. Tick, tick, tick, through the day.

So that’s it, sometimes maybe even half a second out from reality. Still, as long as everything on their servers is half a second out. It is built to sync with the world, as long as it knows that one thing can happen before another. Having continuity is essential to have an accurate time.

Author’s Bio

Name: – Harsh Kumar Sharma

Designation: – Seo Executive

My Self Harsh Kumar Sharma serves as a Seo Executive in the leading Institute named Edunbox.com which provides Python for GIS Training, there I handle all works related to SEO, SMO, SMM, Content Writing and Email Marketing, etc.