Here is a program for calculating Julian day numbers with a base date of 01-Jan-2000 (= day zero). With word variables, the calculations shouldn't overflow until after 31-Dec-2178. I have tried to minimize the number of time-eating multiplication and division steps in this code. Also, with X1/X2 Picaxe versions, shift-right binary math is available.
Formulas are derived from the following sources:
Eric W. Weisstein
Tracy Allen
Formulas are derived from the following sources:
Eric W. Weisstein
Tracy Allen
Code:
'Julian day number (JDN) calculator
'Created: 2007-12-23 (T.Ikeda - KA1OS)
'Latest mod: 2008-01-27
'Works with dates from 2000-01-01 to 2178-12-31
'
'On 2000-01-01, JDN = 0 (Epoch: 2000-01-01) and increments
'from there. This program properly accounts for 2100
'not being a leap year.
'
'In the simulator, this doesn't over overflow until
'2178-06-05 (05-Jun-2179). In theory, the processor should
'overflow in one of the calculation steps (367 * YYY) when
'the YYY is 179
'
'Formulas derived from the following sources and optimized
'for Picaxe math (minimizing multiplication & division
'steps in the calculations):
' http://scienceworld.wolfram.com/astronomy/JulianDate.html
' by Eric W. Weisstein
' http://www.emesystems.com/BS2math4.htm
' by Tracy Allen
'
'Note that this program uses *no* error checking for illegal
' dates: e.g. 2007-13-32
'
Symbol JDN = w4
Symbol YYY = b5 '0 to 178, corresponding to years 2000-2178
Symbol MM = b6 'Months: 1-12
Symbol DD = b7 'Day: 1-31
'Plug-in values for testing... Remove after debugging.
'06-Jun-2010 =
YYY = 10
MM = 6
DD = 6
JDN = 0
If MM > 2 Then
JDN = 1
End If
JDN = YYY + JDN * 7 / 4
JDN = 367 * YYY - JDN
JDN = MM * 3912 / 128 + DD - 31 + JDN
'*******************************************
'X1/X2 parts can use shift right instead of divides.
'JDN = (YYY + JDN) * 7 >> 2
'JDN = (367 * YYY) - JDN
'JDN = ((MM * 3912) >> 7) + DD - 31 + JDN
'*******************************************
'Add correction for 2100 not being a leap year...
If JDN > 36584 Then
JDN = JDN -1
End If
'Testing output... Remove after debugging.
bintoascii JDN, b0, b1, b2, b3, b4
sertxd (b0,b1,b2,b3,b4)
End