TweetFollow Us on Twitter

Time Retrieval
Volume Number:5
Issue Number:1
Column Tag:Basic School

Related Info: Time Manager

Time Retrieval and Storage Techniques

By Dave Kelly, MacTutor Editorial Board

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

Time Retrieval and Storage Techniques

It’s only a matter of time. Anybody see the ad for the MacTutor of the future in the November, 1988 MacTutor? The ad may sound like a joke about the 100th anniversary of MacTutor, but some of the “future” has already been invented. I did some thought on the subject of the Basic School column described in the ad, which is also the title of this column. There are two basic subjects implied by the title, time retrieval and time storage.

From a philosophical point of view you may ask, how can time be saved or retrieved? Looking at the answer from that perspective would lead us to a discussion of Einstein and his theory of relativity which is beyond the scope and intent of this column. Instead let’s look at some ways which we can use time to our advantage and see how storage and retrieval fits into the scheme of things.

From a user level on the Macintosh there is one time retrieval/storage mechanism that is accessible to all “the rest of us”. The example I refer to is the Alarm mechanism built into the clock chip. The date and time setting is copied (“retrieved”) at system startup from the clock chip into its own low-memory location. It’s stored there as the number of seconds since midnight, January 1, 1904, and is updated every second. The range of the clock chip is from January 1, 1904 through February 6, 2040 (does that mean that 51 years from now I’ll need to replace my clock chip?). The low-memory location can be access by your program. BASIC (every brand) includes its own time retrieval command. In True Basic the TIME and TIME$ functions are provided. TIME returns the number of seconds since midnight in tenths of seconds. The TIME$ function returns a string that contains the time as measured by the 24-hour clock with resolution in seconds. ZBasic and MS QuickBASIC uses the TIMER function which returns the seconds since midnight (similar to the True Basic TIME function) and TIME$ which works the same as in True Basic. The problem is that the TIMER function does not provide enough resolution. At least the True Basic TIME function provided tenths of seconds or better resolution. Since we know that other languages are able to access the time in greater resolution, we may use the same function (ROM) to retrieve the time in greater resolution. This is especially important for benchmark tests where two compiled programs are being timed and the difference is tenths of seconds or better. In benchmarks that have been done in the past, sometimes the TIME function was used and there was rounding off to either 0 or 1 second. A program running in 0 seconds is absurd, but that was the best we could do with MS Basic at the time. QuickBASIC and ZBasic both allow access to the TickCount call (Macintosh ROM). In QuickBASIC a typical benchmark might be implemented such as:

Figure 1. The Time Machine

First initialize the ToolBox routine:

TOOLBOX “I”

This must be used before any other ToolBox statement in the program. Now define a sub program or routine that can be called to replace the other time functions usually used in the benchmark.

SUB Tick (Count&) STATIC
TrapNo% = &HA975
Count& = 0&  : ‘Define the variable
ToolBox “L”, TrapNo%, Count&
END SUB

Now call Tick(Count&) whenever you need the time. A tick is one-sixtieth of a second, enough for the tenth of a second accuracy provided by True BASIC’s function. In ZBasic it is even easier to call TickCount. In ZBasic just use the function directly:

Count&=FN TICKCOUNT

As you can see, accessing real time is really the simple part of the job. Accessing stored time is another aspect. I’ll leave the subject of how the stored time became stored for later except to say that the Parameter RAM also stores the alarm setting. You can set the alarm with the Alarm Clock DA (included with your system software).

The Analog Alarm Clock program included here demonstrates how you can retrieve the stored alarm time from the low-memory area. The Alarm time is stored at $200. The getAlarm routine retrieves the time by peeking at memory directly. The result is then converted to date information via the secs2date toolbox routine. The Analog Clock program reveals some problems. The date conversion library routines are used to convert seconds to dates and dates to seconds. Secs2Date works fine, but there is a problem with the Date2Secs routine. This means that the Date2Secs routine must be called a different way. Fortunately, QuickBASIC provides the ToolBox call which allows you to make ROM calls yourself. The following lines replace the Date2Secs library routine:

ToolBox “I”

...   (Don’t forget to initialize the toolbox)


TrapNo%=&HA9C7
ToolBox “R”, TrapNo%, ReturnArray&(0),VARPTR(DtRec%(0))
Al&=ReturnArray&(2)

It’s unfortunate that we have to go through that trouble, but that’s just the way it goes. I confess it took awhile before I realized that I wasn’t going to be able to get it to work without the ToolBox library call.

