Julian and calendar date subroutines
Posted: Mon Jul 20, 2015 6:44 am
Here are two EzSBC Control BASIC subroutines that can be used to calculate Julian and calendar dates. The source code listing includes a short program that demonstrates how to interact with these two subroutines.
Note that the time between two calendar dates is the difference between their corresponding Julian dates.
Additional information about dates can be found at my Orbital and Celestial Mechanics Website located at
http://celestialandorbitalmechanicswebs ... asite.com/
Note that the time between two calendar dates is the difference between their corresponding Julian dates.
Additional information about dates can be found at my Orbital and Celestial Mechanics Website located at
http://celestialandorbitalmechanicswebs ... asite.com/
Code: Select all
' dates.bas
' this program demonstrates how to interact
' with the julian and gdate subroutines
'
' July 20, 2015
'''''''''''''''
pi2 = 2.0 * PI
' user-defined define calender date
month = 6
day = 16.2468642
year = 2015
' print calendar date
PRINT " "
PRINT "user-defined calendar date"
PRINT " "
PRINT "month = ", month
PRINT "day = ", day
PRINT "year = ", year
' call julian date subroutine
GOSUB JULIAN:
' print julian date
PRINT " "
PRINT "jdate = ", jdate
' call calendar date subroutine
GOSUB GDATE:
PRINT " "
PRINT "calendar date computed from julian date"
PRINT " "
PRINT "month = ", month
PRINT "day = ", day
PRINT "year = ", year
END
''''''''''''''''
''''''''''''''''
JULIAN:
' Julian date subroutine
' Input
' month = calendar month [1 - 12]
' day = calendar day [1 - 31]
' year = calendar year [yyyy]
' Output
' jdate = Julian date
' special notes
' (1) calendar year must include all digits
' (2) will report October 5, 1582 to October 14, 1582
' as invalid calendar dates and set jdate = 0
''''''''''''''''''''''''''''''''''''''''''''''''''
y = year
m = month
b = 0.0
c = 0.0
IF (m <= 2.0) THEN
y = y - 1.0
m = m + 12.0
ENDIF
IF (y < 0.0) THEN
c = -0.75
ENDIF
' check for valid calendar date
IF (year < 1582.0) THEN
' null
ELSE
IF (year > 1582.0) THEN
a = INT(y / 100.0)
b = 2.0 - a + INT(a / 4.0)
ENDIF
ELSE
IF (month < 10.0) THEN
' null
ENDIF
ELSE
IF (month > 10.0) THEN
a = INT(y / 100.0)
b = 2.0 - a + INT(a / 4.0)
ENDIF
ELSE
IF (day <= 4.0) THEN
' null
ENDIF
ELSE
IF (day > 14.0) THEN
a = INT(y / 100.0)
b = 2.0 - a + INT(a / 4.0)
ENDIF
ELSE
' this is an invalid calendar date
PRINT "this date does not exist"
jdate = 0.0
RETURN
ENDIF
jd = INT(365.25 * y + c) + INT(30.6001 * (m + 1.0))
jdate = jd + day + b + 1720994.5
RETURN
'''''''''''''''
'''''''''''''''
GDATE:
' convert Julian date to Gregorian (calendar) date subroutine
' input
' jdate = julian day
' output
' month = calendar month [1 - 12]
' day = calendar day [1 - 31]
' year = calendar year [yyyy]
' note: day may include fractional part
''''''''''''''''''''''''''''''''''''''''
jd = jdate
z = INT(jd + 0.5)
fday = jd + 0.5 - z
IF (fday < 0.0) THEN
fday = fday + 1.0
z = z - 1.0
ENDIF
IF (z < 2299161.0) THEN
a = z
ELSE
alpha = INT((z - 1867216.25) / 36524.25)
a = z + 1.0 + alpha - INT(alpha / 4.0)
ENDIF
b = a + 1524.0
c = INT((b - 122.1) / 365.25)
d = INT(365.25 * c)
e = INT((b - d) / 30.6001)
day = b - d - INT(30.6001 * e) + fday
IF (e < 14) THEN
month = e - 1.0
ELSE
month = e - 13.0
ENDIF
IF (month > 2.0) THEN
year = c - 4716.0
ELSE
year = c - 4715.0
ENDIF
RETURN
'''''''''''''''
'''''''''''''''
SIGN:
' sign of a variable subroutine
' input
' x = argument
' output
' sgn = sign of x
''''''''''''''''''
IF (abs(x) <= 1.0e-12) THEN
sgn = 0.0
RETURN
ENDIF
IF (x > 0.0) THEN
sgn = 1.0
RETURN
ENDIF
IF (x < 0.0) THEN
sgn = -1.0
ENDIF
RETURN