Microsoft KB Archive/44305
Article ID: 44305
Article Last Modified on 11/21/2006
- Microsoft QuickBasic 4.0
- Microsoft QuickBASIC 4.0b
- Microsoft QuickBasic 4.5 for MS-DOS
- Microsoft BASIC Compiler 6.0
- Microsoft BASIC Compiler 6.0b
- Microsoft BASIC Professional Development System 7.0
- Microsoft BASIC Professional Development System 7.1
This article was previously published under Q44305
If you test the TIME$ function twice in one IF statement, the returned time may change between invocations and give you a conditional result that you did not expect. In the example below, the symptoms occur intermittently only during the tenth of a second at midnight (24:00:00). To correct this programming mistake, you should assign the value returned by TIME$ to a temporary string variable, which can then be reliably tested multiple times in the IF statement.
This information applies to Microsoft QuickBasic versions 4.0, 4.0b, and 4.50 for MS-DOS; to Microsoft Basic Compiler versions 6.0 and 6.0b for MS-DOS; and to Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1 for MS-DOS. (Programs compiled with BC.EXE may not be as sensitive to this timing issue as the QB.EXE or QBX.EXE environment.)
The following manuals state that the hour (hh) returned TIME$ function can range from 00 to 23. The manuals should also state that the hour can also sometimes be 24 for a split second at midnight:
- "Microsoft QuickBasic 4.0: Language Reference" for QuickBasic versions 4.0 and 4.0b and for Basic Compiler versions 6.0 and 6.0b, page 425.
- "Basic 7.0: Language Reference" for Basic PDS versions 7.0 and 7.1, page 383.
When run inside the QuickBasic environment (QB.EXE), the two TIME$ functions in the following program may return different values (depending upon timing) and sometimes make the IF statement STOP the program. You may think that the IF statement should never be executed, and the programmed loop should continue until you press CTRL+BREAK. However, after the program runs long enough, it will eventually show the behavior in QB.EXE or QBX.EXE. This behavior may not occur in the executable version (.EXE) of the program.
To correct this programming error, assign a string variable to the TIME$ function and execute the IF statement using this variable instead of TIME$, as noted in the commented portions of the program below.
The following code demonstrates this behavior. At 24:00:00, this program will (sometimes) unexpectedly execute the IF statement. The faster the machine, the more likely it is that the behavior will show up. The behavior occurs only during the tenth of a second at midnight because at that time, the TIME$ function may return 24:00:00 and then 00:00:00 at the next invocation. You may have to run the program a number of times to demonstrate the behavior because it does not occur every time.
CLS MISSED: TIME$ = "23:59:00" START: '*** The IF statement below sometimes executes, even though '*** you may have thought it was not logically possible: IF TIME$ >= "10:00:00" AND TIME$ < "16:59:58" THEN STOP 'To correct this behavior, replace the line above with the 'following two lines: ' TimeVar$ = TIME$ ' IF TimeVar$ >= "10:00:00" AND TimeVar$ < "16:59:58" THEN STOP LOCATE 3, 1 PRINT TIME$ '*** If program didn't stop, reset time and try again: IF MID$(TIME$, 5, 1) = "1" THEN GOTO MISSED GOTO START
If you implement the correction shown above, the program will run correctly without stopping in QB.EXE or QBX.EXE. You may press CTRL+BREAK to stop the program in the QB.EXE or QBX.EXE environment.
Additional query words: QuickBas BasicCom 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10