The Problem with Time & Timezones - Computerphile

Computerphile · Intermediate ·📐 ML Fundamentals ·12y ago

Key Takeaways

The video discusses the complexities of time and timezones in coding, highlighting the challenges of creating a web app that calculates how many seconds ago something happened.

Full Transcript

sooner or later every programmer has to deal with time zones and I can't really offer much advice here I can offer a cautionary tale um I can tell you why you really should never ever deal with time zones if you can help it let's imagine that someone has has built an application that lets you calculate how many seconds something is in the past you type in a date and a time it gives you the number of seconds and they they look at that and think okay that that kind of works for me but let's let's add a little box that lets you change the time zone so if you're you know if you're comparing between now in New York and 5 days ago in London you can work that out and that's fine you know the little drop down lets you change which hour forward or backward of Greenwich you are brilliant sooner or later after it gets a bit popular they'll get a call from Australia and Australia will say good day I'm not going to try and do accents um austr I just shouldn't do accents um Australia will say hello um by the way we're 9 and 1 half hours ahead of Greenwich and the program will go really I like yeah yeah 9 and a half hours oh okay I'll add a special case for you that's fine then a little bit later someone will call from Napal and they'll say hello um we're 5 and A4 hours ahead of grenic and they'll say really I say yeah yeah we've been that for ages yeah 5 and a/ qu hours Great okay and they'll and put in a special case and maybe they'll look up the list of time zones the the canonical list that tells you what everything is they'll make sure they covered every time zone in the world and then Autumn will come along and we'll get a call from England and uh England will say excuse me um we're an hour out at the minute what's going on and the work hold on the clock's just changed that's fine no no we dealt with that we dealt with we we made a note of when Daylight Saving changes for us and we've put that in and and England will say no see Daylight Saving changes a week earlier for us it's different depending on where you live we we shift our clocks back a week before you do and and at that point that point generally the program will start to hold their head in their hands and realize what they've got themselves into and that's fine you know they they'll put that in and they will deal with each country shifting to Daylight Savings Time on a different day and they'll look at the file that tells them how to do that and they'll copy all that in and then they'll get a call from someone in the southern h atere again who will say yeah we're not shifting back in the Autumn we shift forward our our spring is in November and and that point they'll generally start looking longingly at their intoxicant of choice and wondering whether they should have a quick drink before keeping going and and then they'll they'll code that in as well and then they'll get a call from Samoa out in the Pacific on the international dat line and Sor will say hello um yeah we we skipped today the other year and the program will say what say yeah we skipped today we went from December the 29th 2011 to December the 31st we we shifted from one side of the International Date Line from being hours and hours behind grenic to being hours and hours ahead of Greenwich helps us with trading with Australia can can you take account of that when you work out how many days things are and how many seconds things are in the past it's fine there's a there's a file that tells you when any country has changed its time zone and it turns out that that happens fairly often but they they're announced ahead of schedule so as long as you keep that file updated and code that into your program's logic as well it'll be fine then you look back and you notice that during World War II England had double British summertime it went completely onto BST and then just added an extra hour so it was 2 hours ahead of Greenwich despite having Greenwich that's fine you deal with that it's changing if you notice I'm starting talking as if as if it was you or me because I've done this before and it's really really frustrating and you make sure that you subscribe to the list of when countries are going to change their time zones which happens apparently many times like sometimes several times in a year because governments change over and then then the program this this mythical programmer gets a call from Libya who in 2013 with only a couple of days notice decided that they weren't going to put the clocks back with enough notice said it wasn't possible for anyone to get the update out in time and meaning that every Libyan computer no matter what operating system it ran was an hour out if it's okay you you read the news article about that and you hurriedly code that in as well and then then you get a call from the West Bank where the Israeli population is on a different time zone to the Palestinian population because one is following Israel and one isn't and now you have two populations of people in the same location who are following different time zones and now they're all having to ask themselves whether they're on this time zone or or this one depending on who they are and where they are and there's no way to code that into your program and then then you get a call from the historian who says right I'm trying to calculate some some times back in the 18th century and we changed from the Julian calendar to the to the Gregorian calendar and it's not that we lost about three weeks it's just that we skipped right from this date to this date and and missed the others and can you code it so that so that it just kind of works that out for me and it's fine because someone else has already told you when those dates are and and you can code that into your program's logic as well but now it's looking really long and really complicated it's Tangled mess of spaghetti code that somehow Works down and then you get a call from the Russian historian who says yeah we only changed the Gregorian calendar in the 20th century and it turns out the dates that you skip change depend on depending on your location and and can you deal with that as well then you get a call from the British historian who says that until I think it was the 16th century the year started on the 25th of March just to blow your mind there on the 24th of March 924 and then it will be the 25th of March 92 5 and that is the next day because you have gone from December 31st 924 to January the 1st 924 because it goes in that order and it's massively complicated and then you get the call from the astrophysicist Who Says by the way we just had a leap second and at this point you just kind of go what leap seconds because the Earth does not rotate at a constant speed it slows down it speeds up as as tectonic plates move about and and magnetic fields shift or something like that and so occasionally the international astronomical Union will work out whether we need a leap second and if you do the clocks go 23 59 58 and then it's 23 59 59 and then instead instead of going like any sensible time zone wood it goes 23 59 60 and everything breaks because suddenly you have 61 seconds in a minute so depending on your implementation either your clock gets 1 second out of sync with the rest of the world or it repeats a second the way you're meant to deal with this is something called the Unix time stamp a number file I think has talked about this before that that you have this number that started at the first exact second of 1970 and increments 1 second per second constantly tick tick tick and that's great because what you're meant to do is you take whatever whatever date has been given you and you calculate that as a Unix time stamp and you put that into your database and and that'll just deal with leap seconds except it doesn't of course it doesn't because because you have Universal Coordinated Time which which includes leap seconds and that it repeats occasionally and it just includes 2359 60 and then you have astronomical time which does not include leap seconds and has steadily been getting out of sink with the rest of the world because we need to look at the stars and design telescopes around it and what you learn what you learn after after dealing with time zones is that what you do is you put away your code you don't try and write anything to deal with this you look at the people who have been there before you you look at the first people the people who have dealt with this before the people who have built the spaghetti code and you go to them and you thank them very much for from making it open source and you give them credit and you take what they have made and you put it in your program and you never ever look at it again because that way lies Madness Google actually has a really really good approach to LEAP seconds that they invented themselves there's an article about it on on their blog I think that that explains it and they do something called a leap smear um because having 61 seconds in a minute or because having a clock tick back a second uh can be really really bad for for massive agencies that sort of have to synchronize everything really precisely and have to trust uh that one bit of data was stored before another they essentially smear the second out over the whole day they increase their clock by a microsc at a time tick tick tick tick tick all the way through the day so that it's sometimes maybe even half a second out from reality but as long as everything on their servers is half a second it's built to be out of sync with the world um as long as it knows that one thing happened before another like having continuity is more important than actually having accurate time

