terça-feira, 12 de março de 2013

Function mostrarTempodecorrido


Função para mostrar o tempo equivalente a X segundos.

function mostrarTempodecorrido(segundos:Variant;tipo:Integer):string;
var
 s,m,h,d,e,a:variant;
 tempo:string;
begin
 s:=segundos;
//min, hr,   dia,  mes,   ano
 m:=0; h:=0; d:=0; e:=0; a:=0;

 while s>59 do begin
  s:=(s-60);
  m:=m+1;
 end;

 while m>59 do begin
  m:=(m-60);
  h:=h+1;
 end;

 while h>23 do begin
   h:=(h-24);
   d:=d+1;
 end;

 while d>29 do begin
   d:=(d-30);
   e:=e+1;
 end;

 while e>11 do begin
   e:=(e-12);
   a:=a+1;
 end;

 if tipo=1 then
  tempo:=RightStr('0'+inttostr(h),2)+':'+RightStr('0'+inttostr(m),2)+':'+RightStr('0'+inttostr(s),2);

 if tipo=2 then
  tempo:= RightStr('0'+inttostr(h),2)+'H '+RightStr('0'+inttostr(m),2)+'m '+RightStr('0'+inttostr(s),2)+'s';

 if (a>0) then begin
  tempo:=inttostr(a)+'A '+inttostr(e)+'M '+inttostr(d)+'D '+tempo;
 end else begin
  if e>0 then begin
    tempo:=inttostr(e)+'M '+inttostr(d)+'D '+tempo;
  end else begin
   if d>0 then
    tempo:=inttostr(d)+'D '+tempo;
  end;
 end;
 Result:=tempo;
end;


Obs: não esquecer de declarar a Function no início do arquivo.


...
  private
    { Private declarations }
  public
    { Public declarations }
  end;
    function mostrarTempodecorrido(segundos:Variant;tipo:Integer):string;
....
....

Exemplo:

Procedure TForm1.Button1Click(Sender: TObject);
var
 tempo:string;
begin
 tempo:=mostrarTempodecorrido(3691,1); //tipo 1 
 ShowMessage(tempo);
 tempo:=mostrarTempodecorrido(3691,2); //tipo 2 
 ShowMessage(tempo);
end;




Nenhum comentário:

Postar um comentário