Input: # of seconds since January 1st, of Year 0001
Output: # of Full years during this time period
I have developed an algorithm that I do not think is the optimal solution. I think there should be a solution that does not involve a loop. See Code Block 1 for the algorithm which A) Determines the quantity of days and B) Iteratively subtracts 366 or 365 depending on Leap Years from the Day Total while incrementing the Year Total
It's not as simple as Dividing DayCount by 365.2425 and truncating, because we hit a failure point at on January 1, 0002 (31536000 Seconds / (365.2425 * 24 * 60 * 60)) = 0.99934.
Any idea on a non-looping method for extracting years from a quantity of seconds since January 1, 0001 12:00 AM?
I need to figure this out because I need a date embedded in a long (which stores seconds) so that I can track years out to 12+ million with 1-second precision.
Code block 1 - Inefficient Algorithm to get Years from Seconds (Including Leap Years)
Dim Days, Years As Integer
'get Days
Days = Ticks * (1 / 24) * (1 / 60) * (1 / 60) 'Ticks = Seconds from Year 1, January 1
'get years by counting up from the beginning
Years = 0
While True
'if leap year
If (Year Mod 4 = 0) AndAlso (Year Mod 100 <> 0) OrElse (Year Mod 400 = 0) Then
If Days >= 366 Then 'if we have enough days left to increment the year
Years += 1
Days -= 366
Else
Exit While
End If
'if not leap year
Else
If Days >= 365 Then 'if we have enough days left to increment the year
Years += 1
Days -= 365
Else
Exit While
End If
End If
End While
Return Years
Edit: My solution was to skip the memory savings of embedding a date within 8 bits and to store each value (seconds through years) in separate integers. This causes instant retrievals at the expense of memory.
Edit2: Typo in first edit (8bits)
I know this question is old now, but I see ones like it often and there weren't any easy answers in here.
My solution uses an old trick of writing two dates as if they were numbers (e.g. 'Dec 12th 2013' as
20131212
) and then subtracting one from the other and discarding the last four digits. I dug up my implementation in F#, you can paste it into LinqPad to check answers. Takes leap years etc into account too:Please note that this is quite naive: it doesn't take timezones or historical timezone changes into account beyond those that are already handled by .NET's DateTime class. The actual grunt work is being done by the DateTime.AddSeconds method. Hope this helps.
Wikipeda has an article on Julian Date with an algorithm which you could adapt to your needs.
If you need accuracy to the very second, you'll probably want a commercial-grade datetime package; it's just too complex to do accurately with a simple algorithm. For instance:
Because of these and more complications, you are better off not writing the code yourself, unless you can relax the constraint that you need accuracy to the very second over 12-million years.
I think that this will work for you:
The following assumes that the Gregorian calendar will remain in effect for the upcoming five hundred and eighty four and a half billion years. Be prepared for disappointment, though; the calendar may end up being scrapped as our sun begins to expand, changing the orbit of the Earth and the duration of the year, and it is very likely something else will be adopted when the Earth falls into the sun seven and a half billion years from now.
As an aside, I don't even try to handle dates prior to the adoption of the Gregorian calendar. I simply return the number of days the date occurred prior to the 15th of October, 1582, and the need to be able to express that sort of return value is the only reason the
GetDateFromSerial
function has anasString
parameter.