Post Reply 
Year(s) Month(s) Day(s) between 2 dates
10-22-2015, 09:46 PM
Post: #19
RE: Year(s) Month(s) Day(s) between 2 dates
(10-22-2015 06:02 PM)Dieter Wrote:  The program now is eight lines shorter. AFAIK this is equivalent to the memory consumption of two data registers, but on the other hand the program requires three additional (local) registers... ;-) As far as I can see R.00 seems to remain unused, so LocR 004 (with adjusted register addresses) should be fine as well. Or do I miss something here?

Dieter

You're right. Here is an updated version and with some improvements in the beginning it's even 3 steps shorter :

Code:
01 LBL"YMD"   ; stack: date2, date1
02 LocR 004   ; allocate local registers R.00 to R.03
03 cSTO.00    ; store date2 in R.00 and date1 in R.01
04 DATE->     ; stack: d2, m2, y2, date1
05 <>YZTX     ; stack: m2, y2, date1, d2
06 cSTO.02    ; store m2 in R.02 and y2 in R.03
07 cX<>Z      ; stack: date1, d2, m2, y2
08 DAY        ; stack: d1, d2, m2, y2
09  -         ; stack: d2-d1, m2, y2 – LastX: d1
10 x≥0?       ; d2 ≥ d1?
11 SKIP 011   ; no adjustment required, goto step 23
12 DEC.02     ; adjust m2 in R.02
13 DROP       ; stack: m2, y2 – LastX: d1
14 DSZ X      ; month before m2
15 SKIP 003   ; is it > 0, i.e. Jan...Nov? Then continue
16 DROP
17 #012       ; if not, month is December
18 DEC Y      ; of previous year
19 RCL L      ; now stack holds d1, m2-1, year of m2-1
20 ->DATE     ; generate new date = d1 in month before m2
21 RCL.00
22 ΔDAYS      ; days between date2 and d1 in month before date2
23 SDR 004    ; move days to 0,00dd
24 RCL.01     ; recall date1
25 DATE->     ; stack: d1, m1, y1, 0,00dd
26 DROP       ; stack: m1, y1, 0,00dd
27 cSTO-.02   ; R.02: m2-m1, R.03: y2-y1
28 cDROP      ; stack: 0,00dd
29 RCL.02     ; stack: m2-m1, 0,00dd
30 x≥0?       ; stack: m2-m1, 0,00dd
31 SKIP 003
32 DEC.03     ; adjust y2-y1 in R.03 if m2-m1 negative
33 #012       ; add back 12 months
34  +         ; stack: mm, 0,00dd 
35 SDR 002    ; move months to 0,mm
36  +         ; add days to get 0,mmdd
37 RCL+.03    ; recall and add y2-y1 
38 END        ; return y,mmdd

Notes:
  • line 5: I could have used RDN but it would work only with a 4-level stack while <>YZTX works with both 4- and 8-level stacks
  • line 7 is entered as CPX x<>y but is displayed as cX<>Z
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Year(s) Month(s) Day(s) between 2 dates - Didier Lachieze - 10-22-2015 09:46 PM



User(s) browsing this thread: 1 Guest(s)