zwol: stylized sketch of a face in profile (Default)
[personal profile] zwol
If 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.]

April 2017

S M T W T F S
      1
2345678
9101112131415
16171819 202122
23242526272829
30      

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 12th, 2026 01:52 am
Powered by Dreamwidth Studios