Original Description

A web app that works out how many seconds ago something happened. How hard can coding that be? Tom Scott explains how time twists and turns like a twisty-turny thing. It's not to be trifled with! A Universe of Triangles: http://www.youtube.com/watch?v=KdyvizaygyY LZ Compression in Text: http://www.youtube.com/watch?v=goOa3DGezUA Characters, Symbols and the Unicode Miracle: http://www.youtube.com/watch?v=MijmeoH9LT4 More from Tom Scott: http://www.youtube.com/user/enyay and https://twitter.com/tomscott http://www.facebook.com/computerphile https://twitter.com/computer_phile This video was filmed and edited by Sean Riley. Computerphile is a sister project to Brady Haran's Numberphile. See the full list of Brady's video projects at: http://bit.ly/bradychannels
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Computerphile · Computerphile · 40 of 60

1 Follow the Cookie Trail - Computerphile
Follow the Cookie Trail - Computerphile
Computerphile
2 EXTRA BITS - Follow the Cookie Trail - Computerphile
EXTRA BITS - Follow the Cookie Trail - Computerphile
Computerphile
3 Musical Floppy Drives - Computerphile
Musical Floppy Drives - Computerphile
Computerphile
4 The Hair Algorithm - Computerphile
The Hair Algorithm - Computerphile
Computerphile
5 Getting Sorted & Big O Notation - Computerphile
Getting Sorted & Big O Notation - Computerphile
Computerphile
6 Quick Sort - Computerphile
Quick Sort - Computerphile
Computerphile
7 Hyper History and Cyber War - Computerphile
Hyper History and Cyber War - Computerphile
Computerphile
8 Entropy in Compression - Computerphile
Entropy in Compression - Computerphile
Computerphile
9 Original Elite on the BBC B - Computerphile
Original Elite on the BBC B - Computerphile
Computerphile
10 IP Addresses and the Internet - Computerphile
IP Addresses and the Internet - Computerphile
Computerphile
11 A Career in Video Games - Computerphile
A Career in Video Games - Computerphile
Computerphile
12 Error Detection and Flipping the Bits - Computerphile
Error Detection and Flipping the Bits - Computerphile
Computerphile
13 Programming BASIC and Sorting - Computerphile
Programming BASIC and Sorting - Computerphile
Computerphile
14 Birthplace of the World Wide Web - Computerphile
Birthplace of the World Wide Web - Computerphile
Computerphile
15 Punch Card Programming - Computerphile
Punch Card Programming - Computerphile
Computerphile
16 Programming Paradigms - Computerphile
Programming Paradigms - Computerphile
Computerphile
17 CERN Computing Centre (and mouse farm) - Computerphile
CERN Computing Centre (and mouse farm) - Computerphile
Computerphile
18 Error Correction - Computerphile
Error Correction - Computerphile
Computerphile
19 Home-Made Code - Computerphile
Home-Made Code - Computerphile
Computerphile
20 Security of Data on Disk - Computerphile
Security of Data on Disk - Computerphile
Computerphile
21 Gesture Controls - Computerphile
Gesture Controls - Computerphile
Computerphile
22 How Intelligent is Artificial Intelligence? - Computerphile
How Intelligent is Artificial Intelligence? - Computerphile
Computerphile
23 Encryption and Security Agencies - Computerphile
Encryption and Security Agencies - Computerphile
Computerphile
24 Virtual Machines Power the Cloud - Computerphile
Virtual Machines Power the Cloud - Computerphile
Computerphile
25 Hacking Websites with SQL Injection - Computerphile
Hacking Websites with SQL Injection - Computerphile
Computerphile
26 How Huffman Trees Work - Computerphile
How Huffman Trees Work - Computerphile
Computerphile
27 Cracking Websites with Cross Site Scripting - Computerphile
Cracking Websites with Cross Site Scripting - Computerphile
Computerphile
28 Cloud Computing (Cloudy with a Chance of Pizza) - Computerphile
Cloud Computing (Cloudy with a Chance of Pizza) - Computerphile
Computerphile
29 Texting Cabbage with a Recorder - Computerphile
Texting Cabbage with a Recorder - Computerphile
Computerphile
30 Hashing Algorithms and Security - Computerphile
Hashing Algorithms and Security - Computerphile
Computerphile
31 How YouTube Works - Computerphile
How YouTube Works - Computerphile
Computerphile
32 How NOT to Store Passwords! - Computerphile
How NOT to Store Passwords! - Computerphile
Computerphile
33 A New Golden Age of Video Games - Computerphile
A New Golden Age of Video Games - Computerphile
Computerphile
34 A Universe of Triangles - Computerphile
A Universe of Triangles - Computerphile
Computerphile
35 Cross Site Request Forgery - Computerphile
Cross Site Request Forgery - Computerphile
Computerphile
36 The True Power of the Matrix (Transformations in Graphics) - Computerphile
The True Power of the Matrix (Transformations in Graphics) - Computerphile
Computerphile
37 The Great 202 Jailbreak - Computerphile
The Great 202 Jailbreak - Computerphile
Computerphile
38 EXTRA BITS - Printing and Typesetting History - Computerphile
EXTRA BITS - Printing and Typesetting History - Computerphile
Computerphile
39 Triangles to Pixels - Computerphile
Triangles to Pixels - Computerphile
Computerphile
The Problem with Time & Timezones - Computerphile
The Problem with Time & Timezones - Computerphile
Computerphile
41 The Visibility Problem - Computerphile
The Visibility Problem - Computerphile
Computerphile
42 Lights and Shadows in Graphics - Computerphile
Lights and Shadows in Graphics - Computerphile
Computerphile
43 The Penguin Barcode - Computerphile
The Penguin Barcode - Computerphile
Computerphile
44 Typesetters in the '80s - Computerphile
Typesetters in the '80s - Computerphile
Computerphile
45 The Font Magicians - Computerphile
The Font Magicians - Computerphile
Computerphile
46 The Little Mac with the Big Bite - Computerphile
The Little Mac with the Big Bite - Computerphile
Computerphile
47 EXTRA BITS - More on the Original Mac at 30 - Computerphile
EXTRA BITS - More on the Original Mac at 30 - Computerphile
Computerphile
48 XP to Ubuntu with an 8yr old Hacktop - Computerphile
XP to Ubuntu with an 8yr old Hacktop - Computerphile
Computerphile
49 EXTRA BITS - Hacktop Real-Time Boot Comparison - Computerphile
EXTRA BITS - Hacktop Real-Time Boot Comparison - Computerphile
Computerphile
50 EXTRA BITS - Making a Bootable USB in Linux - Computerphile
EXTRA BITS - Making a Bootable USB in Linux - Computerphile
Computerphile
51 EXTRA BITS - Installing Ubuntu Permanently - Computerphile
EXTRA BITS - Installing Ubuntu Permanently - Computerphile
Computerphile
52 The Dawn of Desktop Publishing - Computerphile
The Dawn of Desktop Publishing - Computerphile
Computerphile
53 What is Bootstrapping? - Computerphile
What is Bootstrapping? - Computerphile
Computerphile
54 Reverse Polish Notation and The Stack - Computerphile
Reverse Polish Notation and The Stack - Computerphile
Computerphile
55 Home-Made Z80 Retro Computer - Computerphile
Home-Made Z80 Retro Computer - Computerphile
Computerphile
56 Should Everybody Learn to Code? - Computerphile
Should Everybody Learn to Code? - Computerphile
Computerphile
57 Programming in PostScript - Computerphile
Programming in PostScript - Computerphile
Computerphile
58 Heartbleed, Running the Code - Computerphile
Heartbleed, Running the Code - Computerphile
Computerphile
59 YouTube's Secret Algorithm - Computerphile
YouTube's Secret Algorithm - Computerphile
Computerphile
60 YouTube Search & Discovery - Computerphile
YouTube Search & Discovery - Computerphile
Computerphile

