* Patch from Alex Rayne (bug ID 14622) to provide some overloaded versions of the strtodate/strtotime functions

git-svn-id: trunk@13864 -
This commit is contained in:
michael 2009-10-16 08:16:55 +00:00
parent f68faa154d
commit dadd6631ad
2 changed files with 291 additions and 62 deletions

View File

@ -326,7 +326,8 @@ end ;
if S does not represent a valid date value if S does not represent a valid date value
an EConvertError will be raised } an EConvertError will be raised }
function StrToDate(const S: string): TDateTime; function StrToDate(const S: PChar; Len : integer; const useformat : string; separator : char = #0): TDateTime;
const SInvalidDateFormat = '"%s" is not a valid date format'; const SInvalidDateFormat = '"%s" is not a valid date format';
var var
df:string; df:string;
@ -343,7 +344,9 @@ begin
Raise EConvertError.CreateFmt(SInvalidDateFormat,[s]); Raise EConvertError.CreateFmt(SInvalidDateFormat,[s]);
YearMoreThenTwoDigits := False; YearMoreThenTwoDigits := False;
df := UpperCase(ShortDateFormat); if separator = #0 then
separator := DateSeparator;
df := UpperCase(useFormat);
{ Determine order of D,M,Y } { Determine order of D,M,Y }
yp:=0; yp:=0;
mp:=0; mp:=0;
@ -381,7 +384,8 @@ begin
values[i] := 0; values[i] := 0;
s1 := ''; s1 := '';
n := 0; n := 0;
for i := 1 to length(s) do dec(len);
for i := 0 to len do
begin begin
if s[i] in ['0'..'9'] then if s[i] in ['0'..'9'] then
s1 := s1 + s[i]; s1 := s1 + s[i];
@ -389,10 +393,10 @@ begin
{ space can be part of the shortdateformat, and is defaultly in slovak { space can be part of the shortdateformat, and is defaultly in slovak
windows, therefor it shouldn't be taken as separator (unless so specified) windows, therefor it shouldn't be taken as separator (unless so specified)
and ignored } and ignored }
if (DateSeparator <> ' ') and (s[i] = ' ') then if (Separator <> ' ') and (s[i] = ' ') then
Continue; Continue;
if (s[i] = dateseparator) or ((i = length(s)) and (s[i] in ['0'..'9'])) then if (s[i] = separator) or ((i = len) and (s[i] in ['0'..'9'])) then
begin begin
inc(n); inc(n);
if n>3 then if n>3 then
@ -446,41 +450,82 @@ begin
Result := EncodeDate(y, m, d); Result := EncodeDate(y, m, d);
end ; end ;
function StrToDate(const S: ShortString; const useformat : string; separator : char = #0): TDateTime;
begin
result := StrToDate(@S[1],Length(s),UseFormat,separator);
end;
function StrToDate(const S: AnsiString; const useformat : string; separator : char = #0): TDateTime;
begin
result := StrToDate(@S[1],Length(s),UseFormat,separator);
end;
function StrToDate(const S: ShortString; separator : char): TDateTime;
begin
result := StrToDate(@S[1],Length(s),ShortDateFormat,separator)
end;
function StrToDate(const S: ShortString): TDateTime;
begin
result := StrToDate(@S[1],Length(s),ShortDateFormat,#0);
end;
function StrToDate(const S: AnsiString; separator : char): TDateTime;
begin
result := StrToDate(@S[1],Length(s),ShortDateFormat,separator)
end;
function StrToDate(const S: AnsiString): TDateTime;
begin
result := StrToDate(@S[1],Length(s),ShortDateFormat,#0);
end;
{ StrToTime converts the string S to a TDateTime value { StrToTime converts the string S to a TDateTime value
if S does not represent a valid time value an if S does not represent a valid time value an
EConvertError will be raised } EConvertError will be raised }
function StrToTime(const s: string): TDateTime; function StrToTime(const S: PChar; Len : integer; separator : char = #0): TDateTime;
var var
Len, Current: integer; PM: integer; Current: integer; PM: integer;
function StrPas(Src : PChar; len: integer = 0) : ShortString;
var
tmp : integer;
begin
{tmp := IndexChar(Src[0], len, #0);
len :=ifthen(tmp >= 0, tmp, len);
len :=ifthen(len > 255, 255, len);}
SetLength(Result, len);
move(src[0],result[1],len);
end;
function GetElement: integer; function GetElement: integer;
var var
j, c: integer; j, c: integer;
CurrentChar : Char;
begin begin
result := -1; result := -1;
Inc(Current); while (result = -1) and (Current < Len) do
while (result = -1) and (Current <= Len) do
begin begin
if S[Current] in ['0'..'9'] then CurrentChar := S[Current];
if CurrentChar in ['0'..'9'] then
begin begin
j := Current; j := Current;
while (Current < Len) and (s[Current + 1] in ['0'..'9']) do while (Current+1 < Len) and (s[Current + 1] in ['0'..'9']) do
Inc(Current); Inc(Current);
val(copy(S, j, 1 + Current - j), result, c); val(StrPas(S+j, 1 + current - j), result, c);
end end
else if ((TimeAMString<>'') and (S[Current] = TimeAMString[1])) or (S[Current] in ['a', 'A']) then else if ((TimeAMString<>'') and (CurrentChar = TimeAMString[1])) or (S[Current] in ['a', 'A']) then
begin begin
pm:=1; pm:=1;
Current := 1 + Len; Current := 1 + Len;
end end
else if ((TimePMString<>'') and (S[Current] = TimePMString[1])) or (S[Current] in ['p', 'P']) then else if ((TimePMString<>'') and (CurrentChar = TimePMString[1])) or (S[Current] in ['p', 'P']) then
begin begin
Current := 1 + Len; Current := 1 + Len;
PM := 2; PM := 2;
end end
else if (S[Current] = TimeSeparator) or (S[Current] = ' ') then else if (CurrentChar = Separator) or (CurrentChar = ' ') then
Inc(Current) Inc(Current)
else else
raise EConvertError.Create('Invalid Time format'); raise EConvertError.Create('Invalid Time format');
@ -492,8 +537,9 @@ var
TimeValues: array[0..4] of integer; TimeValues: array[0..4] of integer;
begin begin
if separator = #0 then
separator := TimeSeparator;
Current := 0; Current := 0;
Len := length(s);
PM := 0; PM := 0;
for i:=0 to 4 do for i:=0 to 4 do
timevalues[i]:=0; timevalues[i]:=0;
@ -502,6 +548,7 @@ begin
while (i < 5) and (TimeValues[i] <> -1) do while (i < 5) and (TimeValues[i] <> -1) do
begin begin
i := i + 1; i := i + 1;
Inc(Current);
TimeValues[i] := GetElement; TimeValues[i] := GetElement;
end ; end ;
If (i<5) and (TimeValues[I]=-1) then If (i<5) and (TimeValues[I]=-1) then
@ -519,40 +566,79 @@ begin
result := EncodeTime(TimeValues[0], TimeValues[1], TimeValues[2], TimeValues[3]); result := EncodeTime(TimeValues[0], TimeValues[1], TimeValues[2], TimeValues[3]);
end ; end ;
function StrToTime(const s: ShortString; separator : char): TDateTime;
begin
result := StrToTime(@s[1], length(s), separator);
end;
function StrToTime(const s: AnsiString; separator : char): TDateTime;
begin
result := StrToTime(@s[1], length(s), separator);
end;
function StrToTime(const s: ShortString): TDateTime;
begin
result := StrToTime(@s[1], length(s), #0);
end;
function StrToTime(const s: AnsiString): TDateTime;
begin
result := StrToTime(@s[1], length(s), #0);
end;
{ StrToDateTime converts the string S to a TDateTime value { StrToDateTime converts the string S to a TDateTime value
if S does not represent a valid date and/or time value if S does not represent a valid date and/or time value
an EConvertError will be raised } an EConvertError will be raised }
function StrToDateTime(const s: string): TDateTime; function StrToDateTime(const s: string): TDateTime;
var var
i, j, k, l: integer; i: integer;
sd, st: string;
begin begin
l := Length(s); i := Pos(' ',s);
i := 1; if i > 0 then begin
while (i <= l) and (s[i] = ' ') do result := ComposeDateTime(StrToDate(@S[1], i - 1, ShortDateFormat, DateSeparator)
Inc(i); , StrToTime(@S[i+1], Length(S)-i , TimeSeparator));
j := i; end
while (j <= l) and (s[j] <> ' ') do else if pos(TimeSeparator,s) > 0 then
Inc(j); result := StrToTime(s,TimeSeparator)
k := j;
while (k <= l) and (s[k] = ' ') do
Inc(k);
sd := Copy(s, i, j - i);
st := Copy(s, k, l);
if (st = '') and (Pos(TimeSeparator, sd) > 0) then
begin
st := sd;
sd := '';
end;
if (sd <> '') and (st <> '') then
Result := ComposeDateTime(StrToDate(sd), StrToTime(st))
else if st = '' then
Result := StrToDate(sd)
else else
Result := StrToTime(st); result := StrToDate(s,LongDateFormat, DateSeparator)
end ; end ;
function StrToDateTime(const s: ShortString; const UseFormat : TFormatSettings): TDateTime;
var
i : integer;
begin
with useformat do begin
i := Pos(' ',s);
if i > 0 then begin
result := ComposeDateTime(StrToDate(@S[1], i - 1, ShortDateFormat, DateSeparator)
, StrToTime(@S[i+1], Length(S)-i , TimeSeparator));
end
else if pos(TimeSeparator,s) > 0 then
result := StrToTime(s,TimeSeparator)
else
result := StrToDate(s, ShortDateFormat, DateSeparator)
end;
end;
function StrToDateTime(const s: AnsiString; const UseFormat : TFormatSettings): TDateTime;
var
i : integer;
begin
with useformat do begin
i := Pos(' ',s);
if i > 0 then begin
result := ComposeDateTime(StrToDate(@S[1], i - 1, ShortDateFormat, DateSeparator)
, StrToTime(@S[i+1], Length(S)-i , TimeSeparator));
end
else if pos(TimeSeparator,s) > 0 then
result := StrToTime(s,TimeSeparator)
else
result := StrToDate(s,ShortDateFormat, DateSeparator)
end;
end;
{ FormatDateTime formats DateTime to the given format string FormatStr } { FormatDateTime formats DateTime to the given format string FormatStr }
function FormatDateTime(FormatStr: string; DateTime: TDateTime): string; function FormatDateTime(FormatStr: string; DateTime: TDateTime): string;
@ -819,11 +905,56 @@ end;
{$endif unix} {$endif unix}
// ieuw. These should be written to work without exceptions? // ieuw. These should be written to work without exceptions?
function TryStrToDate(const S: string; out Value: TDateTime): Boolean; function TryStrToDate(const S: ShortString; out Value: TDateTime): Boolean;
begin
result := TryStrToDate(S, Value, #0);
end;
function TryStrToDate(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
begin begin
result:=true; result:=true;
try try
value:=StrToDate(s); value:=StrToDate(s, separator);
except
on EConvertError do
result:=false
end;
end;
function TryStrToDate(const S: ShortString; out Value: TDateTime;
const useformat : string; separator : char = #0): Boolean;
begin
result:=true;
try
value:=StrToDate(s, useformat, separator);
except
on EConvertError do
result:=false
end;
end;
function TryStrToDate(const S: AnsiString; out Value: TDateTime): Boolean;
begin
result := TryStrToDate(S, Value, #0);
end;
function TryStrToDate(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
begin
result:=true;
try
value:=StrToDate(s, separator);
except
on EConvertError do
result:=false
end;
end;
function TryStrToDate(const S: AnsiString; out Value: TDateTime;
const useformat : string; separator : char = #0): Boolean;
begin
result:=true;
try
value:=StrToDate(s, useformat, separator);
except except
on EConvertError do on EConvertError do
result:=false result:=false
@ -833,23 +964,42 @@ function TryStrToDate(const S: string; out Value: TDateTime): Boolean;
// function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean; // function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
function TryStrToTime(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
function TryStrToTime(const S: string; out Value: TDateTime): Boolean;
begin begin
result:=true; result:=true;
try try
value:=StrToTime(s); value:=StrToTime(s, separator);
except except
on EConvertError do on EConvertError do
result:=false result:=false
end; end;
end; end;
function TryStrToTime(const S: ShortString; out Value: TDateTime): Boolean;
begin
result := TryStrToTime(S,Value,#0);
end;
function TryStrToTime(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
begin
result:=true;
try
value:=StrToTime(s, separator);
except
on EConvertError do
result:=false
end;
end;
function TryStrToTime(const S: AnsiString; out Value: TDateTime): Boolean;
begin
result := TryStrToTime(S,Value,#0);
end;
// function TryStrToTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean; // function TryStrToTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean; function TryStrToDateTime(const S: ShortString; out Value: TDateTime): Boolean;
begin begin
result:=true; result:=true;
try try
@ -860,28 +1010,76 @@ function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean;
end; end;
end; end;
function TryStrToDateTime(const S: AnsiString; out Value: TDateTime): Boolean;
begin
result:=true;
try
value:=StrToDateTime(s);
except
on EConvertError do
result:=false
end;
end;
// function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean; // function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
function StrToDateDef(const S: string; const Defvalue : TDateTime): TDateTime; function StrToDateDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
begin begin
if not TryStrToDate(s,Result) Then result := StrToDateDef(S,DefValue,#0);
result:=defvalue;
end; end;
function StrToTimeDef(const S: string; const Defvalue : TDateTime): TDateTime; function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
begin begin
if not TryStrToTime(s,Result) Then result := StrToTimeDef(S,DefValue,#0);
result:=defvalue;
end; end;
function StrToDateTimeDef(const S: string; const Defvalue : TDateTime): TDateTime; function StrToDateTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
begin begin
if not TryStrToDateTime(s,Result) Then if not TryStrToDateTime(s,Result) Then
result:=defvalue; result:=defvalue;
end; end;
function StrToDateDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime;
begin
if not TryStrToDate(s,Result, separator) Then
result:=defvalue;
end;
function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime;
begin
if not TryStrToTime(s,Result, separator) Then
result:=defvalue;
end;
function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
begin
result := StrToDateDef(S,DefValue,#0);
end;
function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
begin
result := StrToTimeDef(S,DefValue,#0);
end;
function StrToDateTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
begin
if not TryStrToDateTime(s,Result) Then
result:=defvalue;
end;
function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime;
begin
if not TryStrToDate(s,Result, separator) Then
result:=defvalue;
end;
function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime;
begin
if not TryStrToTime(s,Result, separator) Then
result:=defvalue;
end;
procedure ReplaceTime(var dati:TDateTime; NewTime : TDateTime);inline; procedure ReplaceTime(var dati:TDateTime; NewTime : TDateTime);inline;
begin begin
dati:= ComposeDateTime(dati, newtime); dati:= ComposeDateTime(dati, newtime);

View File

@ -124,24 +124,55 @@ function IsLeapYear(Year: Word): boolean;
function DateToStr(Date: TDateTime): string; function DateToStr(Date: TDateTime): string;
function TimeToStr(Time: TDateTime): string; function TimeToStr(Time: TDateTime): string;
function DateTimeToStr(DateTime: TDateTime): string; function DateTimeToStr(DateTime: TDateTime): string;
function StrToDate(const S: string): TDateTime; function StrToDate(const S: ShortString): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToTime(const S: string): TDateTime; function StrToDate(const S: Ansistring): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToDate(const S: ShortString; separator : char): TDateTime;{$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToDate(const S: AnsiString; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToTime(const S: Shortstring): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToTime(const S: Ansistring): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToTime(const S: ShortString; separator : char): TDateTime;{$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToTime(const S: AnsiString; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToDate(const S: ShortString; const useformat : string; separator : char): TDateTime;{$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToDate(const S: AnsiString; const useformat : string; separator : char): TDateTime;{$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToTime(const S: PChar; Len : integer; separator : char = #0): TDateTime;
function StrToDate(const S: PChar; Len : integer; const useformat : string; separator : char = #0): TDateTime;
function StrToDateTime(const S: string): TDateTime; function StrToDateTime(const S: string): TDateTime;
function StrToDateTime(const s: ShortString; const UseFormat : TFormatSettings): TDateTime;
function StrToDateTime(const s: AnsiString; const UseFormat : TFormatSettings): TDateTime;
function FormatDateTime(FormatStr: string; DateTime: TDateTime):string; function FormatDateTime(FormatStr: string; DateTime: TDateTime):string;
procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime); procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime);
Function DateTimeToFileDate(DateTime : TDateTime) : Longint; Function DateTimeToFileDate(DateTime : TDateTime) : Longint;
Function FileDateToDateTime (Filedate : Longint) :TDateTime; Function FileDateToDateTime (Filedate : Longint) :TDateTime;
function TryStrToDate(const S: string; out Value: TDateTime): Boolean; function TryStrToDate(const S: ShortString; out Value: TDateTime): Boolean; {$ifdef SYSUTILSINLINE}inline;{$endif}
function TryStrToTime(const S: string; out Value: TDateTime): Boolean; function TryStrToDate(const S: AnsiString; out Value: TDateTime): Boolean; {$ifdef SYSUTILSINLINE}inline;{$endif}
function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean; function TryStrToDate(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
function TryStrToDate(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
function TryStrToTime(const S: ShortString; out Value: TDateTime): Boolean; {$ifdef SYSUTILSINLINE}inline;{$endif}
function TryStrToTime(const S: AnsiString; out Value: TDateTime): Boolean; {$ifdef SYSUTILSINLINE}inline;{$endif}
function TryStrToTime(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
function TryStrToTime(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
function TryStrToDate(const S: ShortString; out Value: TDateTime;
const useformat : string; separator : char = #0): Boolean;
function TryStrToDate(const S: AnsiString; out Value: TDateTime;
const useformat : string; separator : char = #0): Boolean;
function TryStrToDateTime(const S: ShortString; out Value: TDateTime): Boolean;
function TryStrToDateTime(const S: AnsiString; out Value: TDateTime): Boolean;
// function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean; // function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
// function TryStrToTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean; // function TryStrToTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
// function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean; // function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
function StrToDateDef(const S: string; const Defvalue : TDateTime): TDateTime; function StrToDateDef(const S: ShortString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToTimeDef(const S: string; const Defvalue : TDateTime): TDateTime; function StrToDateDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToDateTimeDef(const S: string; const Defvalue : TDateTime): TDateTime; function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToDateTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToDateTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function CurrentYear:Word; function CurrentYear:Word;
{ FPC Extra } { FPC Extra }