4
uses exec, timer, amigados, amigalib;
8
{ manifest constants -- 'never will change' }
11
SECSPERHOUR = (60*60);
12
SECSPERDAY = (60*60*24);
16
tr : ptimerequest; { IO block for timer commands }
17
oldtimeval : ttimeval; { timevals to store times }
19
currentval : ttimeval;
21
Function Create_Timer(theUnit : longint) : pTimeRequest;
25
TimeReq : pTimeRequest;
27
TimerPort := CreatePort(Nil, 0);
28
if TimerPort = Nil then
30
TimeReq := pTimeRequest(CreateExtIO(TimerPort,sizeof(tTimeRequest)));
31
if TimeReq = Nil then begin
32
DeletePort(TimerPort);
35
Error := OpenDevice(TIMERNAME, theUnit, pIORequest(TimeReq), 0);
36
if Error <> 0 then begin
37
DeleteExtIO(pIORequest(TimeReq));
38
DeletePort(TimerPort);
41
TimerBase := pointer(TimeReq^.tr_Node.io_Device);
42
Create_Timer := pTimeRequest(TimeReq);
45
Procedure Delete_Timer(WhichTimer : pTimeRequest);
50
WhichPort := WhichTimer^.tr_Node.io_Message.mn_ReplyPort;
51
if assigned(WhichTimer) then begin
52
CloseDevice(pIORequest(WhichTimer));
53
DeleteExtIO(pIORequest(WhichTimer));
55
if assigned(WhichPort) then
56
DeletePort(WhichPort);
59
procedure wait_for_timer(tr : ptimerequest; tv : ptimeval);
61
tr^.tr_node.io_Command := TR_ADDREQUEST; { add a new timer request }
63
{ structure assignment }
64
tr^.tr_time.tv_secs := tv^.tv_secs;
65
tr^.tr_time.tv_micro := tv^.tv_micro;
67
{ post request to the timer -- will go to sleep till done }
71
{ more precise timer than AmigaDOS Delay() }
72
function time_delay(tv : ptimeval; theunit : longint): longint;
76
{ get a pointer to an initialized timer request block }
77
tr := create_timer(theunit);
79
{ any nonzero return says timedelay routine didn't work. }
80
if tr = NIL then time_delay := -1;
82
wait_for_timer(tr, tv);
84
{ deallocate temporary structures }
89
function set_new_time(secs : longint): longint;
93
tr := create_timer(UNIT_MICROHZ);
95
{ non zero return says error }
96
if tr = nil then set_new_time := -1;
98
tr^.tr_time.tv_secs := secs;
99
tr^.tr_time.tv_micro := 0;
100
tr^.tr_node.io_Command := TR_SETSYSTIME;
101
DoIO(pIORequest(tr));
107
function get_sys_time(tv : ptimeval): longint;
111
tr := create_timer( UNIT_MICROHZ );
113
{ non zero return says error }
114
if tr = nil then get_sys_time := -1;
116
tr^.tr_node.io_Command := TR_GETSYSTIME;
117
DoIO(pIORequest(tr));
119
{ structure assignment }
129
procedure show_time(secs : longint);
131
days,hrs,mins : longint;
133
{ Compute days, hours, etc. }
142
writeln('* Hour Minute Second (Days since Jan.1,1978)');
143
writeln('* ', hrs, ': ',mins,': ', secs,' ( ',days, ' )');
149
writeln('Timer test');
151
{ sleep for two seconds }
152
currentval.tv_secs := 2;
153
currentval.tv_micro := 0;
154
time_delay(@currentval, UNIT_VBLANK);
155
writeln('After 2 seconds delay');
157
{ sleep for four seconds }
158
currentval.tv_secs := 4;
159
currentval.tv_micro := 0;
160
time_delay(@currentval, UNIT_VBLANK);
161
writeln('After 4 seconds delay');
163
{ sleep for 500,000 micro-seconds = 1/2 second }
164
currentval.tv_secs := 0;
165
currentval.tv_micro := 500000;
166
time_delay(@currentval, UNIT_MICROHZ);
167
writeln('After 1/2 second delay');
169
writeln('DOS Date command shows: ');
170
Execute('date', 0, 0);
172
{ save what system thinks is the time....we'll advance it temporarily }
173
get_sys_time(@oldtimeval);
174
writeln('Original system time is:');
175
show_time(oldtimeval.tv_secs );
177
writeln('Setting a new system time');
179
seconds := 1000 * SECSPERDAY + oldtimeval.tv_secs;
181
set_new_time( seconds );
182
{ (if user executes the AmigaDOS DATE command now, he will}
183
{ see that the time has advanced something over 1000 days }
185
write('DOS Date command now shows: ');
186
Execute('date', 0, 0);
188
get_sys_time(@mytimeval);
189
writeln('Current system time is:');
190
show_time(mytimeval.tv_secs);
192
{ Added the microseconds part to show that time keeps }
193
{ increasing even though you ask many times in a row }
195
writeln('Now do three TR_GETSYSTIMEs in a row (notice how the microseconds increase)');
197
get_sys_time(@mytimeval);
198
writeln('First TR_GETSYSTIME ',mytimeval.tv_secs,'.', mytimeval.tv_micro);
199
get_sys_time(@mytimeval);
200
writeln('Second TR_GETSYSTIME ',mytimeval.tv_secs,'.', mytimeval.tv_micro);
201
get_sys_time(@mytimeval);
202
writeln('Third TR_GETSYSTIME ',mytimeval.tv_secs,'.', mytimeval.tv_micro);
204
writeln('Resetting to former time');
205
set_new_time(oldtimeval.tv_secs);
207
get_sys_time(@mytimeval);
208
writeln('Current system time is:');
209
show_time(mytimeval.tv_secs);