Adding Hours greater 24 h

Asked

Viewed 943 times

1

The sql statement returns me the time type hours:

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

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

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

end;
  • Which database?

  • 2

    You should not use Ttime, but Ttimespan. Ttime refers to a specific time of the day. Ttimespan applies to a duration.

2 answers

1


Tiago, you need a function that transforms the string in seconds, kind of:

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;

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.

SUM STRING AS DATE - Oracle

http://www.delphitricks.com/source-code/strings/format_seconds_as_hhmmss.html

https://stackoverflow.com/questions/23420818/convert-hhmmss-to-seconds-or-minutes-with-delphi

  • Why is it assumed that the data is string? This doesn’t seem to be the problem!

0

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

  1. Utilise TTimeSpan - that in accordance with the documentation was created to describe a time interval. In this case you will need to use the methods of this object to work with the value;
  2. Using 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.