System time: Difference between revisions

Added Commodore BASIC.
(adding lambdatalk)
(Added Commodore BASIC.)
Line 635:
This shows the time in human-readable format (using a 24-hour clock):
<lang qbasic>PRINT TIME$</lang>
 
==={{header|Commodore BASIC}}===
 
====TI$ and TI====
 
Commodore BASIC maintains both a 24-hour clock as <code>TI$</code> and a jiffy clock (1 jiffy = 1/60 seconds) as the variable <code>TIME</code>. Since Commodore BASIC recognizes only the first two letters of a variable identifier, <code>TIME$</code>, <code>TIMPANI$</code>, and <code>TIMBUKTU$</code> would all refer to the time keeping variable of <code>TI$</code>.
 
Notes about the two variables:
 
* <code>TI$</code> counts in standard time format of HHMMSS from 00:00:00 to 23:59:59 after which it will reset back to 00:00:00. On certain CBM/PET computers, TI$ is seven digits to include 1/10 seconds at the end.
* <code>TI$</code> is both read and write capable. The user can initialize the clock to any valid time within the 24 hour range by assigning it a string value of the time, e.g. <code>TI$ = "111530"</code> for 11:15:30. Assigning improper values (e.g. "089900" as 99 minutes after 8 o'clock) will result in automatic correction for the true time (09:39:00). Unfortunately there is no tracking of overflow in the hours to keep track of days, etc.
* <code>TI</code> is read only, but always reflects the jiffy equivalent to the value in <code>TI$</code>, that is: TI = (HH &times; 216,000) + (MM &times; 3600) + (SS &times; 60) + JIFFIES. <code>TI</code> can be set or reset to a value by setting <code>TI$</code> to the equivalent HHMMSS time, although resolution to specific jiffies is not possible with this method.
* Particularly on the Commodore 64, the timer handler is driven off of a CIA chip interrupt, but is in no way tied to any of the [[#Time of Day Clocks|time-of-day clocks]] built into the CIA (see below.)
* Because the clock is managed by the KERNAL operating system, the clock will loose time during certain I/O processes, such as disk access, etc. when the interrupt is unable to be processed on time.
 
 
'''Code Examples'''
 
<lang commodorebasic>1 rem time since last reset
5 print chr$(147);chr$(14);
10 t$=ti$
20 h$=left$(t$,2)
30 m$=mid$(t$,3,2)
40 s$=right$(t$,2)
50 print chr$(19);"Roughly ";tab(9);h$;" hours "
51 print tab(9);m$;" minutes"
52 print tab(9);s$; " seconds":print
60 print "has elapsed since TI$ was last reset or"
65 print "the system was powered on.":print
70 print "Press any key to quit."
80 get k$:if k$="" then goto 10
</lang>
 
<lang commodorebasic>5 rem set time to current time of day
10 print chr$(147);
20 input "Enter hour of day (0-23)";h$
25 if val(h$)<0 or val(h$)>23 then goto 20
30 input "Enter minutes (0-59)";m$
35 if val(m$)<0 or val(m$)>99 then goto 30
40 input "Enter seconds (0-59)";s$
45 if val(s$)<0 or val(s$)>99 then goto 40
60 ti$=h$+m$+s$
70 print chr$(147);
80 print chr$(19);"The time is now: ";ti$
85 print:print "Press any key to end."
90 get k$:if k$="" then 80
100 end</lang>
 
 
====Time of Day Clocks====
 
The Commodore 64 and 128 models each have two MOS 6526 Complex Interface Adapters (CIA) that each have Time of Day (TOD) clocks. Interestingly, these clocks are ''not'' used to drive the internal variables <code>TI</code> and <code>TI$</code>. The advantage of these clocks is that they will continue to count time independent of the operating system, and therefore do not lose time during the same I/O operations that affect <code>TI</code> and <code>TI$</code>.
 
The time is represented in Binary Coded Decimal, and a separate register is used to access hours, minutes, seconds, and 1/10 seconds.
 
'''Reading''' the HOURS register will suspend further updates to ''all'' of the TOD registers to allow the program to accurately read the complete time. ''The clock itself does not stop''. Once the 1/10 register is read, updates to the registers will continue. If the programmer intends to read only the hours of a running clock, a subsequent read of the 1/10 register must be performed to resume updating of the registers.
 
'''Writing''' to the HOURS register will ''stop the clock completely''. The clock will start only when the 1/10 register is written to, even with a zero.
 
'''Code Example - Commodore 64/128 Only'''
 
<lang commodorebasic>1 rem read and set cia time of day clock
2 rem rosetta code commodore 64/128 example
10 print chr$(147);chr$(14)
15 hr=56331:mr=56330:sr=56329:tr=56328:gosub 300
20 print spc(11);"CIA #1 Time of Day"
21 print:print "Press S to start or stop the clock."
22 print "Press T to change the time."
23 print:print "Press Q to quit.":print
25 h=peek(hr):m=peek(mr):s=peek(sr):th=peek(tr):t=h:gosub 200
35 print chr$(19);:for q=1 to 8:print chr$(17);:next q
36 print "Clock is:";tab(12);cl$(b):print
40 print "Hours:";tab(12);h;". "
45 print "Minutes:";tab(12);m;". "
50 print "Seconds:";tab(12);s;". "
55 print "1/10 Sec.:";tab(12);th;". "
60 print "AM or PM? ";tab(13);ap$
65 get k$:if k$="" then goto 25
70 if k$="s" and b=0 then poke tr,0:b=1:goto 25
75 if k$="s" and b=1 then poke hr,t:b=0:goto 25
80 if k$="t" then gosub 400:goto 20
90 if k$="q" then end
100 goto 25
200 rem decode bcd
210 ap$="AM":if (h and 128)=128 then ap$="PM"
220 s=int((s and 112)/16)*10+(s and 15)
230 m=int((m and 112)/16)*10+(m and 15)
240 h=int((h and 48)/16)*10+(h and 15)
250 return
300 rem decide if clock is running
305 cl$(0)="Stopped":cl$(1)="Running":b=0
301 rem latch, read/resume
310 z=peek(hr):t1=peek(tr)
315 for i=1 to 200:next i
320 z=peek(hr):t2=peek(tr)
325 if t1<>t2 then b=1
330 return
400 rem change clock value
405 print chr$(147)
410 input "Hour";nh$
415 if val(nh$)<1 or val(nh$)>12 then goto 410
416 if val(nh$)<10 then nh$="0"+nh$
420 input "Minutes";nm$
425 if val(nm$)<0 or val(nm$)>59 then goto 420
426 if val(nm$)<10 then nm$="0"+nm$
430 input "Seconds";ns$
435 if val(ns$)<0 or val(ns$)>59 then goto 430
436 if val(ns$)<10 then ns$="0"+ns$
440 print "AM or PM (a,p)? ";
445 get ap$:if ap$<>"a" and ap$<>"p" then 445
446 print ap$
450 ap=0:if ap$="p" then ap=128
455 rem convert input to bcd
457 ns=val(mid$(ns$,1,1))*16+val(mid$(ns$,2,1))
460 nm=val(mid$(nm$,1,1))*16+val(mid$(nm$,2,1))
465 nh=val(mid$(nh$,1,1))*16+val(mid$(nh$,2,1))
470 nh=nh+ap
475 rem now set values into clock
480 poke hr,nh:rem set hour stops clock
485 poke mr,nm:rem set minute
490 poke sr,ns:rem set second
495 poke tr,0 :rem set 1/10 starts clock
497 b=1
499 print chr$(147):return</lang>
 
=={{header|Batch File}}==
113

edits