In the program, alarm time is taken from parameter RAM and then it’s updated when changed when the alarm is turned on. Be careful when poking around the parameter RAM. If the wrong parameter gets changed you may have to Zap the parameter RAM to get started. You can do this by removing your MacPlus battery for a few minutes when your power is off or by holding down command-option when opening the control panel on newer Mac systems.

Flashing the menu bar like the Alarm Clock DA does will be left for another day. Anybody know how to do that from Basic? What I’m not sure of is where the alarm status is stored. Happy New Year!

‘Analog Alarm Clock Program
‘©1989 MacTutor
‘by Dave Kelly

DIM DtRec%(6),CenterRect%(3),pattern%(3)
ToolBox “I”
DIM ReturnArray&(5)
GOSUB getAlarm
false=0:true=NOT false
PI=3.14159

‘Set up menus
MENU 1,0,1,”File”
MENU 1,1,1,”Turn Alarm On ”
MENU 1,2,0,”-”
MENU 1,3,1,”Quit”
CmdKey 1,3,”Q”

‘ Get Screen size
sheight=SYSTEM(6)
swidth=SYSTEM(5)
WINDOW 1,”Analog Alarm Clock”,(4,44)-(swidth-4,sheight-4),6

‘Find the center of the window, centerx%,centery%
‘ This should work with any size of screen
wheight%=WINDOW(3)
wwidth%=WINDOW(2)
centerx%=wwidth%/2
centery%=wheight%/2
circledia=centery%-25
GOSUB SetupClock
ON MENU GOSUB MenuEvent
ON DIALOG GOSUB DialogEvent
DIALOG ON:MENU ON

Loop:
forecolor 33:’ change color to black
t$=TIME$:’ Get the current time and separate into variables
hour$=LEFT$(t$,2)
Min$=MID$(t$,4,2)
Sec$=RIGHT$(t$,2)
H=VAL(hour$):M=VAL(Min$):S=VAL(Sec$)
IF H>12 THEN H=H-12
H=H+M/60

‘ Compute the angles for each hand of the clock
SecAngle=(360-S*6)*PI/180
MinAngle=(360-M*6)*PI/180
HAngle=(360-H*30)*PI/180

‘Draw the second hand
CALL MOVETO (centerx%,centery%)
ypoint=(circledia*COS(SecAngle))*.98
xpoint=(circledia*SIN(SecAngle))*.98
CALL LINE (-xpoint,-ypoint)
IF SecAngle<>OldSec THEN
    CALL MOVETO (centerx%,centery%)
    CALL PENMODE (11)
    ypoint=(circledia*COS(OldSec))*.98
    xpoint=(circledia*SIN(OldSec))*.98
    CALL LINE (-xpoint,-ypoint)
    CALL PENMODE(8)
END IF

‘Draw the minute hand
CALL PENSIZE(2,2)
CALL MOVETO (centerx%,centery%)
ypoint=(circledia*COS(MinAngle))*.9
xpoint=(circledia*SIN(MinAngle))*.9
CALL LINE (-xpoint,-ypoint)
IF MinAngle<>OldMin THEN
    CALL MOVETO (centerx%,centery%)
    CALL PENMODE (11)
    ypoint=(circledia*COS(OldMin))*.9
    xpoint=(circledia*SIN(OldMin))*.9
    CALL LINE (-xpoint,-ypoint)
    CALL PENMODE(8)
END IF

‘Draw the hour hand
CALL PENSIZE(4,4)
CALL MOVETO (centerx%,centery%)
ypoint=(circledia/2*COS(HAngle))
xpoint=(circledia/2*SIN(HAngle))
CALL LINE (-xpoint,-ypoint)
IF MinAngle<>OldMin THEN
    CALL MOVETO (centerx%,centery%)
    CALL PENMODE (11)
    ypoint=(circledia/2*COS(OldH))
    xpoint=(circledia/2*SIN(OldH))
    CALL LINE (-xpoint,-ypoint)
    CALL PENMODE(8)
END IF
CALL PENSIZE(1,1)

forecolor 205:’ Change color to red
LOCATE 1,1:PRINT “AlarmTime: “;AlarmTime$
forecolor 33:’ Change color to black
LOCATE 2,1:PRINT “Current Time: “;t$
CALL TEXTMODE(0)
OldT$=t$:OldSec=SecAngle:OldMin=MinAngle:OldH=HAngle
CALL FILLOVAL(VARPTR(CenterRect%(0)),VARPTR(pattern%(0)))
IF Alrm=false THEN
    forecolor 273
    LOCATE 3,1:PRINT “Alarm is off”:GOTO Loop
