Subject Re: only time in a TDBGrid
Author duilio_fos
>I have added 2 char(5) fields to the table...

I had to rethink my quick-and-dirt solution because it was er ... too
quick and dirt.

I found a much better solution by playing with the OnSetText and
OnGetText events of the timestamp fields:

procedure TTabFrm.Timestamp2HHMM(Sender: TField; var Text: string;
DisplayText: Boolean);
begin
Text:=FormatDateTime('hh:mm',FTable.FieldByName
('time').AsDateTime);
end;
procedure TTabFrm.HHMM2Timestamp(Sender: TField; const Text: string);
begin
FTable.FieldByName('time').AsDateTime:=Convert(Text);
end;

(where the convert function is enclosed below...)

I think that this info could be of some help to others...

Duilio


function Convert(s:string):TDateTime;
var
i:integer;
u,v:string;
Hour, Min, Sec, MSec: Word;
negative:boolean;
d:integer;
begin
Min:=0;
Sec:=0;
MSec:=0;
if s='' then
begin
result:=0;
exit;
end;
v:=trim(s);
s:=v;
negative:=False;
if Copy(s,1,1)='-' then
begin
negative:=True;
s:=Copy(s,2,length(s)-1);
end;
i:=Pos('.',s);
if i=0 then
i:=Pos(',',s);
if i=0 then
i:=Pos(':',s);
if i=0 then
begin
try
Hour:=StrToInt(s);
except
Hour:=0;
end;
end
else
begin
u:=Copy(s,1,i-1);
v:=Copy(s,i+1,length(s)+1-(i+1));
try
Hour:=StrToInt(u);
except
Hour:=0;
end;
try
Min:=StrToInt(v);
except
Min:=0;
end;
end;
d:=0;
while Hour>=24 do
begin
Inc(d);
Hour:=Hour-24;
end;
try
result:=d+EncodeTime(Hour,Min,Sec,Msec);
if negative then
result:=-result;
except
MessageDlg('error!',mtError,[mbOk],0);
result:=-1;
end;
end;