Solved

Calculate to times??

Posted on 2001-06-19
8
291 Views
Last Modified: 2010-04-06
Hi all.
I use this to calculate how long the oparation took:

...
Var
  StartTime,
  EndTime: TTime;

...
//When process starts
StartTime := Time;

...
//When process end
EndTime := (Time + StrToTime(timelabel.caption)) - StartTime;
timelabel.caption := FormatDateTime('hh:nn:ss', EndTime);


It also work fine, but it don't calculate the seconds used. What i mean is that the process are not always very long ex. 1 sec. and then the timelabel is still 00:00:00
The process have to take more then 60 sec before something happens, on the timelabel.

I want to know, why it does not calculate the time correct?


Thanks.
Dennis
0
Comment
Question by:Dennis9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6206381
Use the GetTickCount() function
0
 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6206392
var dProcess, dDif :Double;
begin
  dProcess:=GetTickCount();
  ...;//something
  dDif:=GetTickCount()-dProcess;
  ShowMessage('Finished in '+IntToStr(Trunc(dDif) div 60000)+' min, '+IntToStr((Trunc(dDif) mod 60000) div 1000)+' sec, '+IntToStr(Trunc(dDif) mod 1000)+' ms. ');
  dProcess:=GetTickCount();
  ...; // ready gor next thing
0
 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6206404
GetTickCount() is an API function which retrieves how miliseconds elapsed after the OS was started. The time is stored as a DWORD value. Therefore, the time will wrap around to zero if Windows is run continuously for 49.7 days.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
bugroger earned 30 total points
ID: 6207240
hi,

here is an examples to calc the time and convert
it to a string.
-> CalcTimeString



//When process starts
StartTick := GetTickCount;

//When process ends
StopTick  := GetTickCount;

Procedure CalcTime(StartTick, StopTick : DWord; VAR Hours, Minutes, Seconds : Byte; VAR MilliSeconds : Word);
Var
 Diff : DWord;
Begin
 Diff          := StopTick - StartTick;  //get the whole time

 Hours         := Diff      DIV 3600000;
 Diff          := Diff      MOD 3600000; //get the remaining ticks (without  hours)

 Minutes       := Diff      DIV 60000;
 Diff          := Diff      MOD 60000; //get the remaining ticks (without minutes)

 Seconds       := Diff      DIV 1000;
 Diff          := Diff      MOD 1000; //get the remaining ticks (without seconds)

 MilliSeconds  := Diff;    
End;


Function Dez2ToStr2(b : Byte) : String;
Const
 Ziffern : array[0..9] of Char =
 ('0','1','2','3','4','5','6','7','8','9');

Var
 Str2    : array[0..1] of Char;
 a       : Byte;

Begin
 a       := b DIV 10;
 Str2[0] := Ziffern[a];
 a       := b MOD 10;
 Str2[1] := Ziffern[a];
 Result := Str2[0] + Str2[1];
End;

Function WordDez3ToStr3(w : Word) : String;
Const
 Ziffern : array[0..9] of Char =
 ('0','1','2','3','4','5','6','7','8','9');

Var
 Str3    : array[0..2] of Char;
 a,c       : Byte;

Begin
 a       := w DIV 100;
 w       := w MOD 100;
 Str3[0] := Ziffern[a];
 a       := w DIV 10;
 w       := w MOD 10;
 Str3[1] := Ziffern[a];
 Str3[2] := Ziffern[a];
 Result := Str3[0] + Str3[1] + Str3[2];
End;


Function CalcTimeString(StartTick, StopTick : Dword) : String;
Var
 Hours, Minutes, Seconds : Byte;
 MilliSeconds            : Word;

Begin
 CalcTime(StartTick, StopTick, Hours, Minutes, Seconds, MilliSeconds);
 Result := Dez2ToStr2(Hours)  + ':'
         + Dez2ToStr2(Minutes)  + ':'
         + Dez2ToStr2(Seconds) + ':'
         + WordDez3ToStr3(MilliSeconds);
End;

GL
Bug
0
 
LVL 6

Expert Comment

by:zebada
ID: 6208334
Sometimes you may need sub-milliseconds...

var
  f,st,et: Int64;
begin
  QueryPerformanceFrequency(f);
  QueryPerformanceCounter(st);

  // Do something here

  QueryPerformanceCounter(et);
  ShowMessage(Format('took %f microseconds,[((et-st)*1000000)/f]));
end
0
 
LVL 1

Expert Comment

by:alx512
ID: 6255602
after seting the caption value add line in your code:

timelabel.repaint;
0
 

Expert Comment

by:realzal
ID: 6390356
for measuring small time period less then milliseconds you can use zlrdtsc library from http://www.specosoft.com .
It with sorces and free.
0
 
LVL 1

Author Comment

by:Dennis9
ID: 6528248
Sorry i had forgot this.
I accept your answer because there i both get the hours, min, sec and mil.

Thanks all
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question