[fpc-pascal]Week Day
Marco van de Voort
marcov at stack.nl
Thu Apr 19 00:02:39 CEST 2001
> Does anyone know how to get the actual day (mon, tues, wed, etc) that a
> file was created? The datetime record doesn't seem to have that info,
> nor does the searchrec. Is there a way to get this information from a
> packed date?
No, but you can simply calculate it.
FUNCTION LeapYr( Year : LONGINT) : BOOLEAN; ASSEMBLER;
asm
push %ebx // ease calls from other procedures
push %ecx
push %edx
movl Year,%eax
testl $3,%eax
jne .LLeapEnd
movl $100,%ebx
cltd
idivl %ebx
testb %dl,%dl
jne .LLeapTrue
and $3,%eax
je .LLeapTrue
.LLeapEnd: setb %al
jmp .LLeapExit
.LLeapTrue: movl $1,%ax
.LLeapExit: pop %edx
pop %ecx
pop %ebx
and $1,%al
end ['EAX']; // procedure saves %ebx..%edx, for internal calling.
{ Calculates daynumber, January 1st =1. incl leapyear(LeapYr())
correction }
FUNCTION DayNr( Day, Month, Year: LONGINT) : LONGINT;
ASSEMBLER;
TYPE DyNr=ARRAY[0..11] OF LONGINT;
CONST MonthDays :
DyNr=(0,31,59,90,120,151,181,212,243,273,304,334);
ASM
movl Month,%ebx
movl Day,%ecx
cmp $2,%ebx
jbe .LDayNr2
.LDayNr1: push Year
call LeapYr
add %eax,%ecx
.LDayNr2: decl %ebx
movl MonthDays(,%ebx,4),%eax
add %ecx,%eax
END ['EAX','EBX','ECX'];
TYPE Arr12 = ARRAY[0..11] OF BYTE;
TabType=ARRAY[0..3] OF BYTE;
CONST TabCent : TabType = (0,6,4,2); { Century correction}
TabMonth : Arr12 = (0,3,3,6,1,4,6,2,5,0,3,5); {Month indices}
FUNCTION DOW( Year,Month,Day:LONGINT): LONGINT;
ASSEMBLER;
{ DOW( a Sunday)=0; DOW( a Monday)=1
Ultrafast since it works with tables. }
ASM
xorl %ecx,%ecx
mov Year,%eax
cmp $2000,%eax
jb .LBelow2000
cmp $3,Month
jb .LBelow2000
incl %ecx
.LBelow2000: movl $100,%ebx
cltd
idivl %ebx
incl %eax
andl $3,%eax
movzbl TabCent(,%eax),%ebx
// ebx contains century correction
mov Day,%eax
addl %eax,%ebx // Add Day
addl %edx,%ebx // Add Year MOD 100
shrl $2,%edx
addl %edx,%ebx // Add (Year MOD 100) SHR 2
mov Month,%eax // Month
decl %eax
movzbl TabMonth(,%eax),%edx // is index
addl %edx,%ebx // Add TabMonth[Month]
movl $7,%eax
xchgl %eax,%ebx
addl %ecx,%eax
incl %eax
cltd
idivl %ebx // Divide total by 7
movl %edx,%eax
END ['EAX','EBX','ECX','EDX'];
begin
end.
Marco van de Voort (MarcoV at Stack.nl or marco at freepascal.org)
More information about the fpc-pascal
mailing list