; DATE

; calculates the day of week of any date since 1583
; calculates the number of days between two dates.

; usage: DAY enter MONTH enter YEAR run
; Output : X = day of week (0=MONDAY)
;          Y = number of days between two dates (*)
;          Z and Reg a = Julian date in days.

; (*) after the fist date, if you do RCL a STO e, you store this date as a reference
; and any following calculation will give you the difference using this date.

; Example: Today 4 april 2014
; --> 4 enter 4 enter 2014 run
;     output: 4 (FRIDAY)
; then RCL a STO e (value 735692)
;
; I was born on 23 march 1966
; --> 23 enter 3 enter 1966 run
;     output: 2 (WEDNESDAY)
;	      X<>Y --> -17544, so this is my 17544th day of life!


#C3 2

#reg 0 year
#reg 1 month
#reg 2 day
#reg 3 z
#reg a julien
#reg e reference

DO
	=year rot
	=month rot				; if you prefer the english way just swap the
	=day					; =month and =day
	@month 3 -
	IF(x<0)THEN
   		0 @year 1 -			; jan & feb : d=0, z=year-1
	ELSE
   		@month $0.4 * $2.3 + int	; mar-dec: d=INT(0.4m+2.3), z=year
   		@year
	ENDIF
	=z					; store z
	4 / int x<>y -				; jul = int(z/4)-d
	@day +					;       + day
	@month 1 - $31 * +			;       + 31*(month-1)
	@year $365 * +				;       + 365*year
	@z $100 / int 1 + 3 * 4 / int -		;       - INT(0.75*(int(z/100)+1))
	=julien enter enter			; = date julien
	$578174 -				; this is value for 27 dec 1582 = MONDAY
	enter enter
	7 / int 7 * -				; day of week (0=MONDAY, 6=SUNDAY)
	x<>y
	@reference -				; days between dates (with reg e = reference)
	x<>y
	STOP
LOOP

