# How many bits for timestamps?

2020-12-11 13:43:07 +0000

Signed 32-bit `time_t`, which counts seconds since 1970 runs out after 2^31 seconds; i.e. in 2038.

You probably knew that, so you’ve been looking at 64-bit numbers, and you’re probably thinking about using milliseconds instead.

Here’s a quick way to estimate how long that’ll last.

Note that 2^10 (1024) ~= 10^3 (1000) meaning that every unit of one thousand needs 10 bits.

Given 64 bits, take 10 bits for milliseconds, leaving 54 bits.

There are 60 seconds in a minute, which is pretty close to 63 (i.e. 6 bits). That leaves you with 48 bits. Similarly, 60 minutes in an hour. That leaves you with 42 bits.

Next up is hours. There are 24 hours in a day. The next largest power of two is 32, which takes 5 bits (0-31). That leaves you with 37 bits.

There are 365 and a bit days in a year. That’s going to need 9 bits (0-511). That leaves you with 28 bits.

``````         1         2         3         4         5         6   6
1234567890123456789012345678901234567890123456789012345678901234
............................DDDDDDDDDHHHHHMMMMMMssssssmmmmmmmmmm
``````
Units BitsRemaining
64 bits
Milliseconds1000~102310 bits54 bits
Seconds60~636 bits48 bits
Minutes60~636 bits42 bits
Hours24~315 bits37 bits
Days365.25~5119 bits28 bits