Because I'm sure you were wondering
Mar. 2nd, 2007 04:03 pmIf S is a count of seconds since January 1, 1970, 00:00:00 UTC, then y = ⌊S/31601448⌋ + 1970 is an excellent approximation to the Gregorian year Y containing second S. In the range 1970 ≤ Y ≤ 2,147,483,647, y is always equal to either Y or Y − 1.
The weird thing about this is that 31601448 is 365 days 18 hours 10 minutes 48 seconds — a little more than 12 hours longer than the sidereal period of the Earth. Which makes me highly suspicious of the approximation. And yet. It works.
[EDIT 4 March: The above is totally wrong and I have no idea why my original test program said it worked. The correct divisor to use is the actual average length of a Gregorian year, 31556952 seconds. It can be off by ±1 in the above range, and for some reason (probably having to do with the rules for the C division operator on negative integers, which I do not remember) is occasionally off by ±2 in the range [0, 1969]. It is unfortunate that the error can be in either direction, but better that than have to chase the true value for more than a few iterations.]
The weird thing about this is that 31601448 is 365 days 18 hours 10 minutes 48 seconds — a little more than 12 hours longer than the sidereal period of the Earth. Which makes me highly suspicious of the approximation. And yet. It works.
[EDIT 4 March: The above is totally wrong and I have no idea why my original test program said it worked. The correct divisor to use is the actual average length of a Gregorian year, 31556952 seconds. It can be off by ±1 in the above range, and for some reason (probably having to do with the rules for the C division operator on negative integers, which I do not remember) is occasionally off by ±2 in the range [0, 1969]. It is unfortunate that the error can be in either direction, but better that than have to chase the true value for more than a few iterations.]