Solved

Timer concept

Posted on 2004-04-28
409 Views
Hi

If I have say '5446' seconds, how would you go about converting the mentioned seconds into say 'hours'. I have difficalty with the calculations and also what sting command to use (StrTo...).

Regards

0
Question by:Preditor123
• 7
• 6
• 2
• +3

LVL 22

Expert Comment

an example of time parsing:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TimeParse = record
timeDay  : Integer;
timeHour : Integer;
timeMin  : Integer;
timeSec  : Integer;
end;
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Function DoTime(TickVal : Integer) : TimeParse;
end;

var
Form1: TForm1;

implementation

{\$R *.dfm}
// First we need to create a record

// Now here is the function
function TForm1.DoTime(TickVal : Integer) : TimeParse;
begin
Result.timeHour := 0;
Result.timeMin := 0;
Result.timeDay := 0;
Result.timeSec := 0;
// Get days
While TickVal > 86399 Do
Begin
Result.timeDay := Result.timeDay+ 1;
TickVal := TickVal - 86400;
End;
// Get hours
While TickVal > 3599 Do
Begin
Result.timeHour := Result.timeHour + 1;
TickVal := TickVal - 3600;
End;
// Get minutes
While TickVal > 59 Do
Begin
Result.timeMin := Result.timeMin + 1;
TickVal := TickVal - 60;
End;
// Whats left over are seconds
Result.timeSec := TickVal;
end;

//Call it like this

procedure TForm1.Button1Click(Sender: TObject);
Var
FTimeParse :  TimeParse;
begin
FTimeParse := DoTime(5446);
label1.Caption := inttostr(ftimeparse.timeDay)+' days '+                                      inttostr(Ftimeparse.timeHour)+ ' hours '+
inttostr(Ftimeparse.timeMin)+ ' minutes '+
inttostr(Ftimeparse.timesec)+ ' seconds';
end;
0

LVL 11

Expert Comment

if its a string ('5446')

var
SecStr: String;
SecInt: Integer;
Hrs: Real;

SecStr:= '5446'

SecInt:= StrToInt(SecStr)

then there are 3600 secs in a hour,

Hrs:= SecInt / 3600;

if its an integer

var
SecInt: Integer;
Hrs: Real;

SecInt:= 5446

then there are 3600 secs in a hour,

Hrs:= SecInt / 3600;

Shane

0

LVL 26

Expert Comment

Or simplified a bit (I am not looking for any points on this btw...)

function DivMod(dwNumber, dwDivisor: Integer; var dwModulus: Integer): Integer;
begin
dwModulus:=dwNumber mod dwDivisor;
result:=dwNumber div dwDivisor;
end;

var  dwCarry,
dwHrs,
dwMins,
dwSecs:        Integer;
begin

// Calculate hours and get carry
dwHrs:=DivMod(5446, 3600, dwCarry);

// Calculate the number of minutes, carry is number of seconds
dwMins:=DivMod(dwCarry, 60, dwSecs);

// Display
ShowMessage(Format('Hours: %d, Minutes: %d, Seconds: %d', [dwHrs, dwMins, dwSecs]));

end;

-------------

Regards,
Russell
0

LVL 11

Expert Comment

you could just do...

var
Hours : integer;
begin
Hours := (5446 div 60) div 60; // I divde by 60 once for minutes then i divide by 60 again to get   hours

to display you could use IntToStr() to convert or display the number as a string
0

LVL 11

Expert Comment

What about the decinal portion of (he will loose that portion of the hour)

Shane
0

LVL 4

Accepted Solution

ceoworks earned 20 total points
Hi preditor,

Here is the code :

procedure TForm1.btnCalculateClick(Sender: TObject);
var
tmHour,
tmMinute,
tmSecond: integer;
begin
tmSecond := StrToInt(edtSecond.Text);
tmHour := tmSecond div 3600;
tmSecond := tmSecond - (tmHour * 3600);
tmMinute := tmSecond div 60;
tmSecond := tmSecond - (tmMinute * 60);

Caption := IntToStr(tmHour) + ' hour ' + IntToStr(tmMinute) + ' minute ' + IntToStr(tmSecond) + ' second ';
end;

Kind Regards,

Oktay
0

LVL 11

Expert Comment

OR like this:

var
Hours,
Minutes,
Seconds: word;
begin
Seconds := StrToInt('5446');
Hours := Seconds div 3600;
Seconds := Seconds - (Hours * 3600);
Minutes := Seconds div 60;
Seconds := Seconds - (Minutes * 60);
Caption :=  FormatDateTime('hh:nn:ss',  EncodeTime(Hours, Minutes, Seconds, 0)));
end;

Shane
0

LVL 4

Expert Comment

Isn't it same thing ? :)
0

LVL 11

Expert Comment

well, in that case, they are all the same thing -

i was merely showing you that you could use the formatting method & encoding method to display it as: hh:nn:ss

:))))

Shane
0

LVL 4

Expert Comment

All of them are the same. But this is the easier one.. And I only made a joke, don't take it serious ;))

Cheers Shane,
0

LVL 11

Expert Comment

