Solved

Timer concept

Posted on 2004-04-28
19
413 Views
Last Modified: 2010-05-18
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...).

Please help.

Regards

 
0
Comment
Question by:Preditor123
  • 7
  • 6
  • 2
  • +3
19 Comments
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10940609
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

by:shaneholmes
ID: 10940879

 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

by:Russell Libby
ID: 10941421

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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:Jase-Coder
ID: 10941998
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

by:shaneholmes
ID: 10942067
What about the decinal portion of (he will loose that portion of the hour)

Shane
0
 
LVL 4

Accepted Solution

by:
ceoworks earned 20 total points
ID: 10943773
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

by:shaneholmes
ID: 10944455
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

by:ceoworks
ID: 10944547
Isn't it same thing ? :)
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10944568
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

by:ceoworks
ID: 10944611
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

by:shaneholmes
ID: 10944630
"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

by:ceoworks
ID: 10944682
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

by:Preditor123
ID: 10950013
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

by:ceoworks
ID: 10950458
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

by:shaneholmes
ID: 10951335
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

by:ceoworks
ID: 10951775
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

by:Ferruccio Accalai
ID: 10951905
Just to moderate a little:

Shane, please, don't reply anymore in this topic :))
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

by:Russell Libby
ID: 10951931

Nicely put F68...

Regards,
Russell
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10952728
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

792 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