END IF
forecolor 341:’Change color to Green
LOCATE 3,1:PRINT “Alarm is on “
‘Sound the alarm if time is up!
IF t$=AlarmTime$ THEN
    BEEP:BEEP:BEEP
    LOCATE 4,1:PRINT “Alarm event occurred”
END IF
GOTO Loop
DIALOG OFF

SetupClock:’ Set up the face of the clock
activewindow=WINDOW(0)
WINDOW OUTPUT 1
CALL TEXTSIZE(14)
CALL MOVETO(centerx%-8,centery%-circledia-4):PRINT “12”;
CALL MOVETO(centerx%-8,centery%+circledia+20):PRINT “ 6”;
CALL MOVETO(centerx%-circledia-20,centery%+7):PRINT “9”;
CALL MOVETO(centerx%+circledia+10,centery%+7):PRINT “3”;
PSET (centerx%,centery%)
forecolor 205:’ Change color to red
CALL PENSIZE(2,2)
CIRCLE (centerx%,centery%),circledia
CALL PENSIZE(1,1)
SetRect CenterRect%(0),centerx%-5,centery%-5,centerx%+5,centery%+5
forecolor 33:’ Change color to black
SetRect pattern%(0),&HFFFF,&HFFFF,&HFFFF,&HFFFF
CALL FILLOVAL(VARPTR(CenterRect%(0)),VARPTR(pattern%(0)))
WINDOW OUTPUT activewindow
RETURN

DialogEvent:
d=DIALOG(0)
SELECT CASE d
    CASE 1
        IF DIALOG(1)=1 THEN editwindow=true
    CASE 4
        GOTO Quit
    CASE 5
        IF DIALOG(5)=1 THEN GOSUB SetupClock
    CASE ELSE
END SELECT
RETURN

MenuEvent:
menunumber=MENU(0)
menuitem=MENU(1)
MENU
IF menunumber=1 THEN
    IF menuitem=1 THEN GOSUB SetAlarm
    IF menuitem=3 THEN Quit
END IF
RETURN

SetAlarm:’ Set the alarm
Alrm=NOT Alrm
DoSet:
IF Alrm THEN
    MENU 1,1,1,”Turn Alarm Off”
    WINDOW 2,””,(50,50)-(250,150),2
    LOCATE 2,1:PRINT “Alarm Time:”
    IF AlarmTime$=”” THEN AlarmTime$=TIME$
    EDIT FIELD 1,AlarmTime$,(100,17)-(165,32),1
    BUTTON 1,1,”OK”,(40,45)-(170,75),1
    editwindow=false
    WHILE editwindow=false
    WEND
    AlarmTime$=EDIT$(1)
    a$=””:i=1
    WHILE MID$(AlarmTime$,i,1)<>”:”
        a$=a$+MID$(AlarmTime$,i,1)
        i=i+1
    WEND
    Ahour=VAL(a$)
    IF Ahour>24 OR Ahour<0 THEN GOTO DoSet
    a$=””
    i=i+1
    WHILE MID$(AlarmTime$,i,1)<>”:”
        a$=a$+MID$(AlarmTime$,i,1)
        i=i+1
    WEND
    Amin=VAL(a$)
    IF Amin>59 OR Amin<0 THEN GOTO DoSet
    a$=””
    i=i+1
WHILE i<=LEN(AlarmTime$)
        a$=a$+MID$(AlarmTime$,i,1)
        i=i+1
WEND
Asec=VAL(a$)
IF Asec>59 OR Asec<0 THEN GOTO DoSet
a$=””
IF Ahour<10 THEN a$=”0"+STR$(Ahour) ELSE a$=STR$(Ahour)
a$=a$+”:”
IF Amin<10 THEN a$=a$+”0"+STR$(Amin) ELSE a$=a$+STR$(Amin)
a$=a$+”:”
IF Asec<10 THEN a$=a$+”0"+STR$(Asec) ELSE a$=a$+STR$(Asec)
AlarmTime$=””
FOR i=1 TO LEN(a$)
    IF MID$(a$,i,1)<>” “ THEN AlarmTime$=AlarmTime$+MID$(a$,i,1)
NEXT i
DtRec%(3)=Ahour
DtRec%(4)=Amin
DtRec%(5)=Asec
‘Call Date2secs routine (QuickBASIC Date2secs does not work right)
TrapNo%=&HA9C7
ToolBox “R”, TrapNo%, ReturnArray&(0),VARPTR(DtRec%(0))
Al&=ReturnArray&(2)
POKEL &H200, Al&
    WINDOW CLOSE 2
