Adding Hours higher 24 h

1

The sql statement returns me the times of type Time:

| Horas  |
|20:52:00|
|12:42:00|
|09:00:00|
|07:45:00|

I would like to add up the total hours, but this way it is "zeroing" when it reaches 24 hours.

procedure ....
var
  vqtMaqLigada : TTime;
begin
while Horas.eof do 
 vqtMaqLigada := vqtMaqLigada + HorasValor;

end;
    
asked by anonymous 29.11.2016 / 12:42

2 answers

1

James, you need a function that turns the string into seconds, type this:

function GetSeconds(ATimeString: string): Integer;
var
  Hour, Min, Sec, MSec: Word;
begin
  DecodeTime(StrToTime(ATimeString), Hour, Min, Sec, MSec);
  Result := Hour * 3600 + Min * 60 + Sec;
end;

In this way you can add the seconds contained in the various strings. Then you can turn the number of seconds into string again.

  function SecondToTime(const Seconds: Cardinal): Double; 
var 
  ms, ss, mm, hh, dd: Cardinal; 
begin 
  dd := Seconds div SecPerDay; 
  hh := (Seconds mod SecPerDay) div SecPerHour; 
  mm := ((Seconds mod SecPerDay) mod SecPerHour) div SecPerMinute; 
  ss := ((Seconds mod SecPerDay) mod SecPerHour) mod SecPerMinute; 
  ms := 0; 
  Result := dd + EncodeTime(hh, mm, ss, ms); 
end; 

If you are using Oracle you can add up using the same database functions.

SOMAR STRING as DATE - Oracle

link

link

    
29.11.2016 / 13:01
0

Type TTime is used to represent a specific time . If you want to represent a time period you should choose other options. I thought of at least two:

  • Use TTimeSpan - which according to documentation was created to describe a time interval . In this case you will have to use the methods of this object to work with the value;
  • Use a common integer - This way you can store time without having to do conversions;
  • I made an example using the first option:

    procedure ....
    var
      TempoMaqLigadaTotal : TTimeSpan;
    begin
      //Inicializando o tempo;
    
      TempoMaqLigadaTotal := TTimeSpan.FromSeconds(0);
      while tblHoras.eof do
      begin 
       TempoMaqLigadaTotal.Add(TTimeSpan.FromSeconds(HorasValorEmSegundos));
      end;
    
      ShowMessage(Format('Ficou ligada por "%d dias, %d horas, %d minutos e %d segundos"', 
                  [TempoMaqLigadaTotal.Days, TempoMaqLigadaTotal.Hours, 
                   TempoMaqLigadaTotal.Minutes, TempoMaqLigadaTotal.Seconds]));
    end;
    
        
    29.11.2016 / 17:31