Time in QB - Considerations
Time in QB - Considerations
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.
*****
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.
*****
Roughly:
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)
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$ = ""
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.
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.
*****
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.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
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.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..
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
*****
Ok, does qblinux have the equivalent of:Seb McClouth wrote:alrighty... I'm talking about qbinux... I need a working time routine... that's all...
grtz
"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?
*****
-
- Veteran
- Posts: 399
- Joined: Wed Mar 02, 2005 9:01 pm
- Location: Nashville, Tennessee
- Contact: