home *** CD-ROM | disk | FTP | other *** search
- Unit DateRout;
-
- { This set of routines is placed in the public domain by: }
- { John Wood }
- { 14720 Karyl Dr. }
- { Minnetonka, MN 55345 }
- { If you have any suggestions for improvements or }
- { additions, please write me at this address. Enjoy! }
- { }
- { I wrote these routines to circumvent the dos date }
- { limitation of 1980 thru 2100. I believe these routines }
- { correctly deal with Gregorian dates from year 1590 }
- { forward. The TdyDate function is the only routine }
- { that uses dos - I figured we'd probably have a new }
- { version of dos to determine today's date by year 2100... }
- { }
- { DateParse & DaysFrBase are really internal routines. I }
- { left the declarations in the public section to help }
- { with my own testing - you may want to make them private. }
-
- Interface
- Uses Dos;
- Type
- Str2 = String[2];
- Str3 = String[3];
- Str4 = String[4];
- Str8 = String[8];
- Str10 = String[10];
-
- Function TdyDate : Str10;
- Procedure DateParse(Date:Str10; Var mmi,ddi,yyi:LongInt);
- Function LeapYear(CurrYr:LongInt):Boolean;
- Function DaysFrBase(CurrMM,CurrDD,CurrYY:LongInt):LongInt;
- Procedure DateComp(Dte1,Dte2:Str10;Var TotDays:LongInt);
- Function ConvertDays(DaysInt:LongInt):Str10;
- 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 = 1590;
- { Note: Gregorian Calender started 10/15/1582 }
- Function TdyDate : Str10;
- Var
- Month, Day : Str2;
- Year : Str4;
- 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 : Str2;
- yy : Str4;
- 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);
- Years : Array[0..3] of Integer=(0,365,730,1096);
- Var
- CentYrs, QuadYrs, MnDays,
- SubDays, TotYrs : LongInt;
- Begin
- MnDays:= 0; CentYrs:= 0; QuadYrs:= 0; SubDays:= 0; TotYrs:= 0;
- TotYrs:= CurrYY - BaseYr;
- CentYrs:= TotYrs div 400;
- TotYrs:= TotYrs - (CentYrs * 400);
- QuadYrs:= TotYrs div 4;
- TotYrs:= TotYrs - (QuadYrs * 4);
- SubDays:= (CentYrs * 146097) + (QuadYrs * 1461) + Years[TotYrs];
- 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
- mmi1:= 0; mmi2:= 0; ddi1:= 0; ddi2:= 0; yyi1:= 0; yyi2:= 0;
- TotDays:= 0; Days1:= 0; Days2:= 0;
- 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
- QuadYrs, CentYrs,
- FinalYr, MMint, DDint : LongInt;
- MMstr, DDstr : Str2;
- YYstr : Str4;
- Begin
- YYstr:=''; MMstr:= ''; DDstr:= ''; DDint:= 0;
- CentYrs:= DaysInt div 146097;
- DaysInt:= DaysInt - (CentYrs * 146097);
- QuadYrs:= DaysInt div 1461;
- DaysInt:= DaysInt - (QuadYrs * 1461);
- Case DaysInt of
- 0 : Begin
- FinalYr:= BaseYr - 1;
- DaysInt:= 365;
- End;
- 1..365 : FinalYr:= BaseYr;
- 366..730 : Begin
- FinalYr:= BaseYr + 1;
- DaysInt:= DaysInt - 365;
- End;
- 731..1096 : Begin
- FinalYr:= BaseYr + 2;
- DaysInt:= DaysInt - 730;
- End;
- 1097..1461 : Begin
- FinalYr:= BaseYr + 3;
- DaysInt:= DaysInt - 1096;
- End;
- End;
- FinalYr:= FinalYr + (CentYrs * 400) + (QuadYrs * 4);
- 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;
- DDint:= DaysInt;
- Str(MMint,MMstr);
- If MMint < 10 Then MMstr:= '0'+MMstr;
- Str(DDint,DDstr);
- If DDint < 10 Then DDstr:= '0'+DDstr;
- ConvertDays:= MMstr+'/'+DDstr+'/'+YYstr;
- End;
-
- Function DateAdd(Dte1:Str10;AddDays:LongInt):Str10;
- Var
- OrigDays, NewDays : LongInt;
- MMi, DDi, YYi : LongInt;
- Begin
- OrigDays:= 0; NewDays:= 0; MMi:= 0; DDi:= 0; YYi:= 0;
- 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)+4; {+4 is adjustment for BaseYr = 1590}
- 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
- Days, Accum : LongInt;
- MMi, DDi, YYi : LongInt;
- Begin
- Days:= 0; Accum:= 0;
- DateParse(Dte1,MMi,DDi,YYi);
- Days:= DaysFrBase(MMi,DDi,YYi);
- Accum:= (Days + LastDay) - DowInt(Dte1);
- WeekOf:= ConvertDays(Accum);
- End;
-
- End. {of unit}
-