"But this is the easier one.. "

OK, if you insist.....

then I insist that

Caption :=  FormatDateTime('hh:nn:ss',  EncodeTime(Hours, Minutes, Seconds, 0)));

is better than

Caption := IntToStr(tmHour) + ' hour ' + IntToStr(tmMinute) + ' minute ' + IntToStr(tmSecond) + ' second ';

And I too was only joking......

Cheers!

Shane
0

LVL 4

Expert Comment

This string is only for to explain what i did. :

Caption := IntToStr(tmHour) + ' hour ' + IntToStr(tmMinute) + ' minute ' + IntToStr(tmSecond) + ' second ';

I tried not to include any extra info like EncodeTime, FormatDateTime for him/her to easily understand. When i said that this is the easier one, i meant the logic of my solution.

Ohh we did so much work for 20 points :-)

Bye,

Oktay
0

Author Comment

Hi ceoworks

Thanks for the code logic on the timer. I see that there is many ways to 'kill a cat'. The EncodeTime and FormatDateTime functions looks very interesting, but is way above my understanding for now.

To the rest of the programmers the participated in this problem thank you very much. I used 'ceoworks' code because of it's simplicity.

ceoworks, sorry for the little points, but it was the last of my reserve.

Regards

Preditor

0

LVL 4

Expert Comment

I'm glad to hear that you like the answer. And don't think, i don't care about the points. Because It's so hard to become an expert in EE(50.00 point !!). :))

Have a nice day,

Oktay
0

LVL 11

Expert Comment

Yup, points  are not a big deal.....  IM glad you found a solution - thats what we are here for, however, for sakes OF AN ARGUMENT, the solution you chose was a collaboration of everyone elses, so before you give all the co-hoots to ceoworks, remember all he did was to wrap everyone elses answer up nicely....

Look at mine, rlibbys, and Jase-Coder's answers and you will see what i mean....

and seeing your question was only:

"If I have say '5446' seconds, how would you go about converting the mentioned seconds into say 'hours'"

I believe all three of us answered it....

ceoworks, took a little from each and made a nice little neat sample for you - which i had to top off for him...

<SMILE>

ceoworks - enjoy the 20 points - <SMILE>!

Good Luck!

Shane
0

LVL 4

Expert Comment

Preditor if you are new in Delphi, you may wants to have a look at http://www.delphibasics.co.uk . It's including tons of things about Delphi's basics concepts.

And Shane,

"ceoworks, took a little from each and made a nice little neat sample for you - which i had to top off for him..."

First of all i should say that i didn't look at the other one's answers before i gave the accepted answer. Look at the other answers that given before my accepted answer !! And please tell me are you really sure that i got a trick from the other answers ?? I really don't understand why are you that much aggressive with this 20 point. I get point or not, i'm only a new member. I'm not the man who you should race with !!

Before i started to use EE, i was thinking that it's a good place to share my experience with the others. Because when you give an answer here, the person who ask the question can accept your answer as a right answer or not. It means that you can learn if you know that topic correctly or not. This is the feature is not exist in newsgroups and mail groups.

Last night i tried to answer to 5 or 6 question. I tried to be helpfull because do you know that i like it. I don't have enough time to answer a lots of question and get 50.000p. This is not the thing i'm working for. Only "thanks Oktay" is enough too.. I was so excited about to help people in here but i should say now i'm not feeling that much excited. I only gave an answer for 20p question and now i'm making an argument because of this. It's really not an enjoyfull thing.

Shane you type "<SMILE>" and it shows that you know that i'll be angry with this "ceoworks, took a little from each and made a nice little neat sample for you - which i had to top off for him..." words. "<SMILE>" is not shows that you are joking. If you wouldn't care about the points, you wouldn't say that too "the solution you chose was a collaboration of everyone elses".  Ferruccio68 or the other didn't gave any other comment. They may thinking that Preditor123 got the right answer so there isn't any reason to post more.. So i think that you are the only one who is not happy with it. If you think so, I'm so sorry to steal your points Shane...

Cheers,

Oktay
0

LVL 22

Expert Comment

Just to moderate a little:

I know and understand perfectly what you was meaning (about the <SMILE> also: for shane a smile is just a smile, not enough more than that....) but...

ceoworks got the rigth feeling with Ex-Ex -> Before i started to use EE, i was thinking that it's a good place to share my experience with the others. Because when you give an answer here, the person who ask the question can accept your answer as a right answer or not. It means that you can learn if you know that topic correctly or not. This is the feature is not exist in newsgroups and mail groups. <-

and as here in Ex-Ex all we need new Experts with good helping feel don't let's take them afraid about it...

So welcome to newbes an good work to all....let's learn all togeter from each other :)
F68 ;-)

0

LVL 26

Expert Comment

Nicely put F68...

Regards,
Russell
0

LVL 11

Expert Comment

No, im going to reply..... and leave it at that..... my post was for  Preditor123 as clearly stated... ceoworks can interpret as he sees fit.... and I stand by what i said... enough said...

Shane
0

Featured Post

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small printâ€¦
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have toâ€¦
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidtâ€¦
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.