Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time variable

Status
Not open for further replies.

The

Programmer
Jul 30, 2001
92
CA
I know that there is TIME$, which is a string value of the computer's time when the program is started. However, I thought it would be a lot more useful if there were a <i>number</i> value for the time, and I was sure that there must be, but I couldn't find it on the internet. Can anyone help?
 
TIMER.
vcn.gif

Do no harm.​
 
Okay... here's a programming example. Since TIMER returns the number of seconds since midnight, you can derive the hour by dividing it by 3600, the minute by dividing the remainder of the hour division by sixty... and your seconds, well, they're the scraps that are left over. This is a crude approximation of the way things work - and not entirely accurate. You'll see what I mean if you run the following code. It will display the system time followed by your calculated time, based on the value of TIMER.

Notice how the calulated time is always about a half-second slower than the system time? Your assignment, should you decide to accept it, will be to synchronize these values.

This code sets the system time to two seconds before midnight to illustrate another problem. Notice how, when the system clock strikes midnight, both TIME$ and MyTime$ display the hour as 24 (is that a valid hour on the 24-hour clock?) and MyTime$ begins to display the seconds as a negative value (-.128, etc.). You might want to fix this nasty piece of midnight screwiness before you paste the code into some mission-critical application :).
[tt]
CLS
OldTime$ = TIME$
TIME$ = &quot;23:59:58&quot;
DO
t = TIMER
h = t \ 3600
m = (t MOD 3600) \ 60
s = t - ((h * 3600) + (m * 60))
MyTime$ = RIGHT$(&quot;0&quot; + LTRIM$(STR$(h)), 2) + &quot;:&quot;
MyTime$ = MyTime$ + RIGHT$(&quot;0&quot; + LTRIM$(STR$(m)), 2) + &quot;:&quot;
MyTime$ = MyTime$ + LEFT$(LTRIM$(STR$(s)) + &quot;00000&quot;, 5)
LOCATE 1, 1
PRINT TIME$; &quot; = &quot;; MyTime$
IF INKEY$ <> &quot;&quot; THEN EXIT DO
LOOP
TIME$ = OldTime$
[/tt]
There are, undoubtedly, better, quicker, more efficient, more accurate ways to do this. Personally, I never derived much satisfaction from having somebody hand me a 100% working solution.

BTW: You might want to set your system clock after running this. It will be off by the number of seconds you run the code. That part of my post is 100% accurate.
vcn.gif

Do no harm.​
 
The C programming language in fact does offer exactly what you're looking for, The, in a function called 'time()'. 'time()' returns a 32-bit integer value that contains the number of seconds since January 1st 1970 at midnight (this precise second is called the 'epoch'). This is convenient, as you can compare two dates very easily, although it does have some problems, first and foremost that it will shortly run out of possible values for dates, so it isn't something to use in programs that you expect to be running in some shape or form 50 years from now (or for data files that will still be in use then), but for little hack jobs in the now, it's a very useful function :)

Using this function from QB is a different matter, though :) It is not present in any of the QB libraries, so you would have to import it from a C compiler. I don't have any 16-bit DOS C compilers any more, so I can't help with this. The alternative would be to build up a C time from [tt]DATE$[/tt] and [tt]TIME$[/tt]. The following code should do this:
[tt]
FUNCTION ctime&
d$ = DATE$
month% = VAL(LEFT$(d$, 2))
day% = VAL(MID$(d$, 4, 2))
year% = VAL(MID$(d$, 7))

t& = 0
FOR i% = 1970 TO year% - 1
IF isLeapYear%(i%) THEN days% = 366 ELSE days% = 365
t& = t& + 86400 * days%
NEXT i%
FOR i% = 1 TO month% - 1
SELECT CASE i%
CASE 1: days% = 31
CASE 2: IF isLeapYear%(year%) THEN days% = 29 ELSE days% = 28
CASE 3: days% = 31
CASE 4: days% = 30
CASE 5: days% = 31
CASE 6: days% = 30
CASE 7: days% = 31
CASE 8: days% = 31
CASE 9: days% = 30
CASE 10: days% = 31
CASE 11: days% = 30
CASE 12: days% = 31
END SELECT
t& = t& + 86400 * days%
NEXT i%

days% = day% - 1
t& = t& + 86400 * days%

d$ = TIME$
hour% = VAL(LEFT$(d$, 2))
minute% = VAL(MID$(d$, 4, 2))
second% = VAL(MID$(d$, 7))

seconds& = second% + 60& * (minute% + 60& * hour%)
t& = t& + seconds&

ctime& = t&
END FUNCTION

FUNCTION
isLeapYear% (y%)
IF (y% MOD 400) = 0 THEN
isLeapYear% = -1
ELSEIF (y% MOD 100) = 0 THEN
isLeapYear% = 0
ELSEIF (y% MOD 4) = 0 THEN
isLeapYear% = -1
END IF 'defaults to 0
END FUNCTION

[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top