The video explains the complexities of time and timezones in coding and how to handle them when creating a web app. It highlights the challenges of calculating time intervals and displaying time-related data. Viewers will learn how to approach time and date processing in their coding projects.

Key Takeaways
  1. Determine the timezone of the user
  2. Calculate the time interval between two dates
  3. Handle daylight saving time (DST) differences
  4. Display time-related data in a user-friendly format
💡 Time and timezones can be complex and nuanced, and handling them correctly is crucial for creating accurate and user-friendly web apps.

Related Reads

📰
Deep Dive: Why Post-LayerNorm Crashes Big Models (And the Bare-Metal Math of Pre-LN Identity Highways)
Learn why post-layer normalization causes crashes in big models and understand the math behind pre-LN identity highways
Reddit r/deeplearning
📰
The Model Context Protocol in Python
Learn to implement the Model Context Protocol in Python and understand its use cases
Dev.to · Puneet Gupta
📰
Experiment tracking is a dashboard problem. Until it isn't.
Automate experiment tracking by integrating Claude or Cursor with Comet ML for real-time metrics and parameter inspection
Dev.to · Renato Marinho
📰
We Gave Our Engineering Team a Memory — Here’s How PRECOG Uses Cognee
Learn how PRECOG uses Cognee to build predictive engineering intelligence, enhancing their engineering team's capabilities
Medium · Machine Learning
Up next
Reinforcement Learning : Agent, Environment, Action, Reward, Policy Simply Explained
codehubgenius
Watch →