TIME$
| BASIC variable | |
| Variable-Name: | TI$ |
| Type: | System variable |
| List of all BASIC keywords | |
Remark: This article describes the BASIC variable TI$ in BASIC V2 of the Commodore 64.
Type: System variable as string General Programming-Syntax: TI$
The system variable TI$ contains a six-character string representing the running time of the computer since the last reset in the format "HHMMSS", corresponding to the value of the system variable TI, both of which are based on the system-internal timer (time counter). Setting TI$ also sets TI to the corresponding value in 1/60 s.
The system variable holds values ranging from "000000" to "235959". When this value overflows, it wraps back to "000000", and the numeric timer TI is also reset to 0.
Although "235959" is the highest meaningful displayable value, you can assign values up to "774020" without immediate issues. If you assign a value between "774021" and "999999", TI$ wraps around by subtracting 774021. For example:
TI$ = "888888"
This sets the value to "114907" because 888888 - 774021 = 114867, which is then converted from raw seconds into the time format (adding 1 minute for 67 seconds → "114907").
Accuracy[edit | edit source]
TI$ does not always reflect real elapsed time. It relies on the system’s interrupt routine, which can be temporarily halted—for example, when using the datasette or running code that disables interrupts (e.g., using SEI). During such periods, the timer stops advancing.
Edge Cases[edit | edit source]
- Assigning a non-string to TI$ triggers ?TYPE MISMATCH ERROR.
- Invalid strings (e.g., incorrect length or containing illegal characters like "-" or "+") result in ?ILLEGAL QUANTITY ERROR.
- Despite this, the system does not strictly validate the content of valid-length strings. Briefly, TI$ and TI can take on invalid or out-of-range values before resetting to "000000" on the next jiffy tick.
- A common mistake occurs when assigning to a string variable like TITLE$. Since BASIC only considers the first two characters (TI), this accidentally overwrites TI$ and alters the system timer.
Examples[edit | edit source]
Name resolving[edit | edit source]
Because Commodore 64 BASIC only considers the first two characters of variable names, using TIME$ resolves to TI$, making them interchangeable identifiers. This may provide a convenient mnemonic, but it comes at a very slight performance cost due to the extra characters being parsed during variable name resolution.
Digital clock[edit | edit source]
10 REM Clock simulation, cancel with RUN/STOP 20 TIME$="235800" : REM Setting a new time for overflow! 30 T$=TIME$ : REM Save the time to avoid a overflow! 40 PRINT CHR$(147); 50 PRINT LEFT$(T$,2);":"; 60 PRINT MID$(T$,3,2);":"; 70 PRINT RIGHT$(T$,2) 80 GOTO 30
The updated time is shown at the upper, left corner on the screen.
Time delay[edit | edit source]
100 TI$="000000" 110 WAIT 162,64
This program waits approximately 1 second. The WAIT command pauses execution until bit 6 of the low byte of the jiffy clock (location 162, $A2) changes from 0 to 1. This occurs once every 64 jiffies, or approximately 64/60 seconds (~1.07 seconds), as the bit flips when the value reaches 64. This code won't break by pushing RUN/STOP , just with RUN/STOP +RESTORE combination.
ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT