mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-06-06 09:38:23 +02:00
* TryStrToTime/TryStrToDate no longer need to catch exceptions
git-svn-id: trunk@13865 -
This commit is contained in:
parent
dadd6631ad
commit
cb29ddbd04
@ -326,7 +326,7 @@ 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: PChar; Len : integer; const useformat : string; separator : char = #0): TDateTime;
|
function IntStrToDate(Out ErrorMsg : AnsiString; 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
|
||||||
@ -340,9 +340,12 @@ var
|
|||||||
LocalTime:tsystemtime;
|
LocalTime:tsystemtime;
|
||||||
YearMoreThenTwoDigits : boolean;
|
YearMoreThenTwoDigits : boolean;
|
||||||
begin
|
begin
|
||||||
if s = '' then
|
ErrorMsg:='';
|
||||||
Raise EConvertError.CreateFmt(SInvalidDateFormat,[s]);
|
if (Len=0) then
|
||||||
|
begin
|
||||||
|
ErrorMsg:=Format(SInvalidDateFormat,['']);
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
YearMoreThenTwoDigits := False;
|
YearMoreThenTwoDigits := False;
|
||||||
if separator = #0 then
|
if separator = #0 then
|
||||||
separator := DateSeparator;
|
separator := DateSeparator;
|
||||||
@ -378,7 +381,10 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
if Which<>3 then
|
if Which<>3 then
|
||||||
Raise EConvertError.Create('Illegal format string');
|
begin
|
||||||
|
ErrorMsg:=Format(SErrIllegalDateFormatString,[useformat]);
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
{ Get actual values }
|
{ Get actual values }
|
||||||
for i := 1 to 3 do
|
for i := 1 to 3 do
|
||||||
values[i] := 0;
|
values[i] := 0;
|
||||||
@ -400,16 +406,25 @@ begin
|
|||||||
begin
|
begin
|
||||||
inc(n);
|
inc(n);
|
||||||
if n>3 then
|
if n>3 then
|
||||||
Raise EConvertError.CreateFmt(SInvalidDateFormat,[s]);
|
begin
|
||||||
|
ErrorMsg:=Format(SInvalidDateFormat,[s]);
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
// Check if the year has more then two digits (if n=yp, then we are evaluating the year.)
|
// Check if the year has more then two digits (if n=yp, then we are evaluating the year.)
|
||||||
if (n=yp) and (length(s1)>2) then YearMoreThenTwoDigits := True;
|
if (n=yp) and (length(s1)>2) then YearMoreThenTwoDigits := True;
|
||||||
val(s1, values[n], c);
|
val(s1, values[n], c);
|
||||||
if c<>0 then
|
if c<>0 then
|
||||||
Raise EConvertError.CreateFmt(SInvalidDateFormat,[s]);
|
begin
|
||||||
|
ErrorMsg:=Format(SInvalidDateFormat,[s]);
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
s1 := '';
|
s1 := '';
|
||||||
end
|
end
|
||||||
else if not (s[i] in ['0'..'9']) then
|
else if not (s[i] in ['0'..'9']) then
|
||||||
Raise EConvertError.CreateFmt(SInvalidDateFormat,[s]);
|
begin
|
||||||
|
ErrorMsg:=Format(SInvalidDateFormat,[s]);
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
end ;
|
end ;
|
||||||
// Fill in values.
|
// Fill in values.
|
||||||
getLocalTime(LocalTime);
|
getLocalTime(LocalTime);
|
||||||
@ -450,6 +465,17 @@ begin
|
|||||||
Result := EncodeDate(y, m, d);
|
Result := EncodeDate(y, m, d);
|
||||||
end ;
|
end ;
|
||||||
|
|
||||||
|
function StrToDate(const S: PChar; Len : integer; const useformat : string; separator : char = #0): TDateTime;
|
||||||
|
|
||||||
|
Var
|
||||||
|
MSg : AnsiString;
|
||||||
|
|
||||||
|
begin
|
||||||
|
Result:=IntStrToDate(Msg,S,Len,useFormat,Separator);
|
||||||
|
If (Msg<>'') then
|
||||||
|
EConvertError.Create(Msg);
|
||||||
|
end;
|
||||||
|
|
||||||
function StrToDate(const S: ShortString; const useformat : string; separator : char = #0): TDateTime;
|
function StrToDate(const S: ShortString; const useformat : string; separator : char = #0): TDateTime;
|
||||||
begin
|
begin
|
||||||
result := StrToDate(@S[1],Length(s),UseFormat,separator);
|
result := StrToDate(@S[1],Length(s),UseFormat,separator);
|
||||||
@ -484,7 +510,7 @@ end;
|
|||||||
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: PChar; Len : integer; separator : char = #0): TDateTime;
|
function IntStrToTime(Out ErrorMsg : AnsiString; const S: PChar; Len : integer; separator : char = #0): TDateTime;
|
||||||
var
|
var
|
||||||
Current: integer; PM: integer;
|
Current: integer; PM: integer;
|
||||||
|
|
||||||
@ -528,7 +554,7 @@ var
|
|||||||
else if (CurrentChar = Separator) or (CurrentChar = ' ') then
|
else if (CurrentChar = Separator) or (CurrentChar = ' ') then
|
||||||
Inc(Current)
|
Inc(Current)
|
||||||
else
|
else
|
||||||
raise EConvertError.Create('Invalid Time format');
|
ErrorMsg:=Format(SErrInvalidTimeFormat,[StrPas(S)]);
|
||||||
end ;
|
end ;
|
||||||
end ;
|
end ;
|
||||||
|
|
||||||
@ -545,11 +571,15 @@ begin
|
|||||||
timevalues[i]:=0;
|
timevalues[i]:=0;
|
||||||
i := 0;
|
i := 0;
|
||||||
TimeValues[i] := GetElement;
|
TimeValues[i] := GetElement;
|
||||||
|
If ErrorMsg<>'' then
|
||||||
|
Exit;
|
||||||
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);
|
Inc(Current);
|
||||||
TimeValues[i] := GetElement;
|
TimeValues[i] := GetElement;
|
||||||
|
If ErrorMsg<>'' then
|
||||||
|
Exit;
|
||||||
end ;
|
end ;
|
||||||
If (i<5) and (TimeValues[I]=-1) then
|
If (i<5) and (TimeValues[I]=-1) then
|
||||||
TimeValues[I]:=0;
|
TimeValues[I]:=0;
|
||||||
@ -566,6 +596,17 @@ 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: PChar; Len : integer; separator : char = #0): TDateTime;
|
||||||
|
|
||||||
|
Var
|
||||||
|
Msg : AnsiString;
|
||||||
|
|
||||||
|
begin
|
||||||
|
Result:=IntStrToTime(Msg,S,Len,Separator);
|
||||||
|
If (Msg<>'') then
|
||||||
|
Raise EConvertError.Create(Msg);
|
||||||
|
end;
|
||||||
|
|
||||||
function StrToTime(const s: ShortString; separator : char): TDateTime;
|
function StrToTime(const s: ShortString; separator : char): TDateTime;
|
||||||
begin
|
begin
|
||||||
result := StrToTime(@s[1], length(s), separator);
|
result := StrToTime(@s[1], length(s), separator);
|
||||||
@ -904,92 +945,84 @@ begin
|
|||||||
end;
|
end;
|
||||||
{$endif unix}
|
{$endif unix}
|
||||||
|
|
||||||
// ieuw. These should be written to work without exceptions?
|
|
||||||
function TryStrToDate(const S: ShortString; out Value: TDateTime): Boolean;
|
function TryStrToDate(const S: ShortString; out Value: TDateTime): Boolean;
|
||||||
begin
|
begin
|
||||||
result := TryStrToDate(S, Value, #0);
|
result := TryStrToDate(S, Value, #0);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TryStrToDate(const S: ShortString; 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: ShortString; out Value: TDateTime;
|
function TryStrToDate(const S: ShortString; out Value: TDateTime;
|
||||||
const useformat : string; separator : char = #0): Boolean;
|
const useformat : string; separator : char = #0): Boolean;
|
||||||
begin
|
|
||||||
result:=true;
|
Var
|
||||||
try
|
Msg : Ansistring;
|
||||||
value:=StrToDate(s, useformat, separator);
|
|
||||||
except
|
|
||||||
on EConvertError do
|
|
||||||
result:=false
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TryStrToDate(const S: AnsiString; out Value: TDateTime): Boolean;
|
|
||||||
begin
|
begin
|
||||||
result := TryStrToDate(S, Value, #0);
|
Value:=IntStrToDate(Msg,@S[1],Length(S),useformat,separator);
|
||||||
|
Result:=(Msg='');
|
||||||
end;
|
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;
|
function TryStrToDate(const S: AnsiString; out Value: TDateTime;
|
||||||
const useformat : string; separator : char = #0): Boolean;
|
const useformat : string; separator : char = #0): Boolean;
|
||||||
begin
|
|
||||||
result:=true;
|
Var
|
||||||
try
|
Msg : Ansistring;
|
||||||
value:=StrToDate(s, useformat, separator);
|
|
||||||
except
|
begin
|
||||||
on EConvertError do
|
Result:=Length(S)<>0;
|
||||||
result:=false
|
If Result then
|
||||||
|
begin
|
||||||
|
Value:=IntStrToDate(Msg,@S[1],Length(S),useformat);
|
||||||
|
Result:=(Msg='');
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
function TryStrToDate(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
|
||||||
|
begin
|
||||||
|
Result:=TryStrToDate(S,Value,ShortDateFormat,Separator);
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
function TryStrToDate(const S: AnsiString; out Value: TDateTime): Boolean;
|
||||||
|
begin
|
||||||
|
Result:=TryStrToDate(S,Value,ShortDateFormat,#0);
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TryStrToDate(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
|
||||||
|
|
||||||
|
begin
|
||||||
|
Result:=TryStrToDate(S,Value,ShortDateFormat,Separator);
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 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: ShortString; out Value: TDateTime; separator : char): Boolean;
|
||||||
begin
|
|
||||||
result:=true;
|
Var
|
||||||
try
|
Msg : AnsiString;
|
||||||
value:=StrToTime(s, separator);
|
begin
|
||||||
except
|
Value:=IntStrToTime(Msg,@S[1],Length(S),Separator);
|
||||||
on EConvertError do
|
result:=(Msg='');
|
||||||
result:=false
|
end;
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TryStrToTime(const S: ShortString; out Value: TDateTime): Boolean;
|
function TryStrToTime(const S: ShortString; out Value: TDateTime): Boolean;
|
||||||
begin
|
begin
|
||||||
result := TryStrToTime(S,Value,#0);
|
Result := TryStrToTime(S,Value,#0);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TryStrToTime(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
|
function TryStrToTime(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
|
||||||
begin
|
Var
|
||||||
result:=true;
|
Msg : AnsiString;
|
||||||
try
|
begin
|
||||||
value:=StrToTime(s, separator);
|
Result:=Length(S)<>0;
|
||||||
except
|
If Result then
|
||||||
on EConvertError do
|
begin
|
||||||
result:=false
|
Value:=IntStrToTime(Msg,@S[1],Length(S),Separator);
|
||||||
|
Result:=(Msg='');
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TryStrToTime(const S: AnsiString; out Value: TDateTime): Boolean;
|
function TryStrToTime(const S: AnsiString; out Value: TDateTime): Boolean;
|
||||||
begin
|
begin
|
||||||
|
Loading…
Reference in New Issue
Block a user