Einzelnen Beitrag anzeigen

shmia

Registriert seit: 2. Mär 2004
5.508 Beiträge
 
Delphi 5 Professional
 
#4

AW: exchange elapsed time between Clients

  Alt 24. Jun 2010, 17:53
The elapsed time could be greater than 12 or 24 hours.
I suggest to format the time like this:
Delphi-Quellcode:
function GetElapsedTimeAsString(t : Double; WithMS:Boolean=False):string;
const
  HOURS_PER_DAY = 24.0;
  MINUTES_PER_HOUR = 60.0;
  SECONDS_PER_MINUTE = 60.0;
  MS_PER_SECOND = 1000.0;
var
  Hh, Mh, Sec, MSec: integer;
  sign : string;
begin
  if t < 0.0 then
  begin
    t := -t;
    sign := '-';
  end
  else
    sign := '';

  t := t * HOURS_PER_DAY;
  Hh := Trunc(t);
  t := (t - Hh) * MINUTES_PER_HOUR;
  Mh := Trunc(t);
  t := (t - Mh) * SECONDS_PER_MINUTE;
  Sec := Trunc(t);
  t := (t - Sec) * MS_PER_SECOND;
  MSec := Trunc(t);

  if WithMS then
     result := sign + Format('%2.2d%s%2.2d%s%2.2d.%3.3d', [Hh, TimeSeparator,Mh, TimeSeparator,sec,Msec])
   else
     result := sign + Format('%2.2d%s%2.2d%s%2.2d', [Hh, TimeSeparator,Mh, TimeSeparator,sec]);
end;

procedure TForm1.ElapsedTimeTimer(Sender: TObject);
begin
  label1.Caption:='"elapsed time : " ' + GetElapsedTimeAsString(Now-FStartTime);
end;
The function GetElapsedTimeAsString() can format negative timespans, too.
Andreas
  Mit Zitat antworten Zitat