ELSE
    TEXTMODE (0):LOCATE 2,1:PRINT”                                   
     “
    MENU 1,1,1,”Turn Alarm On ”
END IF
RETURN

getAlarm:’ Retrieve
Al&= PEEKL(&H200)
secs2Date Al&,DtRec%(0)
a$=””
IF DtRec%(3)<10 THEN a$=”0"+STR$(DtRec%(3)) ELSE a$=STR$(DtRec%(3))
a$=a$+”:”
IF DtRec%(4)<10 THEN a$=a$+”0"+STR$(DtRec%(4)) ELSE a$=a$+STR$(DtRec%(4))
a$=a$+”:”
IF DtRec%(5)<10 THEN a$=a$+”0"+STR$(DtRec%(5)) ELSE a$=a$+STR$(DtRec%(5))
AlarmTime$=””
FOR i=1 TO LEN(a$)
    IF MID$(a$,i,1)<>” “ THEN AlarmTime$=AlarmTime$+MID$(a$,i,1)
NEXT i
RETURN

Quit:
WINDOW CLOSE 1
END

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Minecraft 1.20.2 - Popular sandbox build...
Minecraft allows players to build constructions out of textured cubes in a 3D procedurally generated world. Other activities in the game include exploration, gathering resources, crafting, and combat... Read more
HoudahSpot 6.4.1 - Advanced file-search...
HoudahSpot is a versatile desktop search tool. Use HoudahSpot to locate hard-to-find files and keep frequently used files within reach. HoudahSpot is a productivity tool. It is the hub where all the... Read more
coconutBattery 3.9.14 - Displays info ab...
With coconutBattery you're always aware of your current battery health. It shows you live information about your battery such as how often it was charged and how is the current maximum capacity in... Read more
Keynote 13.2 - Apple's presentation...
Easily create gorgeous presentations with the all-new Keynote, featuring powerful yet easy-to-use tools and dazzling effects that will make you a very hard act to follow. The Theme Chooser lets you... Read more
Apple Pages 13.2 - Apple's word pro...
Apple Pages is a powerful word processor that gives you everything you need to create documents that look beautiful. And read beautifully. It lets you work seamlessly between Mac and iOS devices, and... Read more
Numbers 13.2 - Apple's spreadsheet...
With Apple Numbers, sophisticated spreadsheets are just the start. The whole sheet is your canvas. Just add dramatic interactive charts, tables, and images that paint a revealing picture of your data... Read more
Ableton Live 11.3.11 - Record music usin...
Ableton Live lets you create and record music on your Mac. Use digital instruments, pre-recorded sounds, and sampled loops to arrange, produce, and perform your music like never before. Ableton Live... Read more
Affinity Photo 2.2.0 - Digital editing f...
Affinity Photo - redefines the boundaries for professional photo editing software for the Mac. With a meticulous focus on workflow it offers sophisticated tools for enhancing, editing and retouching... Read more
SpamSieve 3.0 - Robust spam filter for m...
SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering. SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more
WhatsApp 2.2338.12 - Desktop client for...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more

Latest Forum Discussions

See All

Square Enix commemorates one of its grea...
One of the most criminally underused properties in the Square Enix roster is undoubtedly Parasite Eve, a fantastic fusion of Resident Evil and Final Fantasy that deserved far more than two PlayStation One Games and a PSP follow-up. Now, however,... | Read more »
Resident Evil Village for iPhone 15 Pro...
During its TGS 2023 stream, Capcom showcased the Following upcoming ports revealed during the Apple iPhone 15 event. Capcom also announced pricing for the mobile (and macOS in the case of the former) ports of Resident Evil 4 Remake and Resident Evil... | Read more »
The iPhone 15 Episode – The TouchArcade...
After a 3 week hiatus The TouchArcade Show returns with another action-packed episode! Well, maybe not so much “action-packed" as it is “packed with talk about the iPhone 15 Pro". Eli, being in a time zone 3 hours ahead of me, as well as being smart... | Read more »
TouchArcade Game of the Week: ‘DERE Veng...
Developer Appsir Games have been putting out genre-defying titles on mobile (and other platforms) for a number of years now, and this week marks the release of their magnum opus DERE Vengeance which has been many years in the making. In fact, if the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for September 22nd, 2023. I’ve had a good night’s sleep, and though my body aches down to the last bit of sinew and meat, I’m at least thinking straight again. We’ve got a lot to look at... | Read more »
TGS 2023: Level-5 Celebrates 25 Years Wi...
Back when I first started covering the Tokyo Game Show for TouchArcade, prolific RPG producer Level-5 could always be counted on for a fairly big booth with a blend of mobile and console games on offer. At recent shows, the company’s presence has... | Read more »
TGS 2023: ‘Final Fantasy’ & ‘Dragon...
Square Enix usually has one of the bigger, more attention-grabbing booths at the Tokyo Game Show, and this year was no different in that sense. The line-ups to play pretty much anything there were among the lengthiest of the show, and there were... | Read more »
Valve Says To Not Expect a Faster Steam...
With the big 20% off discount for the Steam Deck available to celebrate Steam’s 20th anniversary, Valve had a good presence at TGS 2023 with interviews and more. | Read more »
‘Honkai Impact 3rd Part 2’ Revealed at T...
At TGS 2023, HoYoverse had a big presence with new trailers for the usual suspects, but I didn’t expect a big announcement for Honkai Impact 3rd (Free). | Read more »
‘Junkworld’ Is Out Now As This Week’s Ne...
Epic post-apocalyptic tower-defense experience Junkworld () from Ironhide Games is out now on Apple Arcade worldwide. We’ve been covering it for a while now, and even through its soft launches before, but it has returned as an Apple Arcade... | Read more »

