Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi exchange elapsed time between Clients (https://www.delphipraxis.net/152500-exchange-elapsed-time-between-clients.html)

sdean 24. Jun 2010 15:17

Delphi-Version: 2005

exchange elapsed time between Clients
 
Hi , i've 2 Clients " 2 Machines in my network " and a Server i use the following procedure to calc the elapsed time :

Delphi-Quellcode:
procedure TForm1.ElapsedTimeTimer(Sender: TObject);
var
    s: string;
    TotalTime: TDateTime;
    DLTimes: Double;
    Hh, Mh, Sec, MSec: Word;
begin
  TotalTime:= Now-FStartTime;
  DecodeTime(TotalTime, hH, Mh, Sec, MSec);
  Sec := Sec + Mh * 60 + hH * 3600;
   DLTimes := Sec + MSec / 1000;
  s:=Format('%2d:%2d:%2d', [sSec div 3600, (Sec div 60) mod 60, Sec mod 60]);
  label1.Caption:=FormatDateTime('"elapsed time : " hh:nn:ss', StrToTime(s));
end;
Let's suppose that ClientA's elapsed time is : 00:20:00 here when i transfer this elapsed time value to ClientB this letter elapsed time will become 12:20:00

so why 12:20:00 and not 00:20:00 ?



P.S : i use Indy

Stevie 24. Jun 2010 15:30

AW: exchange elapsed time between Clients
 
I guess on the one machine the date time format of windows is set to 24h format while the other is set to 12h format (if i remember 12:20 is the 12h format for 20 minutes after midnight)

sdean 24. Jun 2010 15:58

AW: exchange elapsed time between Clients
 
thank you Stevie , But the 2 Machines' time format is 12h .

shmia 24. Jun 2010 16:53

AW: exchange elapsed time between Clients
 
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.

sdean 24. Jun 2010 23:36

AW: exchange elapsed time between Clients
 
thank you shmia , But when i transfer elapsed time from a Client to an other the result will be as follows :

ClientA's elapsed time is : 00:20:00 when i trabsfer it to ClientB it will be : 968484:20:00

is there any issue with it ?!

Stevie 25. Jun 2010 06:49

AW: exchange elapsed time between Clients
 
How do you transfer it? Send the float value or format it to let's say UTC?


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:43 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz