Microsoft KB Archive/44305

= TIME$ Used Twice in IF Statement May Differ; TIME$ 24: 00 Issue =

Article ID: 44305

Article Last Modified on 11/21/2006

-

APPLIES TO


 * 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



SUMMARY
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.)



Documentation Correction
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:


 * &quot;Microsoft QuickBasic 4.0: Language Reference&quot; for QuickBasic versions 4.0 and 4.0b and for Basic Compiler versions 6.0 and 6.0b, page 425.
 * &quot;Basic 7.0: Language Reference&quot; for Basic PDS versions 7.0 and 7.1, page 383.

Code Example
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$ = &quot;23:59:00&quot; START: '*** The IF statement below sometimes executes, even though '*** you may have thought it was not logically possible: IF TIME$ >= &quot;10:00:00&quot; AND TIME$ < &quot;16:59:58&quot; THEN STOP 'To correct this behavior, replace the line above with the 'following two lines: ' TimeVar$ = TIME$ ' IF TimeVar$ >= &quot;10:00:00&quot; AND TimeVar$ < &quot;16:59:58&quot; THEN STOP LOCATE 3, 1 PRINT TIME$ '*** If program didn't stop, reset time and try again: IF MID$(TIME$, 5, 1) = &quot;1&quot; 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

Keywords: KB44305

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.