TimestampStrToDateTime error fix
Posted: 12.11.2008, 12:29
Some dates of form 2008-1-1 seem not to work good with
function TimestampStrToDateTime in ZSysUtils. Therefore i rewrote the function to accept a more date format's.
Good luck with it.
gr,
Niek
function TimestampStrToDateTime in ZSysUtils. Therefore i rewrote the function to accept a more date format's.
Good luck with it.
gr,
Niek
Code: Select all
{**
Converts Timestamp String to TDateTime
@param Value a timestamp string.
@return a decoded TDateTime value.
}
function TimestampStrToDateTime(const Value: string): TDateTime;
var
Year, Month, Day, Hour, Min, Sec: Integer;
StrLength, StrPos, StrPosPrev: Integer;
//
function CharMatch( matchchars: string ): boolean;
// probeer zoveel mogelijk char te matchen, p op 1 na laatste match
begin
StrPosPrev:= StrPos;
Result:= false;
while StrPos<=StrLength do
if pos(Value[StrPos],matchchars)>0 then begin inc(StrPos); Result:= true; end
else break;
end;
begin
Result := 0;
StrPos:= 1;
StrLength := Length(Value);
if not CharMatch('1234567890') then exit; // year
Year := StrToIntDef(Copy(Value, StrPosPrev, StrPos-StrPosPrev), 0);
if not CharMatch('-/\') then exit;
if not CharMatch('1234567890') then exit; // month
Month:= StrToIntDef(Copy(Value, StrPosPrev, StrPos-StrPosPrev), 0);
if not CharMatch('-/\') then exit;
if not CharMatch('1234567890') then exit; // day
Day:= StrToIntDef(Copy(Value, StrPosPrev, StrPos-StrPosPrev), 0);
Result := EncodeDate(Year, Month, Day);
//
if not CharMatch(' ') then exit;
if not CharMatch('1234567890') then exit; // hour
Hour := StrToIntDef(Copy(Value, StrPosPrev, StrPos-StrPosPrev), 0);
if not CharMatch('-/\') then exit;
if not CharMatch('1234567890') then exit; // minute
Min:= StrToIntDef(Copy(Value, StrPosPrev, StrPos-StrPosPrev), 0);
if not CharMatch('-/\') then exit;
if not CharMatch('1234567890') then exit; // second
Sec:= StrToIntDef(Copy(Value, StrPosPrev, StrPos-StrPosPrev), 0);
Result := REsult + EncodeTime(Hour, Min, Sec,0);
end;