cdeagle Senior Member
Joined: 22/06/2014 Location: United StatesPosts: 261 |
Posted: 08:29am 02 Mar 2017 |
Copy link to clipboard |
Print this post |
|
This post presents double precision calendar and Julian day subroutines for the Micromite eXtreme. The following is the output from an interactive program that converts a calendar date to its corresponding Julian day and vice versa.
The program also computes the number of days between two calendar dates which is simply the difference between their corresponding Julian days.
Many countries currently use the Gregorian calendar implemented by Pope Gregory XIII in the sixteenth century. Thanks to the pope, October 5, 1582 to October 14, 1582 are invalid calendar dates!
The Julian day is the number of continuous days that have elapsed since noon on January 1, 4712 B.C. The Julian day begins at noon because astronomers don't like to change their clocks during the night while they are observing. Also note that the Julian date, based on the calendar implemented by Julius Caesar, is not the same as the Julian day number.
program demo_dates - demonstrate date conversions ================================================= please input the calendar date (month [1 - 12], day [1 - 31], year [yyyy]) < for example, october 21, 1986 is input as 10,21,1986 > < b.c. dates are negative, a.d. dates are positive > < the day of the month may also include a decimal part > ? 10,21,2017 please input the time (hours [0 - 24], minutes [0 - 60], seconds [0 - 60]) ? 10,20,30.4589 convert calendar date and time to julian day -------------------------------------------- calendar month = 10 calendar day = 21 calendar year = 2017 time = 10 hr 20 min 30.4589 sec jday = 2458047.930908089 convert Julian day to Gregorian date and time --------------------------------------------- jday = 2458047.930908089 calendar month = 10 calendar day = 21 calendar year = 2017 time = 10 hr 20 min 30.4589 sec number of days between two dates -------------------------------- first date please input the calendar date (month [1 - 12], day [1 - 31], year [yyyy]) < for example, october 21, 1986 is input as 10,21,1986 > < b.c. dates are negative, a.d. dates are positive > < the day of the month may also include a decimal part > ? 3,30,2017 second date please input the calendar date (month [1 - 12], day [1 - 31], year [yyyy]) < for example, october 21, 1986 is input as 10,21,1986 > < b.c. dates are negative, a.d. dates are positive > < the day of the month may also include a decimal part > ? 10,21,2017 number of days between dates = 205 Here's the source code for the main program and subroutines;
' demo_dates.bas
' demonstrate date conversions
' March 2, 2017
'''''''''''''''
print " " print "program demo_dates - demonstrate date conversions" print "================================================="
' request calendar date (month, day, year)
getdate(month1, day0, year1)
' request time (hours, minutes, seconds)
gettime(thr1, tmin1, tsec1)
' compute "complete" calendar day
day1 = day0 + thr1 / 24.0 + (tmin1 / 60.0) / 24.0 + (tsec1 / 3600.0) / 24.0
' compute corresponding Julian day
julian (month1, day1, year1, jday1)
' print results
print " "
print "convert calendar date and time to julian day"
print "--------------------------------------------"
print " "
print "calendar month = ", month1
print "calendar day = ", day0
print "calendar year = ", year1
print " "
print "time = ", str$(thr1) + " hr " + str$(tmin1) + " min " + str$(tsec1, 0, 4) + " sec"
print " "
print "jday = ", str$(jday1, 0, 9)
print " "
print "convert Julian day to Gregorian date and time"
print "---------------------------------------------"
print " "
print "jday = ", str$(jday1, 0, 9)
' compute calendar date
gdate (jday1, month2, day2, year2)
print " "
print "calendar month = ", month2
print "calendar day = ", int(day2)
print "calendar year = ", year2
print " "
thr0 = 24.0 * (day2 - int(day2))
thr2 = int(thr0) tmin0 = 60.0 * (thr0 - thr2)
tmin2 = int(tmin1)
tsec2 = 60.0 * (tmin0 - tmin2)
print "time = ", str$(thr2) + " hr " + str$(tmin2) + " min " + str$(tsec2, 0, 4) + " sec"
print " "
print "number of days between two dates"
print "--------------------------------"
print " "
print "first date"
getdate(month3, day3, year3)
julian (month3, day3, year3, jday3)
print " "
print "second date"
getdate(month4, day4, year4)
julian (month4, day4, year4, jday4)
delta_days = jday4 - jday3
print " "
print "number of days between dates = ", delta_days
print " "
end
''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''
sub julian (month, day, year, jday)
' Gregorian date to julian day subroutine ' input
' month = calendar month ' day = calendar day ' year = calendar year (all four digits)
' output
' jday = julian day ' special notes
' (1) calendar year must include all digits ' (2) will report October 5, 1582 to October 14, 1582 ' as invalid calendar dates and exit
''''''''''''''''''''''''''''''''''''''''' y = year
m = month
b = 0.0
c = 0.0
if (m <= 2.0) then
y = y - 1.0
m = m + 12.0
end if
if (y < 0.0) then c = -0.75.0
if (year < 1582.0) then
' null
elseif (year > 1582.0) then
a = fix(y / 100.0)
b = 2.0 - a + fix(a / 4.0)
elseif (month < 10.0) then
' null
elseif (month > 10.0) then
a = fix(y / 100.0)
b = 2.0 - a + fix(a / 4.0)
elseif (day <= 4.0) then
' null
elseif (day > 14.0) then
a = fix(y / 100.0)
b = 2.0 - a + fix(a / 4.0)
else
print "this date does not exist!!"
exit
end if
jday = fix(365.25 * y + c) + fix(30.6001 * (m + 1.0)) + day + b + 1720994.5
end sub
'''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''
sub gdate (jday, month, day, year)
' Julian day to Gregorian date subroutine
' input
' jday = julian day ' output
' month = calendar month ' day = calendar day ' year = calendar year
'''''''''''''''''''''''' z = fix(jday + 0.5) f = jday + 0.5 - z
if (z < 2299161) then a = z else alpha = fix((z - 1867216.25) / 36524.25) a = z + 1.0 + alpha - fix(alpha / 4.0) end if
b = a + 1524.0 c = fix((b - 122.1) / 365.25) d = fix(365.25 * c) e = fix((b - d) / 30.6001)
day = b - d - fix(30.6001 * e) + f
if (e < 13.5) then month = e - 1.0 else month = e - 13.0 end if
if (month > 2.5) then year = c - 4716.0 else year = c - 4715.0 end if end sub
'''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''
sub getdate (month, day, year)
' request calendar date subroutine
do print " " print "please input the calendar date" print " " print "(month [1 - 12], day [1 - 31], year [yyyy])" print "< for example, october 21, 1986 is input as 10,21,1986 >" print "< b.c. dates are negative, a.d. dates are positive >" print "< the day of the month may also include a decimal part >" print " " input month, day, year loop until (month >= 1 and month <= 12) and (day >= 1 and day <= 31)
end sub
''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''
sub gettime (thr, tmin, tsec)
' request time subroutine
do print " " print "please input the time" print " " print "(hours [0 - 24], minutes [0 - 60], seconds [0 - 60])" print " " input thr, tmin, tsec loop until (thr >= 0.0 and thr <= 24.0 and tmin >= 0.0 and tmin <= 60.0 and tsec >= 0.0 and tsec <= 60.0)
end sub
|