Price Scanner via MacPrices.net

New low price: 13″ M2 MacBook Pro for $1049,...
Amazon has the Space Gray 13″ MacBook Pro with an Apple M2 CPU and 256GB of storage in stock and on sale today for $250 off MSRP. Their price is the lowest we’ve seen for this configuration from any... Read more
Apple AirPods 2 with USB-C now in stock and o...
Amazon has Apple’s 2023 AirPods Pro with USB-C now in stock and on sale for $199.99 including free shipping. Their price is $50 off MSRP, and it’s currently the lowest price available for new AirPods... Read more
New low prices: Apple’s 15″ M2 MacBook Airs w...
Amazon has 15″ MacBook Airs with M2 CPUs and 512GB of storage in stock and on sale for $1249 shipped. That’s $250 off Apple’s MSRP, and it’s the lowest price available for these M2-powered MacBook... Read more
New low price: Clearance 16″ Apple MacBook Pr...
B&H Photo has clearance 16″ M1 Max MacBook Pros, 10-core CPU/32-core GPU/1TB SSD/Space Gray or Silver, in stock today for $2399 including free 1-2 day delivery to most US addresses. Their price... Read more
Switch to Red Pocket Mobile and get a new iPh...
Red Pocket Mobile has new Apple iPhone 15 and 15 Pro models on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide service using all the major... Read more
Apple continues to offer a $350 discount on 2...
Apple has Studio Display models available in their Certified Refurbished store for up to $350 off MSRP. Each display comes with Apple’s one-year warranty, with new glass and a case, and ships free.... Read more
Apple’s 16-inch MacBook Pros with M2 Pro CPUs...
Amazon is offering a $250 discount on new Apple 16-inch M2 Pro MacBook Pros for a limited time. Their prices are currently the lowest available for these models from any Apple retailer: – 16″ MacBook... Read more
Closeout Sale: Apple Watch Ultra with Green A...
Adorama haș the Apple Watch Ultra with a Green Alpine Loop on clearance sale for $699 including free shipping. Their price is $100 off original MSRP, and it’s the lowest price we’ve seen for an Apple... Read more
Use this promo code at Verizon to take $150 o...
Verizon is offering a $150 discount on cellular-capable Apple Watch Series 9 and Ultra 2 models for a limited time. Use code WATCH150 at checkout to take advantage of this offer. The fine print: “Up... Read more
New low price: Apple’s 10th generation iPads...
B&H Photo has the 10th generation 64GB WiFi iPad (Blue and Silver colors) in stock and on sale for $379 for a limited time. B&H’s price is $70 off Apple’s MSRP, and it’s the lowest price... Read more

Jobs Board

Housekeeper, *Apple* Valley Villa - Cassia...
Apple Valley Villa, part of a 4-star senior living community, is hiring entry-level Full-Time Housekeepers to join our team! We will train you for this position and Read more
Housekeeper, *Apple* Valley Village - Cassi...
Apple Valley Village Health Care Center, a 4-star rated senior care campus, is hiring a Part-Time Housekeeper to join our team! We will train you for this position! Read more
Optometrist- *Apple* Valley, CA- Target Opt...
Optometrist- Apple Valley, CA- Target Optical Date: Sep 23, 2023 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 796045 At Target Read more
Senior *Apple* iOS CNO Developer (Onsite) -...
…Offense and Defense Experts (CODEX) is in need of smart, motivated and self-driven Apple iOS CNO Developers to join our team to solve real-time cyber challenges. Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.