The Restart of Time
Tags: coffeelessthoughts, todayilearned, devjournal, computerscience
Published: 2021-06-10 22:38:43

### Prerequisites:
- A basic knowledge of bits, bytes, and words

#### Refresher:
1. [Intro to Binary Numbers | CSAnim](https://csanim.com/tutorials/intro-binary-numbers-bitwise-operations-ultimate-visual-guide)
1. [Intro to Binary Integers | Aidi Rivera](https://dev.to/aidiri/intro-to-x-bit-and-binary-integers-39lj)

_____
Have you ever asked yourself how time is represented in a computer? How about whether we're on the verge of running out of bits to store said time?

If yes, fantastic. Let's get to the bottom of it. If not, I hope your interest is piqued. Keep reading and we can learn something together!

_____

## The Representation of Time

We'll start with the simpler question, the representation of time in bits.

In computer science, <span title="Reading on Unix epoch below in 'References'">we generally record & store time in milliseconds relative to January 1st, 1970</span>.

Knowing this, you may find yourself wondering:
> Well there are 1000 ms in EVERY second and that has to add up fast. We're already 50 years past the epoch and that's a lot of milliseconds! Won't we run out of bits soon, say in a few decades?

Good news is that we've little to worry about and even better, I'll show you why.

_____
## The Magnitude of Time in Millis

Let's get a feel for the scale of milliseconds.

- <span title="(Cool, looks manageable)">1 sec = 1,000 ms</span>
- <span title="(Well that was a big jump ^_^')">1 min = 60 sec = 60,000 ms</span>
- <span title="(Spongebob, you're spending all me bits!)">**1 day** = 1,440 min = **86,400,000 ms** </span> 
- <span title="(...!!!) ヽ(ಠ_ಠ)ノ">1 year = 365 days = 31,536,000,000 ms</span>

Hmmmmm... so **~32 billion milliseconds go by each year.**

Seems like a rather large value for a *single* year, doesn't it?

_____
## The Magnitude of the Container

Now we'll get a feel for the scale of our data types which will hold the milliseconds elapsed since a given date.

While word sizes can be 32 or 64 bits, we forgo one bit in order <span title="meaning time in the past, before 1970">to represent negative values, i.e. time before the Unix epoch.</span>

**Signed Max Values**
- 32-bit word: 2^31 = 2,147,483,648 ≈ 2.15E9
- 64-bit word: 2^63 = 9,223,372,036,854,775,808 ≈ 9.2E18

Since the number of milliseconds in a year is 3.15E10, we've already surpassed the size of a 32-bit word. But there's still hope!

_____
A 64-bit word can represent a value several magnitudes higher than a year of milliseconds, i.e. <span title="Signed 64-bit Max Value: 9.2E18">10<sup>18</sup></span> vs <span title="Milliseconds in a year: 3.15E10">10<sup>10</sup></span>, but just how many years?

`9,223,372,036,854,775,808 ms ÷ 31,536,000,000 ms/yr  = 292,471,208.68 years`

A 64-bit word can represent time a colossal 2.9E8 years to either end of a given date, i.e. **~290 million years in the past or future**.

So maybe if humans are still around by `268,435,456 AD (2^28)`, we'll need a v2 of the Unix epoch, the restart of time.

Perhaps we'll decide to reset at a happier number, e.g. 250,000,000 AD.

I imagine by then our processors will support much higher word sizes, e.g. x256, x2048, and beyond. I personally can't fathom practical uses of word sizes over 128-bits aside from cryptography. However, intergalactic conquest & expansion may prove this point moot.

In any case, we can put our minds at ease. The restart of time of time won't happen on our watch. :drum:

Until Next Time,
Pulkit

_____
### References:
1. [System API | JDK 16 Docs](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/System.html#currentTimeMillis())
1. [Unix Time | Narrative.io](https://kb.narrative.io/what-is-unix-time)
1. [Why is 1/1/1970 the “epoch time”? | StackOverflow](https://stackoverflow.com/questions/1090869/why-is-1-1-1970-the-epoch-time)
1. [Why are dates calculated from January 1st, 1970? | StackOverflow](https://stackoverflow.com/questions/2533563/why-are-dates-calculated-from-january-1st-1970)

_____
### Further Reading:
1. [Basics of Bit Manipulation | HackerEarth](https://www.hackerearth.com/practice/basic-programming/bit-manipulation/basics-of-bit-manipulation/tutorial/)

_____
### Published on Medium
- [The Restart of Time | CoffeelessProgrammer | Medium](https://coffeelessprogrammer.medium.com/the-restart-of-time-2aab3f19bde0?source=friends_link&sk=1a1cac750a6aa8f1ed9ee3bbb10499eb)