* faster En/Decodedate routines from Frank Reichert

* Fixed FormatDateTime with short/longtimeformat.
This commit is contained in:
michael 1999-07-14 08:47:54 +00:00
parent 45233bf8bb
commit 727d5d3100

View File

@ -31,16 +31,21 @@ const
(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335)); (0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335));
function DoEncodeDate(Year, Month, Day: Word): longint; function DoEncodeDate(Year, Month, Day: Word): longint;
var i: longint; var c, ya: word;
begin begin
Result := 0; if (Month > 0) and (Month < 13) and (Day > 0) and (Day < 32) then
if (Year >= 1) and (Year <= 9999) and (Month >= 1) and (Month <= 12) and begin
(Day >= 1) and (Day <= 31) then begin if month > 2 then Month := Month - 3 else
Day := Day + DayTable[IsLeapYear(Year), Month] - 1; begin
I := Year - 1; Month := Month + 9;
result := I * 365 + I div 4 - I div 100 + I div 400 + Day - DateDelta; Year:= Year - 1;
end ; end;
end ; c:= Year DIV 100;
ya:= Year - 100*c;
result := (146097*c) SHR 2 + (1461*ya) SHR 2 + (153*Month+2) DIV 5 + Day -
693901;
end else result:=0;
end;
function DoEncodeTime(Hour, Minute, Second, MilliSecond: word): longint; function DoEncodeTime(Hour, Minute, Second, MilliSecond: word): longint;
begin begin
@ -112,38 +117,41 @@ end ;
Year, Month and Day } Year, Month and Day }
procedure DecodeDate(Date: TDateTime; var Year, Month, Day: word); procedure DecodeDate(Date: TDateTime; var Year, Month, Day: word);
const
D1 = 365; { number of days in 1 year }
D4 = D1 * 4 + 1; { number of days in 4 years }
D100 = D4 * 25 - 1; { number of days in 100 years }
D400 = D100 * 4 + 1; { number of days in 400 years }
var var
l:longint; j: word;
ly:boolean;
begin begin
l := Trunc(System.Int(Date)) + DateDelta; j := pred((Trunc(System.Int(Date)) + 693901) SHL 2);
year := 1 + 400 * (l div D400); l := (l mod D400); Year:= j DIV 146097;
year := year + 100 * (l div D100);l := (l mod D100); j:= j - 146097 * Year;
year := year + 4 * (l div D4);l := (l mod D4); Day := j SHR 2;
year := year + (l div D1);l := 1 + (l mod D1); j:=(Day SHL 2 + 3) DIV 1461;
month := 0; Day:= (Day SHL 2 + 7 - 1461*j) SHR 2;
ly := IsLeapYear(Year); Month:=(5 * Day-3) DIV 153;
while (month < 12) and (l > DayTable[ly, month + 1]) do Day:= (5 * Day +2 - 153*Month) DIV 5;
inc(month); Year:= 100 * Year + j;
day := l - DayTable[ly, month]; if Month < 10 then Month:= Month + 3 else begin
Month:= Month-9;
inc(Year);
end;
end ; end ;
{ DecodeTime unpacks Time into four values: { DecodeTime unpacks Time into four values:
Hour, Minute, Second and MilliSecond } Hour, Minute, Second and MilliSecond }
procedure DecodeTime(Time: TDateTime; var Hour, Minute, Second, MilliSecond: word); procedure DecodeTime(Time: TDateTime; var Hour, Minute, Second, MilliSecond: word);
Var l : longint;
begin begin
{ l := Trunc(Frac(time) * MSecsPerDay); l := Trunc(Frac(time) * MSecsPerDay);
Hour := l div 3600000;l := l mod 3600000; Hour := l div 3600000;
Minute := l div 60000;l := l mod 60000; l := l mod 3600000;
Second := l div 1000;l := l mod 1000; Minute := l div 60000;
l := l mod 60000;
Second := l div 1000;
l := l mod 1000;
MilliSecond := l; MilliSecond := l;
} {
Time := Frac(Time) * 24; Time := Frac(Time) * 24;
Hour := Trunc(Time); Hour := Trunc(Time);
Time := Frac(Time) * 60; Time := Frac(Time) * 60;
@ -151,6 +159,7 @@ begin
Time := Frac(Time) * 60; Time := Frac(Time) * 60;
Second := Trunc(Time); Second := Trunc(Time);
MilliSecond := Trunc(Frac(Time) * 1000); MilliSecond := Trunc(Frac(Time) * 1000);
}
end; end;
{ DateTimeToSystemTime converts DateTime value to SystemTime } { DateTimeToSystemTime converts DateTime value to SystemTime }
@ -452,6 +461,17 @@ var
end ; end ;
StoreStr(pchar(@S[1]), Len); StoreStr(pchar(@S[1]), Len);
end ; end ;
Function TimeReFormat(Const S : string) : string;
// Change m into n for time formatting.
Var i : longint;
begin
Result:=S;
For I:=1 to Length(Result) do
If Result[i]='m' then
result[i]:='n';
end;
var var
Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word; Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word;
@ -570,10 +590,10 @@ var
else StoreInt(Second, 2); else StoreInt(Second, 2);
end ; end ;
'T': begin 'T': begin
if Count = 1 then StoreFormat(ShortTimeFormat) if Count = 1 then StoreFormat(timereformat(ShortTimeFormat))
else StoreFormat(LongTimeFormat); else StoreFormat(TimeReformat(LongTimeFormat));
end ; end ;
'C': StoreFormat(ShortDateFormat + ' ' + ShortTimeFormat); 'C': StoreFormat(ShortDateFormat + ' ' + TimeReformat(ShortTimeFormat));
end ; end ;
end ; end ;
else else
@ -632,7 +652,11 @@ end;
{ {
$Log$ $Log$
Revision 1.13 1999-05-31 20:50:44 peter Revision 1.14 1999-07-14 08:47:54 michael
* faster En/Decodedate routines from Frank Reichert
* Fixed FormatDateTime with short/longtimeformat.
Revision 1.13 1999/05/31 20:50:44 peter
* removed warnings * removed warnings
Revision 1.12 1999/05/13 21:51:41 michael Revision 1.12 1999/05/13 21:51:41 michael