* trystrto* variants with formatsettings.

git-svn-id: trunk@14018 -
This commit is contained in:
marco 2009-11-03 21:20:10 +00:00
parent c49c838270
commit 8bc0583045
2 changed files with 85 additions and 39 deletions

View File

@ -326,9 +326,16 @@ 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 IntStrToDate(Out ErrorMsg : AnsiString; 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; const defs:TFormatSettings; separator : char = #0): TDateTime;
const SInvalidDateFormat = '"%s" is not a valid date format'; const SInvalidDateFormat = '"%s" is not a valid date format';
procedure FixErrorMsg(const errm :ansistring;const errmarg : ansistring);
begin
errormsg:=format(errm,[errmarg]);
end;
var var
df:string; df:string;
d,m,y,ly:word; d,m,y,ly:word;
@ -340,15 +347,15 @@ var
LocalTime:tsystemtime; LocalTime:tsystemtime;
YearMoreThenTwoDigits : boolean; YearMoreThenTwoDigits : boolean;
begin begin
ErrorMsg:=''; ErrorMsg:=''; Result:=0;
if (Len=0) then if (Len=0) then
begin begin
ErrorMsg:=Format(SInvalidDateFormat,['']); FixErrorMsg(SInvalidDateFormat,'');
exit; exit;
end; end;
YearMoreThenTwoDigits := False; YearMoreThenTwoDigits := False;
if separator = #0 then if separator = #0 then
separator := DateSeparator; separator := defs.DateSeparator;
df := UpperCase(useFormat); df := UpperCase(useFormat);
{ Determine order of D,M,Y } { Determine order of D,M,Y }
yp:=0; yp:=0;
@ -382,8 +389,8 @@ begin
end; end;
if Which<>3 then if Which<>3 then
begin begin
ErrorMsg:=Format(SErrIllegalDateFormatString,[useformat]); FixErrorMsg(SErrIllegalDateFormatString,useformat);
Exit; Exit;
end; end;
{ Get actual values } { Get actual values }
for i := 1 to 3 do for i := 1 to 3 do
@ -406,24 +413,24 @@ begin
begin begin
inc(n); inc(n);
if n>3 then if n>3 then
begin begin
ErrorMsg:=Format(SInvalidDateFormat,[s]); FixErrorMsg(SInvalidDateFormat,s);
exit; exit;
end; 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
begin begin
ErrorMsg:=Format(SInvalidDateFormat,[s]); FixErrorMsg(SInvalidDateFormat,s);
Exit; Exit;
end; end;
s1 := ''; s1 := '';
end end
else if not (s[i] in ['0'..'9']) then else if not (s[i] in ['0'..'9']) then
begin begin
ErrorMsg:=Format(SInvalidDateFormat,[s]); FixErrorMsg(SInvalidDateFormat,s);
Exit; Exit;
end; end;
end ; end ;
// Fill in values. // Fill in values.
@ -457,13 +464,13 @@ begin
end; end;
if (y >= 0) and (y < 100) and not YearMoreThenTwoDigits then if (y >= 0) and (y < 100) and not YearMoreThenTwoDigits then
begin begin
ly := ly - TwoDigitYearCenturyWindow; ly := ly - defs.TwoDigitYearCenturyWindow;
Inc(Y, ly div 100 * 100); Inc(Y, ly div 100 * 100);
if (TwoDigitYearCenturyWindow > 0) and (Y < ly) then if (defs.TwoDigitYearCenturyWindow > 0) and (Y < ly) then
Inc(Y, 100); Inc(Y, 100);
end; end;
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; function StrToDate(const S: PChar; Len : integer; const useformat : string; separator : char = #0): TDateTime;
@ -471,7 +478,7 @@ Var
MSg : AnsiString; MSg : AnsiString;
begin begin
Result:=IntStrToDate(Msg,S,Len,useFormat,Separator); Result:=IntStrToDate(Msg,S,Len,useFormat,DefaultFormatSettings,Separator);
If (Msg<>'') then If (Msg<>'') then
Raise EConvertError.Create(Msg); Raise EConvertError.Create(Msg);
end; end;
@ -510,7 +517,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 IntStrToTime(Out ErrorMsg : AnsiString; const S: PChar; Len : integer; separator : char = #0): TDateTime; function IntStrToTime(Out ErrorMsg : AnsiString; const S: PChar; Len : integer;const defs:TFormatSettings; separator : char = #0): TDateTime;
var var
Current: integer; PM: integer; Current: integer; PM: integer;
@ -541,12 +548,12 @@ var
Inc(Current); Inc(Current);
val(StrPas(S+j, 1 + current - j), result, c); val(StrPas(S+j, 1 + current - j), result, c);
end end
else if ((TimeAMString<>'') and (CurrentChar = TimeAMString[1])) or (S[Current] in ['a', 'A']) then else if ((defs.TimeAMString<>'') and (CurrentChar = defs.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 (CurrentChar = TimePMString[1])) or (S[Current] in ['p', 'P']) then else if ((defs.TimePMString<>'') and (CurrentChar = defs.TimePMString[1])) or (S[Current] in ['p', 'P']) then
begin begin
Current := 1 + Len; Current := 1 + Len;
PM := 2; PM := 2;
@ -564,7 +571,7 @@ var
begin begin
if separator = #0 then if separator = #0 then
separator := TimeSeparator; separator := defs.TimeSeparator;
Current := 0; Current := 0;
PM := 0; PM := 0;
for i:=0 to 4 do for i:=0 to 4 do
@ -602,7 +609,7 @@ Var
Msg : AnsiString; Msg : AnsiString;
begin begin
Result:=IntStrToTime(Msg,S,Len,Separator); Result:=IntStrToTime(Msg,S,Len,DefaultFormatSettings,Separator);
If (Msg<>'') then If (Msg<>'') then
Raise EConvertError.Create(Msg); Raise EConvertError.Create(Msg);
end; end;
@ -964,7 +971,7 @@ Var
Msg : Ansistring; Msg : Ansistring;
begin begin
Value:=IntStrToDate(Msg,@S[1],Length(S),useformat,separator); Value:=IntStrToDate(Msg,@S[1],Length(S),useformat,defaultformatsettings,separator);
Result:=(Msg=''); Result:=(Msg='');
end; end;
@ -978,12 +985,11 @@ begin
Result:=Length(S)<>0; Result:=Length(S)<>0;
If Result then If Result then
begin begin
Value:=IntStrToDate(Msg,@S[1],Length(S),useformat,Separator); Value:=IntStrToDate(Msg,@S[1],Length(S),useformat,DefaultFormatSettings,Separator);
Result:=(Msg=''); Result:=(Msg='');
end; end;
end; end;
function TryStrToDate(const S: ShortString; out Value: TDateTime; separator : char): Boolean; function TryStrToDate(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
begin begin
Result:=TryStrToDate(S,Value,ShortDateFormat,Separator); Result:=TryStrToDate(S,Value,ShortDateFormat,Separator);
@ -1001,16 +1007,25 @@ begin
Result:=TryStrToDate(S,Value,ShortDateFormat,Separator); Result:=TryStrToDate(S,Value,ShortDateFormat,Separator);
end; end;
function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
Var
// function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean; Msg : Ansistring;
begin
Result:=Length(S)<>0;
If Result then
begin
Value:=IntStrToDate(Msg,@S[1],Length(S),FormatSettings.ShortDateFormat,FormatSettings,#0);
Result:=(Msg='');
end;
end;
function TryStrToTime(const S: ShortString; out Value: TDateTime; separator : char): Boolean; function TryStrToTime(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
Var Var
Msg : AnsiString; Msg : AnsiString;
begin begin
Value:=IntStrToTime(Msg,@S[1],Length(S),Separator); Value:=IntStrToTime(Msg,@S[1],Length(S),DefaultFormatSettings,Separator);
result:=(Msg=''); result:=(Msg='');
end; end;
@ -1026,8 +1041,8 @@ begin
Result:=Length(S)<>0; Result:=Length(S)<>0;
If Result then If Result then
begin begin
Value:=IntStrToTime(Msg,@S[1],Length(S),Separator); Value:=IntStrToTime(Msg,@S[1],Length(S),DefaultFormatSettings,Separator);
Result:=(Msg=''); Result:=(Msg='');
end; end;
end; end;
@ -1036,8 +1051,16 @@ begin
result := TryStrToTime(S,Value,#0); result := TryStrToTime(S,Value,#0);
end; 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;
Var msg : AnsiString;
begin
Result:=Length(S)<>0;
If Result then
begin
Value:=IntStrToTime(Msg,@S[1],Length(S),FormatSettings,#0);
Result:=(Msg='');
end;
end;
function TryStrToDateTime(const S: ShortString; out Value: TDateTime): Boolean; function TryStrToDateTime(const S: ShortString; out Value: TDateTime): Boolean;
begin begin
@ -1061,8 +1084,31 @@ function TryStrToDateTime(const S: AnsiString; out Value: TDateTime): Boolean;
end; end;
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;
var
I: integer;
dtdate, dttime :TDateTime;
begin
result:=true;
I:=Pos(FormatSettings.TimeSeparator,S);
If (I>0) then
begin
While (I>0) and (S[I]<>' ') do
Dec(I);
If I>0 then
begin
if not TryStrToDate(Copy(S,1,I-1),dtdate,Formatsettings) then
exit;
if not TryStrToTime(Copy(S,i+1, Length(S)-i),dttime,Formatsettings) then
exit;
Value:=ComposeDateTime(dtdate,dttime);
end
else
result:=TryStrToTime(s,Value,Formatsettings);
end
else
result:=TryStrToDate(s,Value,Formatsettings);
end;
function StrToDateDef(const S: ShortString; const Defvalue : TDateTime): TDateTime; function StrToDateDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
begin begin

View File

@ -158,9 +158,9 @@ function TryStrToDate(const S: AnsiString; out Value: TDateTime;
function TryStrToDateTime(const S: ShortString; out Value: TDateTime): Boolean; function TryStrToDateTime(const S: ShortString; out Value: TDateTime): Boolean;
function TryStrToDateTime(const S: AnsiString; 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: ShortString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif} function StrToDateDef(const S: ShortString; const Defvalue : TDateTime): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}
function StrToDateDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif} function StrToDateDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime; {$ifdef SYSUTILSINLINE}inline;{$endif}