home *** CD-ROM | disk | FTP | other *** search
- Unit DateRout;
- Interface
- Uses Dos;
- Type
- Str3 = String[3];
- Str10 = String[10];
-
- Function TdyDate : Str10;
- Function LeapYear(CurrYr:LongInt) : Boolean;
- Procedure DateComp(Dte1,Dte2:Str10;Var TotDays:LongInt);
- Function DateAdd(Dte1:Str10;AddDays:LongInt) : Str10;
- Function DowInt(Dte1:Str10) : LongInt;
- Function DayName(Dte1:Str10) : Str3;
- Function WeekOf(Dte1:Str10;LastDay:LongInt) : Str10;
-
- Implementation
-
- Const
- BaseYr : LongInt = 1600;
- { Note: Gregorian Calender started 10/15/1582 }
- Function TdyDate : Str10;
- Var
- Month, Day : String[2];
- Year : String[4];
- mMonth, mDay, mYear, mDayOfWk : Word;
- Begin
- GetDate(mYear,mMonth,mDay,mDayOfWk);
- Str(mMonth,Month);
- Str(mDay,Day);
- Str(mYear,Year);
- If mMonth<10 Then insert('0',Month,1);
- If mDay <10 Then insert('0',Day,1);
- TdyDate:= Month + '/' + Day + '/' + Year;
- End;
-
- Procedure DateParse(Date:Str10; Var mmi,ddi,yyi:LongInt);
- Var
- mm,dd : String[2];
- yy : String[4];
- Ecode : Integer;
- Begin
- mm:= ''; dd:= ''; yy:= '';
- mmi:= 0; ddi:= 0; yyi:= 0;
- mm:= Copy(Date,1,2); dd:= Copy(Date,4,2); yy:= Copy(Date,7,4);
- Val(mm,mmi,Ecode); Val(dd,ddi,Ecode); Val(yy,yyi,Ecode);
- End;
-
- Function LeapYear(CurrYr:LongInt):Boolean;
- Begin
- LeapYear:= True;
- If (CurrYr mod 4) <> 0 Then LeapYear:= False;
- If (CurrYr mod 100 = 0) and (CurrYr mod 400 <> 0) Then LeapYear:= False;
- End;
-
- Function DaysFrBase(CurrMM,CurrDD,CurrYY:LongInt):LongInt;
- Const
- Days1 : Array[1..12] of Integer=(0,31,59,90,120,151,181,212,243,273,304,334);
- Days2 : Array[1..12] of Integer=(0,31,60,91,121,152,182,213,244,274,305,335);
- Years1 : Array[0..3] of Integer=(0,365,730,1095);
- Years2 : Array[0..3] of Integer=(0,366,731,1096);
- CentArry : Array[0..3] of LongInt=(0,36525,73049,109573);
- Var
- MegYrs, CentYrs, QuadYrs, QuadDays,
- MnDays, SubDays, TotYrs, YrDays : LongInt;
- Begin
- QuadDays:= 0; {shouldn't need to be set, but have to}
- TotYrs:= CurrYY - BaseYr;
- MegYrs:= TotYrs div 400;
- TotYrs:= TotYrs - (MegYrs * 400);
- CentYrs:= TotYrs div 100;
- TotYrs:= TotYrs - (CentYrs * 100);
- QuadYrs:= TotYrs div 4;
- TotYrs:= TotYrs - (QuadYrs * 4);
- Case CentYrs of
- 0 : QuadDays:= QuadYrs * 1461;
- 1..3 : Case QuadYrs of
- 1 : QuadDays:= 1460;
- 2..24 : QuadDays:= (QuadYrs*1461) - 1;
- End;
- End;
- Case CentYrs of
- 0 : YrDays:= Years2[TotYrs];
- 1..3 : Case QuadYrs of
- 0 : YrDays:= Years1[TotYrs];
- 1..24 : YrDays:= Years2[TotYrs];
- End;
- End;
- SubDays:= (MegYrs * 146097) + CentArry[CentYrs] + QuadDays + YrDays;
- If LeapYear(CurrYY) Then MnDays:= Days2[CurrMM] Else MnDays:= Days1[CurrMM];
- DaysFrBase:= SubDays+MnDays+CurrDD;
- End;
-
- Procedure DateComp(Dte1,Dte2:Str10;Var TotDays:LongInt);
- Var
- mmi1, mmi2, ddi1, ddi2, yyi1, yyi2 : LongInt;
- Days1, Days2 : LongInt;
- Begin
- DateParse(Dte1,mmi1,ddi1,yyi1);
- DateParse(Dte2,mmi2,ddi2,yyi2);
- Days1:= DaysFrBase(mmi1,ddi1,yyi1);
- Days2:= DaysFrBase(mmi2,ddi2,yyi2);
- TotDays:= Days2 - Days1;
- End;
-
- Function ConvertDays(DaysInt:LongInt):Str10;
- Const
- MnArray1:Array[1..12] of Integer=(0,31,59,90,120,151,181,212,243,273,304,334);
- MnArray2:Array[1..12] of Integer=(0,31,60,91,121,152,182,213,244,274,305,335);
- Var
- MegYrs, CentYrs, QuadYrs,
- SingYrs, FinalYr, MMint : LongInt;
- MMstr, DDstr : String[2];
- YYstr : String[4];
- Begin
- YYstr:=''; MMstr:= ''; DDstr:= '';
- MegYrs:= DaysInt div 146097;
- DaysInt:= DaysInt - (MegYrs * 146097);
- If (MegYrs > 0) and (DaysInt = 0) Then
- Begin {mults of 146097 = 12/31/19(+4..)99, must have daysint=146097 to eval}
- Dec(MegYrs);
- DaysInt:= 146097;
- End;
- If DaysInt < 36526 Then CentYrs:= 0
- Else
- If DaysInt < 73050 Then
- Begin
- CentYrs:= 1;
- Dec(DaysInt,36525);
- End
- Else
- If DaysInt < 109574 Then
- Begin
- CentYrs:= 2;
- Dec(DaysInt,73049);
- End
- Else
- Begin
- CentYrs:= 3;
- Dec(DaysInt,109573);
- End;
- Case CentYrs of
- 0 : Begin
- QuadYrs:= DaysInt div 1461;
- DaysInt:= DaysInt - (QuadYrs * 1461);
- End;
- 1..3 : Case DaysInt of
- 0..1460 : QuadYrs:= 0;
- 1461..2921 : Begin
- QuadYrs:= 1;
- Dec(DaysInt,1460);
- End;
- Else Begin
- Dec(DaysInt,1460);
- QuadYrs:= DaysInt div 1461;
- DaysInt:= DaysInt - (QuadYrs * 1461);
- Inc(QuadYrs);
- End;
- End;
- End;
- If (QuadYrs = 25) and (DaysInt = 0) Then
- Begin {mults of 1461 = 12/31/16(+1..)99, must have daysint=1461 to eval}
- QuadYrs:= 24;
- DaysInt:= 1461;
- End;
- Case CentYrs of
- 0 : Case DaysInt of
- 0..366 : SingYrs:= 0;
- 367..731 : Begin
- SingYrs:= 1;
- Dec(DaysInt,366);
- End;
- 732..1096 : Begin
- SingYrs:= 2;
- Dec(DaysInt,731);
- End;
- Else Begin
- SingYrs:= 3;
- Dec(DaysInt,1096);
- End;
- End;
- 1..3 : Case QuadYrs of
- 0 : Case DaysInt of
- 0..365 : SingYrs:= 0;
- 366..730 : Begin
- SingYrs:= 1;
- Dec(DaysInt,365);
- End;
- 731..1095 : Begin
- SingYrs:= 2;
- Dec(DaysInt,730);
- End;
- Else Begin
- SingYrs:= 3;
- Dec(DaysInt,1095);
- End;
- End;
- 1..24 : Case DaysInt of
- 0..366 : SingYrs:= 0;
- 367..731 : Begin
- SingYrs:= 1;
- Dec(DaysInt,366);
- End;
- 731..1096 : Begin
- SingYrs:= 2;
- Dec(DaysInt,731);
- End;
- Else Begin
- SingYrs:= 3;
- Dec(DaysInt,1096);
- End;
- End;
- End;
- End;
- FinalYr:= BaseYr+(MegYrs * 400) + (CentYrs * 100) + (QuadYrs * 4) + SingYrs;
- Str(FinalYr,YYstr);
- If LeapYear(FinalYr) Then
- Begin
- Case DaysInt of
- 1..31 : MMint:= 1; {-0}
- 32..60 : MMint:= 2; {-31}
- 61..91 : MMint:= 3; {-60}
- 92..121 : MMint:= 4; {-91}
- 122..152 : MMint:= 5; {-121}
- 153..182 : MMint:= 6; {-152}
- 183..213 : MMint:= 7; {-182}
- 214..244 : MMint:= 8; {-213}
- 245..274 : MMint:= 9; {-244}
- 275..305 : MMint:= 10; {-274}
- 306..335 : MMint:= 11; {-305}
- 336..366 : MMint:= 12; {-335}
- End;
- DaysInt:= DaysInt - MnArray2[MMint];
- End
- Else
- Begin
- Case DaysInt of
- 1..31 : MMint:= 1; {-0}
- 32..59 : MMint:= 2; {-31}
- 60..90 : MMint:= 3; {-59}
- 91..120 : MMint:= 4; {-90}
- 121..151 : MMint:= 5; {-120}
- 152..181 : MMint:= 6; {-151}
- 182..212 : MMint:= 7; {-181}
- 213..243 : MMint:= 8; {-212}
- 244..273 : MMint:= 9; {-243}
- 274..304 : MMint:= 10; {-273}
- 305..334 : MMint:= 11; {-304}
- 335..365 : MMint:= 12; {-334}
- End;
- DaysInt:= DaysInt - MnArray1[MMint];
- End;
- Str(MMint,MMstr);
- If MMint < 10 Then MMstr:= '0' + MMstr;
- Str(DaysInt,DDstr);
- If DaysInt < 10 Then DDstr:= '0' + DDstr;
- ConvertDays:= MMstr + '/' + DDstr + '/' + YYstr;
- End;
-
- Function DateAdd(Dte1:Str10;AddDays:LongInt):Str10;
- Var
- OrigDays, NewDays,
- MMi, DDi, YYi : LongInt;
- Begin
- DateParse(Dte1,MMi,DDi,YYi);
- OrigDays:= DaysFrBase(MMi,DDi,YYi);
- NewDays:= OrigDays + AddDays;
- DateAdd:= ConvertDays(NewDays);
- End;
-
- Function DowInt(Dte1:Str10):LongInt;
- Var
- mmi, ddi, yyi : LongInt;
- Days : LongInt;
- Begin
- DateParse(Dte1,mmi,ddi,yyi);
- Days:= DaysFrBase(mmi,ddi,yyi) + 5;
- DowInt:= Days mod 7;
- End;
-
- Function DayName(Dte1:Str10):Str3;
- Const
- DayStr : Array[0..6] of Str3 = ('Sun','Mon','Tue','Wed','Thr','Fri','Sat');
- Var
- I : LongInt;
- Begin
- I:= DowInt(Dte1);
- DayName:= DayStr[I]
- End;
-
- Function WeekOf(Dte1:Str10;LastDay:LongInt):Str10;
- Var { Caution: }
- Days, Accum, { won't work on first days in }
- MMi, DDi, YYi : LongInt; { year 1600 if weekof falls in }
- Begin { year 1599 }
- DateParse(Dte1,MMi,DDi,YYi);
- Days:= DaysFrBase(MMi,DDi,YYi);
- Accum:= (Days + LastDay) - DowInt(Dte1);
- WeekOf:= ConvertDays(Accum);
- End;
-
- End{Unit}.