function TryEncodeTime(Hour, Min, Sec, MSec: Word; out Time: TDateTime): Boolean;
begin
Result := False;
if (Hour < HoursPerDay) and (Min < MinsPerHour) and (Sec < SecsPerMin) and (MSec < MSecsPerSec) then
begin
Time := (Hour * (MinsPerHour * SecsPerMin * MSecsPerSec) +
Min * (SecsPerMin * MSecsPerSec) +
Sec * MSecsPerSec +
MSec) / MSecsPerDay;
Result := True;
end;
end;
function TryEncodeDate(Year, Month, Day: Word; out Date: TDateTime): Boolean;
var
I: Integer;
DayTable: PDayTable;
begin
Result := False;
DayTable := @MonthDays[IsLeapYear(Year)];
if (Year >= 1) and (Year <= 9999) and (Month >= 1) and (Month <= 12) and
(Day >= 1) and (Day <= DayTable^[Month]) then
begin
for I := 1 to Month - 1 do Inc(Day, DayTable^[I]);
I := Year - 1;
Date := I * 365 + I div 4 - I div 100 + I div 400 + Day - DateDelta;
Result := True;
end;
end;
非常简单的2个函数,说明了Delphi TDateTime的本质。
Function XXXNow(): TDateTime;
var
t: TSystemTime;
Begin
GetLocalTime(t);
Result := EncodeDate(t.wYear, t.wMonth, t.wDay) + EnCodeTime(t.wHour, t.wMinute, t.wSecond, t.wMilliseconds);
end;
function IsValidIdent(const Ident: string): Boolean;
const
Alpha = ['A'..'Z', 'a'..'z', '_'];
AlphaNumeric = Alpha + ['0'..'9'];
var
I: Integer;
begin
Result := False;
if (Length(Ident) = 0) or not (Ident[1] in Alpha) then Exit;
for I := 2 to Length(Ident) do if not (Ident[I] in AlphaNumeric) then Exit;
Result := True;
end;
function XXXUpCase(ch: Char): Char;
Begin
Result := ch;
case Result of
'a'..'z': dec(Result, Ord('a') - Ord('A'));
end;
end;