Search code examples
pythoncomputer-scienceunix-timestamp

How was the date of December 4, 292,277,026,596 calculated for the overflow of the 64 bit signed unix timestamp?


I've tried to calculate the date myself using the maximum 64-bit signed integer but always end up with another date that's a few million years different. I've tried using sidereal years and leap years but I always get results that are at least a few million years off.

Here is what I've tried thus far:

dateA = 1970 + (9223372036854775807/31556926.08)

dateB = 1970 + (9223372036854775807/31536000) + (((9223372036854775807/31536000)/4)/365)

Both return the wrong answer. Can anyone guide me in the right direction?


Solution

  • You need to account for the years that are leap years. A year is a leap year if:

    • it is evenly divisible by 4;
    • unless it is evenly divisible by 100;
    • or if it's evenly divisible by 400.

    As a rough estimate for the year, calculating 1970 + 0x7fffffffffffffff // 86400 // (365 + 1/4 - 1/100 + 1/400) gives an answer of 292277026596. I'll leave the derivation of the exact day as an exercise for the reader.

    I used Python 3 for this calculation which gives real results for integer divides. Adjust accordingly for Python 2.