Time in QB - Considerations

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Time in QB - Considerations

Post by moneo »

Hey guys, here are some considerations when tracking elapsed time.

Consideration #1:
If your program will never run passed midnight,
and therefore never run for more than 24 hours,
then the computation of elapsed time in seconds needs no additional computation. You may want to convert seconds to hours/minutes/seconds.

Consideration #2:
If your program can run passed midnight,
but not run for more than 24 hours,
then the computation of elapsed time has to detect that it has gone beyond midnight and adjust the elapsed time accordingly.
If you're using the TIMER function and have a start.time and an end.time, you detect having gone passed midnight when the end.time is less than the start.time. You can adjust for this by adding 86400 (seconds in a day 60*60*24) to the end.time before computing the elapsed time in seconds (end.time - start.time).
You may want to convert elapsed seconds to hours/minutes/seconds.

Consideration #3:
This is rarely required.
If your program can run passed midnight,
and can run for more than 24 hours,
then the computation of elapsed time must also involve the computation of elapsed days. This becomes a quite lengthy process which must take into consideration the following points:
* You will need to get a start date and an end date.
* Determine leap year for both dates.
* Convert a year/month/day to the equivalent total number of days from some offset or pivot year.
* Compute the difference in days between two dates (start date and end date).
* Having the difference in days and the difference in seconds (from TIMER), compute the total elapsed time in seconds. Note: This could be tedious.
* Having the elapsed time in seconds, you will probably want to convert to days/hours/minutes/seconds.
*****
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

... why dont you elaborate some more and then send it into QBXP...

btw, you have 1 more day to get it out pete... you can do it!
Image
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Nathan1993 wrote:... why dont you elaborate some more and then send it into QBXP....
Yeah, good idea, but perhaps a little too late. Why give Pete some more last minute work.
*****
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

well then why not document this and submit it to this sites tutors? That way it wont get COMPLETLY lost within the next month or 2.
Image
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

As for #3, not needed, as you could just increase a var by 1 each second..

Then divide to get proper values...
I have left this dump.
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Z!re wrote:As for #3, not needed, as you could just increase a var by 1 each second..

Then divide to get proper values...
Please explain your idea in more detail.
*****
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Roughly:

Code: Select all

t! = Timer+1
Do
If Timer >= t! Then
  t! = Timer+1
  secCount = secCount + 1
End If
locate 1,1:print int(secCount/86400);"d";int(secCount/3600) mod 24;"h";int(secCount/60) mod 60;"m";int(secCount) mod 60;"s   "
loop while inkey$ = ""
You might want to be even more accurate by taking the difference between timer and t!+1 and add it to the new time (or subtract). However, this method, as all other computer time measuring methods, is not accurate if left for long periods of time..

However, for a few days this method works fine...


Also note that I didnt do any midnight correction, which is required, as this is just an example..


Assuming secCount is a long you'd be able to get: 2147483647 seconds, or: ~24855 days (~70 years)
I have left this dump.
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Z!re,

Very nice bit of code. What you are doing is tracking every single second that goes by (or elapses) in the variable secCount. I tested it and it runs fine.

If a program can afford to loop on the timer in this manner, then at any give time the elapsed time of the program running is contained in the variable secCount. Obviously, while doing other things, the program must get the timer at least once per second, otherwise secCounter will be out of phase.

Actually, this was not the idea of my post. I was referring to obtaining a starting date and time, and then an ending date and time, and computing the elapsed days/hours/minutes/seconds as the difference between them. This is much more difficult but doesn't have to worry about accessing the timer at least once per second.

Anyway, nice idea, I like it.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Depends on the program I guess..

And you could make it so it can add the difference since last "call" to the secCount

Ofcourse then you'd most likely get rounding errors..
I have left this dump.
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Z!re wrote:Depends on the program I guess..

And you could make it so it can add the difference since last "call" to the secCount

Ofcourse then you'd most likely get rounding errors..
After my last post, I started thinking about your approach and I came up with the same idea about adding the difference since the last "call" to the secCount. This allows you to only have to do a "call" to the timer at least once per day.

If you save the timer of the last call, and do an "if" before adding to see if we went past midnight, then this approach will handle every case needed. The secCount will have a grand total of seconds since we started, which can be easily converted to days/hours/minutes/seconds as you already showed.

If we keep the secCounter as double, then any rounding errors should be negligible.

Here's an implementation of Z!re's general purpose elapsed time logic.

Code: Select all

' Initialization for time logic
dim TimerLastCall as double   'Timer value on last call
dim TimerNow      as double   'Timer value now
dim secCount       as double   'Cumulative elapsed seconds w/decimals
dim ElapsedSecs  as long       'Final elpased seconds w/o decimals
TimerLastCall=timer              'Establish start time
secCount=0                          

' Program continues here...

' Somewhere in the main loop of the program, you must call or sample
' the timer as follows:
gosub CallTimer

' At the point in program where you want to determine the elapsed time
' in seconds that the program has been running, do as follows:
gosub CallTimer
ElapsedSecs=secCount
' At this point you can use whatever conversion you prefer to convert the
' total elapsed seconds to days/hours/minutes/seconds

' Insert the following subroutine:
CallTimer:
  TimerNow=timer
  if TimerNow < TimerLastCall then
    TimerNow=TimerNow+86400   'adjust when went past midnight
  end if
  secCount = secCount + (TimerNow - TimerLastCall)   'track elapsed
  TimerLastCall = TimerNow   'update last call
return
Thanks to you, Z!re, the above bit of code should handle any of the 3 considerations regarding computing elapsed time.
*****
Seb McClouth

Post by Seb McClouth »

How could this could be of use in a linux-clone?

grtz
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Seb McClouth wrote:How could this could be of use in a linux-clone?

grtz
First tell me what a linux-clone is.
*****
Seb McClouth

Post by Seb McClouth »

alrighty... I'm talking about qbinux... I need a working time routine... that's all...

grtz
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Seb McClouth wrote:alrighty... I'm talking about qbinux... I need a working time routine... that's all...
grtz
Ok, does qblinux have the equivalent of:
"TIMER - a function that returns the number of seconds elapsed since
midnight."

If so, than you should be able to lift my implementation of Z!re's logic above, which I tested for the simplest case (consideration #1).

BTW, what is the language that qblinux is coded in, and what operating system does it run under?
*****
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations »

QBinux is written in QB (hence the first 2 letters) and is not QB(L)inux.
Secondly, to see the source, Seb has graciously put it up for download in the MISC section of Pete's Site downloads. That should be mostly what you need.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

QBinux is based on Novix, and as I made that, I know it's core is not "pausing" but based on looping.. (Think: INKEY instead of INPUT)

Implementing timing should be no problem..
I have left this dump.
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Hey, Z!re,

I kinda expected some comment of my implementation of your idea. :cry:
*****
PQBC at school

Post by PQBC at school »

moneo wrote:Hey, Z!re,

I kinda expected some comment of my implementation of your idea. :cry:
*****
That is the first time I have ever seen you use an emoticon.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

moneo, sorry..

Not much to say.. it's pretty much what I always used when I coded in QB.. Except I had it as a function with static variables in it..


And I have to agree with PQBC.. I've never seen you use an emoteicon before =)
I have left this dump.
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Regarding my use of emoticons:

I used one on October 26th.

I hardly use them because the majority of my posts deal with coding problems and examples. Somehow emoticons just don't apply to code. :wink:
*****
Post Reply