In December 2014 a music video did something nobody at YouTube had planned for. Gangnam Style, by the South Korean singer Psy, passed 2,147,483,647 views, the biggest number YouTube’s view counter had been built to hold. YouTube wrote: “We never thought a video would be watched in numbers greater than a 32-bit integer.” The counter didn’t break in the end, because Google’s engineers had seen the number coming months before and given the counter more bits. But it would have.
That counter was not the only one. The clock in a computer is a counter too: it counts seconds. And many computers still keep that count in the same 32 bits. Here is one, ten seconds before it runs out.
The count reaches 2,147,483,647 at 8:59:07 one morning in Nepal. What will the clock show one second later?
Pick an answer to see why.
You need: binary numbers (Grade 6). The blocks use variables (Grade 7); the Python parts need loops and functions, from Grade 8. If you haven’t read What Is the Biggest Number a Computer Can Count To?, it helps: it explains why a counter rolls over.
Try it
Use the clock above.
- a.Press Today. How big is the count now? Is the left bit on or off?
- b.Press Near the end, then Play, and watch the last ten seconds. What does the count become?
- c.Keep it playing for a few seconds after that. Does the clock go on counting? Which way does the date move?
How it works
A clock is a counter
A computer doesn’t keep the date as “19 January 2038”. It keeps one number: how many seconds have passed since midnight on 1 January 1970, by world time (UTC, the time in London without summer time). Nepal’s clocks are now 5 hours and 45 minutes ahead of UTC. Every second, the number goes up by 1, and whenever a date is needed, the computer works it out from the number. This way of keeping time is called Unix time, after an early operating system that used it, and it is still how a great many computers, phones and websites count.
For many years the count was kept in a signed 32-bit number. As you met in Grade 9 Grade 9: Two’s complement, the left bit of a signed number is worth minus its place value, so 32 bits hold −2,147,483,648 to 2,147,483,647. Half the patterns are for dates before 1970, half for dates after. That let programs store a birthday in 1950, but it cut the clock’s future in half.
Why did the clock give half of its 32 bits’ patterns to dates before 1970?
Pick an answer to see why.
Not only seconds: the GPS week
Satellite navigation, GPS, sends its own date down from space. Its clock started at midnight on 6 January 1980, and the satellites’ old message gives the week as a 10-bit number. Here is that week counter, a few weeks before its end.
Ten bits hold 0 to 1,023, so after 1,024 weeks, a little under 20 years, the week number goes back to 0. A receiver that isn’t ready for it thinks it is 1980 again, or some other wrong date. It has happened twice already, and your program will find when.
A counter in decimal: the year 2000
The oldest of these stories isn’t in binary at all. In the 1960s and 1970s computer memory cost a great deal, so many programs kept a year as two digits: 75 for 1975. Two digits are a meter with two wheels, 00 to 99, and they roll over just as bits do: after 99 comes 00. Nobody expected those programs to still be running when 1999 became 2000, but many were, in banks, airlines, power stations and governments. The worry was called Y2K (Y for year, 2K for 2,000). Grade 10 tells it as a design choice that lasted Grade 10: A design choice: how many digits for a year?; here you’ll program what went wrong.
Program it
With blocks: the year 2000
A program works out a person’s age from two-digit years, the way many old programs did.
In 1999 the person is 24, as they should be. In 2000 the program says −75: the year wheel went from 99 to 00, and the program thinks the person will be born 75 years from now. A bank or a hospital working out ages would get them all wrong in the same way.
Some web pages had a different version of the same mistake. Their programming languages gave the year as the year − 1900 (99 for 1999, and 100 for 2000), and the page simply wrote “19” in front of it.
For 1999 it says 1999. For 2000 it says 19100, and on 1 January 2000 some real websites did, including a weather service’s maps and an official time service’s page.
In Python (from Grade 8): when does the clock run out?
Python has a module, datetime, that does the calendar for you: a datetime is a date and a time, and adding a timedelta (a length of time, such as so many seconds) to it gives a new date. So the computer’s clock is one line: the start, plus the count in seconds.
from datetime import datetime, timedelta start = datetime(1970, 1, 1)nepal = timedelta(hours=5, minutes=45) last = start + timedelta(seconds=2 ** 31 - 1)after = start + timedelta(seconds=-2 ** 31)print('Last second, world time:', last)print('Last second, in Nepal: ', last + nepal)print('One second later: ', after)Output
Last second, world time: 2038-01-19 03:14:07Last second, in Nepal: 2038-01-19 08:59:07One second later: 1901-12-13 20:45:52There it is, to the second: 19 January 2038, 3:14:07 in the morning, world time, 8:59:07 in Nepal, which will be early in Magh 2094 BS. One second later a clock that trusts its 32 bits says 13 December 1901.
When does each counter run out?
Every counter here is a start, a number of patterns, and the time one count stands for (a second, a week). So one function answers for all of them: the counter rolls over at the start plus patterns × one count.
from datetime import datetime, timedelta def rolls_over(start, patterns, tick): # tick is how many seconds one count stands for return start + timedelta(seconds=patterns * tick) clock = datetime(1970, 1, 1)gps = datetime(1980, 1, 6)week = 7 * 24 * 60 * 60 print('Seconds, signed 32 bits:', rolls_over(clock, 2 ** 31, 1))print('Seconds, 32 bits, no sign:', rolls_over(clock, 2 ** 32, 1))for lap in [1, 2, 3]: print(f'GPS weeks, 10 bits, lap {lap}:', rolls_over(gps, lap * 2 ** 10, week))Output
Seconds, signed 32 bits: 2038-01-19 03:14:08Seconds, 32 bits, no sign: 2106-02-07 06:28:16GPS weeks, 10 bits, lap 1: 1999-08-22 00:00:00GPS weeks, 10 bits, lap 2: 2019-04-07 00:00:00GPS weeks, 10 bits, lap 3: 2038-11-21 00:00:00(The signed clock rolls over at 3:14:08, the first second it can’t show; its last good second is 3:14:07.) The GPS week ran out on 22 August 1999 and again on 7 April 2019, by GPS’s own clock, which runs a few seconds ahead of world time, so in world time each was just before midnight the evening before. And its third lap ends in November 2038, the same year as the 32-bit clock.
The detective: what was the plane counting?
In 2015 the United States’ aviation authority sent an order to airlines flying the Boeing 787, a large passenger plane. In a laboratory test Boeing had found that a counter inside the plane’s generator control units, the computers that run its electric power, would overflow after the plane had been powered on for 248 days without a break. If that happened, all four main units could stop at the same moment, and the plane would lose all its main (AC) electric power, “regardless of flight phase”: even in the air. Until a fix was ready, airlines had to switch the plane’s power off and on again at least every 120 days.
The order doesn’t say what the counter counted. Your program can work it out. Try every number of bits, and every likely tick (seconds, tenths, hundredths, thousandths of a second), and print only the counters that last about 248 days.
ticks = [('seconds', 1), ('tenths', 10), ('hundredths', 100), ('thousandths', 1000)]day = 24 * 60 * 60 for bits in range(8, 65): for name, per_second in ticks: days = 2 ** bits / per_second / day if 240 < days < 260: print(f'{bits} bits counting {name}: {days:.1f} days')Output
31 bits counting hundredths: 248.6 daysOut of 228 possible counters, only one fits: 31 bits counting hundredths of a second. And 31 bits are what a signed 32-bit number has for counting up, once one bit is kept for the sign. So the plane’s computers very probably counted hundredths of a second since power-on in a signed 32-bit number, exactly like YouTube’s view counter and the 2038 clock. (That is our program’s conclusion, not something Boeing said; but the numbers leave little room for anything else.)
What your program shows
- The same 2,147,483,647, three times. A video’s views, a computer’s seconds since 1970, and a plane’s hundredths of a second since power-on: three different things, all counted in a signed 32-bit number, and all running out at the same count. What changes is only what one count stands for, so the end comes after 2.1 billion views, after 248 days, or after 68 years.
- The exact morning. Your program found that a 32-bit clock runs out at 8:59:07 a.m. in Nepal on 19 January 2038, and then believes it is 1901. GPS’s 10-bit week runs out for the third time in November of the same year.
- One counter caught from its size alone. From “248 days” your program worked out what the plane was counting. These bugs are not mysteries: once you know how many patterns a counter has, you can say to the second when it will run out, and so mend it before it does.
So the answer to the title: YouTube’s counter would have broken because 32 bits were chosen when nobody imagined two billion views, and it didn’t because someone did the sum in time.
Going further still
64 bits. Most computers and phones made in recent years already keep their clocks in 64 bits. How long does a signed 64-bit count of seconds last? Change the program above, or work it out:
print(f'{2 ** 63 // (365 * 24 * 60 * 60):,} years')Output
292,471,208,677 yearsAbout 292 billion years, more than 20 times the age of the universe. The danger is in small computers inside machines, routers and cars that still count in 32 bits: they are often left running for decades, so they must be found and changed before 2038.
The Bikram Sambat year. Suppose a program in Nepal kept the BS year as two digits, 82 for 2082. When would it go wrong, and which comes first in Nepal: that, or the 2038 clock?
Challenges
★ Mend the year 2000 (blocks)
Many Y2K fixes kept two digits and added a rule: if the answer comes out negative, the century must have turned, so add 100. Add that rule to the age program, so that 75 and 00 says Age: 25. Then press Test.
★★ What does the clock show? (Python, from Grade 8)
Write clock_shows(seconds): the date and time, as text, that a signed 32-bit clock shows after counting seconds seconds since midnight on 1 January 1970. Until the end it is just the date; after it, the count rolls over first. For 2,147,483,647 it gives '2038-01-19 03:14:07', and one second more gives '1901-12-13 20:45:52'.
from datetime import datetime, timedelta def clock_shows(seconds): return str(datetime(1970, 1, 1) + timedelta(seconds=seconds)) print(clock_shows(2 ** 31))★★★ Which year is it? (Python, from Grade 8)
Without datetime, write year_of(seconds): the year it is after seconds seconds (0 or more) since midnight on 1 January 1970. A year has 365 days, or 366 in a leap year: a year divisible by 4, except years divisible by 100, unless they are also divisible by 400 (so 2000 was a leap year and 2100 won’t be).
def year_of(seconds): return 1970 print(year_of(2 ** 31 - 1))Questions
A computer’s clock counts seconds since 1970 in a signed 32-bit number. Which is the last moment it can show?
Pick an answer to see why.
Why did many old programs keep the year as two digits, and what went wrong with them in 2000? Give one example.
Answer
A view counter, a clock and a plane’s power computer all used signed 32-bit counters. The view counter would have run out after 2.1 billion views, the clock after 68 years and the plane’s counter after 248 days. Explain how the same kind of counter can last such different times.
Answer
Some people said the year 2000 problem was a fuss about nothing, because so little went wrong. What would you say to them?
Answer
A company in Nepal is putting small computers in a hydropower plant to record the river’s flow. Their clocks count seconds since 1970 in a signed 32-bit number. The computers should last 20 years. What should the company be told, and what should it do?
Answer
Where to go next
- What is the biggest number a computer can count to? Why a counter rolls over, and Pac-Man’s level 256, broken by a counter of one byte.
- Grade 6, Counting in binary: place values and carries.
- Grade 9, Two’s complement: why the left bit of a signed number is a minus sign, and why the clock jumps to 1901.
- Grade 10, A design choice: how many digits for a year?: Y2K and 2038 as choices whose effects last for decades.
- Why can’t a formula convert Bikram Sambat to AD? In Going Further 9–12: what a computer needs to turn its count into a date in Nepal’s own calendar.
Found a mistake? Please tell us.
