 |
Calendar Functions
summary
 |
Days between dates, Day of the week, and more...
|
instructions
 |
I am indebted to my old HP-67 for this program. I added a "^Shift" button and some advanced functionality to the version that came with the HP-67 in the standard pack. But, the algorithms come straight from the manual for this great old calculator.
===>The RPN 2.56 "User Interface"<===
----------------- | DT1 DT2 | | #Days #Wks.Dys | | ^Shift DT->DOW | -----------------
===>BASIC OPERATION<===
The idea is that you can enter two dates and then calcuate the number of days between them (or the number of weeks and days). Or, you can enter a date and a number of days (or a number of weeks and days) and calculate the future or past date. Or you can enter a date and calculate the day of the week.
Example 1: 7.191999 DT1 12.251999 DT2 #Days
The sequence in Example 1 calculates that there are 159 days between July 19, 1999 and December 25, 1999.
Example 2: #Wks.Dys
This example continues from the first example. 22.5 is displayed indicating that there are 22 weeks and 5 days between July 19, 1999 and December 25, 1999.
NOTE =>
" When entering a date, be sure that you always input TWO digits for the day. e.g., 7.022002 for July 2, 2002 and NOT 7.22002. When entering a number of weeks and days you enter the number of weeks, followed by the decimal point, followed by the number of days. "
Example 3: 7.191999 DT1 159 #Days DT2
In this example, you want to know what the date will be in 159 days if the given date is 7/19/1999. Of course, the answer is 12/25/1999.
Example 4: 7.191999 DT2 159 #Days DT1
In this example, you want to know what the date was 159 days ago if the given date is 7/19/1999. The answer is 2/10/1999.
NOTE: To calculate a date a given number of days in the past, enter the date with DT2 and then enter the days with #Days and then press DT1.
To calculate a date a given number of days in the future, enter the date with DT1 and then enter the days with #Days and then press DT2.
Example 5: 12.251999 DT->DOW
Christmas in 1999 falls on Saturday.
===>ADVANCED OPERATION<===
The program is always in one of three "modes": "recall mode", "input mode", or "calculate mode".
By pressing the numbers and decimal point you put the program into "input mode" so that when DT1, DT2, #Days, or #Wks.Dys is pressed the input is "stored". Also, and this is important, the program mode switches to the "calculate mode". Assuming there are enough "inputs" (AND THERE BETTER BE because no checking is done!) another press of DT1, DT2, #Days, or #Wks.Dys will calculate that key based on the stored information in the other keys and put the program back into "input mode". When a calculation is done, the calculation is also stored for later retrieval via the ^Shift key.
IMPORTANT=>
" input mode => DT1, DT2, #Days, or #Wks.Dys => calculate mode => DT1, DT2, #Days, or #Wks.Dys => input mode "
Pressing the ^Shift key puts the program in "recall mode" and then pressing DT1, DT2, #Days, #Wks.Dys or DT->DOW will recall the last input and put the program in "input mode".
DT-DOW is a little different - as long as there is a valid date on the top of the stack, DT-DOW will store and calculate the day of the week. ^Shift DT-DOW will recall the last date stored by this key.
The program "remembers" if #Days or #Wks.Dys was pressed last. Calculation is done with whatever was input last.
With this information in mind, let's do some "advanced" examples:
Example 6: 7.191999 DT1 1.0 #Wks.Dys DT2 DT1 DT2 DT1 DT2
The stack will be tos=8.091999 8.021999 7.261999 1 7.1999. You can see what the date will be 3 weeks, 2 weeks, and 1 week from 7/19/1999.
Because a calculation throws the program into "input" mode, "chaining" is possible.
===>FINAL EXAM<===
You arrive in Tokyo, Japan on 8/02/1999. You know that you will stay 17 days in Tokyo. Then, you will go to Nara for 13 days. Then, you will go to Osaka for 2 weeks and 2 days. Compute the arrival date and day for each city. And, what date and day will you leave?
Solution:
8.021999 DT->DOW (Arrive on Monday in Tokyo) DT1 17 #Days DT2 (Arrive on 8/19/1999 in Nara) DT->DOW (on Thursday) DT1 13 #Days DT2 (Arrive on 9/01/1999 in Osaka) DT->DOW (on Wednesday) DT1 2.2 #Wks.Dys DT2(Depart on 9/17/1999) DT->DOW (a Friday)
===>EQUATIONS<===
To compute the Julian Day number from the date:
Julian Day number = INT(365.25y') + INT(30.6001 m') + d + 1,720,982
where y' = year-1 if m = 1 or 2 OR year if m > 2. and m' = month+13 if m=1 or 2 month+1 if m > 2
Then, the days between dates is found by
Days = Julian Day number 2 - Julian Day number 1
To compute the date from a Julian Day number:
Day # = Julian Day number - 1,720,982
y' = INT[(Day # - 122.1)/365.25]
*The Julian Day number is an astronomical convention representing the number of days since January 1, 4713 B.C.
m' = INT[(Day # - INT(365.25 y'))/30.6001]
Day of the month = D = Day # - INT [365.25 y'] - INT [30.6001 m']
The Month = M = m' - 13 if m' = 14 or 15 OR m' - 1 if m' < 14
The Year = Y = y' if M > 2 OR y' + 1 if M = 1 or 2 To compute the day of the week:
Day of the week = 7 x FRAC [(Day # + 5)/7] 0=Sunday, 1=Monday etc ...
===>WORKING RANGE<===
According to my HP-67 manual, this algorithm is good for dates from March 1, 1900 through February 28, 2100.
|
code
 |
RPN.2.z+3 \Calendar functions
[a]#'1720982';
[b]#'30.6001';
[f]#'365.25';
[g]#'11';
[e]#'10'r2P;
[d] g1g1g1g1xo1+X@wxo3+X
@f2Ce*wxo4+X@f2Ce*f4C
e*xo2+X@Cjxo5+X@;
[j]Cfxo2+x@xo3+x@g1V2>(:
1-)*wCbvg12>(1+:Cg2++)*w
+xo4+x@+Ca+;
[c] xk5+x@xs1=0(xl:xmw7*x
mf1Ce*+)xk0=0(+:-)Cs;
[s] Ca-Xuxu#'122.1'-Cf/wXvx
uCfxv*w-Cb/wXwxuCfxv*w
-Cbxw*w-XyxwCg3+<(xw1-:
xwCg2+-)Vvxy2Ce/+xvv2>(:
1+)6Ce/+;
[h]V
xz1=0(
\shift mode
v1+x@0Xz1Xx.:
\not Shift mode, go on
)
xx1=0(
\input mode
vXoCd0XxvXk1Xu
:
\calculate mode
xuv=0(.:)
CcvXoCd1Xx
vXu
);
[i]1XuV
xz1=0(
\shift mode
vCg+x@0Xz1Xx.:
\not shift mode, go on
)
xx1=0(
\input mode
g1vCg+X@0XxvXs:
\calculate mode
xjxe-v2=0(g17/wr27/f7*1Ce/
+:)g1vCg+X@);
[k] xz1=0(xp0Xz.:)
#'15'XoCdxtCa-5+7/f7*
c(D'Sunday':
D'Monday':
D'Tuesday':
D'Wednesday':
D'Thursday':
D'Friday':
D'Saturday')d1;
{b} Vv#'21'<(1Xx:);
"Calendar functions"
"DT1"0Ch;
"DT2"5Ch;
~
"#Days"1Ci;
"#Wks.Dys"2Ci;
~
"^Shift"1Xz;
"DT->DOW"Ck;
|
|