OptEncodeDateI Function
A Speed Optimised EncodeDate developed by Ken Otto that is many times faster than the once included in SysUtils, and includes Exception Handling.

Unit
QESBPCSDateTime

Declaration
Function OptEncodeDateI(Year, Month, Day: Integer): TDateTime;

Description
If you need Words rather than Integers use the slightly slower OptEncodeDateW.

Category
Date/Time Conversion Routines

Implementation

function OptEncodeDateI (Year, Month, Day: Integer): TDateTime;
var
     DayTable: PDayTable;
begin
     if (Month < 1) or (Month > 12) then
          raise EConvertError.Create (rsInvalidMonth);

     DayTable := @MonthDays [IsLeapYear (Year)];
     if (Day <= DayTable^ [Month]) and (Year >= 1) and (Year < 10000) and
          (Month < 13) and (Day > 0) then
     begin
          if Month > 2 then
               Dec (Month, 3)
          else if (Month > 0) then
          begin
               Inc (Month, 9);
               Dec (Year);
          end
          else // Month <= 0
               raise EConvertError.Create (rsInvalidDate);

          Result := (146097 * (Year div 100)) shr 2 +
               (1461 * (Year - 100 * (Year div 100))) shr 2 +
               (153 * Month + 2) div 5 + Day - 693900;
     end
     else
          raise EConvertError.Create (rsInvalidDate + IntToStr (Year) + '-'
               + IntToStr (Month) + '-' + IntToStr (Day));
End;


HTML generated by Time2HELP
http://www.time2help.com