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

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.