News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

CCYYMMDD dword to Julian date conversion

Started by fearless, April 27, 2009, 09:28:22 PM

Previous topic - Next topic

fearless

Thought this may come in handy for others, i found one or two algos that required decimal points to be included which would mean using fpu.
This algo from Peter Meyer (http://www.hermetic.ch/cal_stud/jdn.htm) doesnt, so ive based this function on his information.

Please feel free to use, abuse, comment, etc.

JulianDate PROC DwordDate:DWORD

LOCAL _CentYear:DWORD
LOCAL _Month:DWORD
LOCAL _Day:DWORD

LOCAL msub14div12:DWORD
LOCAL _Stage1:DWORD
LOCAL _Stage2:DWORD
LOCAL _Stage3:DWORD

; DwordDate contains CentYear in upper word, month and day in lower word. CCYYMMDD

; Test data:
;
; 1582 October 15
;
;jd = ( 1461 * ( y + 4800 + ( m - 14 ) / 12 ) ) / 4 +
; ( 367 * ( m - 2 - 12 * ( ( m - 14 ) / 12 ) ) ) / 12 -
; ( 3 * ( ( y + 4900 + ( m - 14 ) / 12 ) / 100 ) ) / 4 +
; d - 32075
;
;jd = ( 1461 * (1582 + 4800 + (10 - 14) / 12 ) ) /4 +
; ( 367 * ( 10 - 2 - 12 * ( ( 10 - 14 ) /12 ) ) ) /12 -
; ( 3 * ( ( 1582 + 4900 + ( 10 - 14 ) / 12 ) /100 ) ) /4 +
; 15 - 32075
;
;jd = ( 1461 * (1582 + 4800 + 0 ) ) /4 +
; ( 367 * ( 10 - 2 - 12 * ( 0 ) ) ) /12 -
; ( 3 * ( ( 1582 + 4900 + 0 ) /100 ) ) /4 +
; 15 - 32075
;
;jd = ( 1461 * (6382) ) /4 +
; ( 367 * ( 10 - 2 - 0 ) ) /12 -
; ( 3 * ( ( 6482 ) /100 ) ) /4 +
; 15 - 32075
;
;jd = ( 9324102 ) /4 +
; ( 367 * ( 8 ) ) /12 -
; ( 3 * ( 64 ) ) /4 +
; 15 - 32075
;
;jd = 2331025 +
; 244 -
; 192 /4 +
; 15 - 32075
;
;
;jd = 2331025 +
; 244 -
; 48 +
; 15 - 32075
;
;jd = 2299161
;
; 1582 October 15

; Break up date info to day, month, year
mov edx, DwordDate
xor eax, eax
mov al, dl
mov _Day, eax
xor eax, eax
mov al, dh
mov _Month, eax
mov edx, DwordDate
shr edx,16
mov _CentYear, edx


; Stage 1
mov eax, _Month
sub eax, 14
mov ecx, 12
cdq
idiv ecx
mov msub14div12, eax
; msub14div12 is repeated in calculation so keep for later use
add eax, 4800
add eax, _CentYear
mov ecx, 1461
xor edx, edx
mul ecx
xor edx, edx
mov ecx, 4
div ecx
mov _Stage1, eax

; Stage 2
mov eax, msub14div12
mov ecx, 12
xor edx, edx
mul ecx
mov ebx, eax
mov eax, _Month
sub eax, 2
sub eax, ebx
mov ecx, 367
xor edx, edx
mul ecx
mov ecx, 12
xor edx, edx
div ecx
mov _Stage2, eax

; Stage 3
mov eax, msub14div12
add eax, 4900
add eax, 1582
mov ecx, 100
xor edx, edx
div ecx
mov ecx, 3
xor edx, edx
mul ecx
mov ecx, 4
xor edx, edx
div ecx
mov _Stage3, eax

; Final Stage, put all together
mov eax, _Stage1
add eax, _Stage2
sub eax, _Stage3
add eax, _Day
sub eax, 32075

; eax is julian date
ret
JulianDate ENDP
Ć’earless

NightWare

hi,

mul by 3 :
   lea Reg,[Reg*2+Reg]   ; mul by 3

mul by 12 :
   lea Reg,[Reg*2+Reg]   ; mul by 3
   shl Reg,2      ; mul by 4

div by 4 :
   shr Reg,2      ; div by 4

div by 12 :
   mov eax,Reg   ; here, must be tested with all the possible value
   mov edx,357913942
   mul edx
   mov Reg,edx
or
   mov eax,Reg
   mov edx,2863311531
   mul edx
   shr edx,3
   mov Reg,edx

div by 100 :
   mov eax,Reg   ; here, must be tested with all the possible value
   mov edx,42949673
   mul edx
   mov Reg,edx
or
   mov eax,Reg   ; here, must be tested with all the possible value too
   inc eax
   mov edx,2748779069
   mul edx
   shr edx,6
   mov Reg,edx

also :
   add eax,4900
   add eax,1582
can be replaced by :
   add eax,6482 (or add eax,4900+1582 if you want to maintain understanding)

additionnal note : no need to clean edx for multiply.  :wink