Debian Codegraph @

gcc/m2/gm2-libs-iso/SysClock.mod

raw
1 (* SysClock.mod implement the ISO SysClock specification.
2
3 Copyright (C) 2009-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
26
27 IMPLEMENTATION MODULE SysClock ;
28
29 FROM wrapclock IMPORT timespec, timezone, isdst, InitTimespec, KillTimespec,
30 GetTimespec, SetTimespec, GetTimeRealtime, SetTimeRealtime ;
31
32 FROM libc IMPORT printf ;
33
34 IMPORT Args ;
35
36 CONST
37 Debugging = FALSE ;
38
39 VAR
40 canget,
41 canset,
42 known : BOOLEAN ;
43
44
45 (*
46 determineAccess - test to see whether we can get and set
47 the time.
48 *)
49
50 PROCEDURE determineAccess ;
51 VAR
52 ts: timespec ;
53 BEGIN
54 IF NOT known
55 THEN
56 ts := InitTimespec () ;
57 canget := GetTimeRealtime (ts) = 0 ;
58 canset := canget AND (SetTimeRealtime (ts) = 0) ;
59 ts := KillTimespec (ts) ;
60 known := TRUE
61 END
62 END determineAccess ;
63
64
65 PROCEDURE CanGetClock () : BOOLEAN ;
66 (* Tests if the clock can be read *)
67 BEGIN
68 determineAccess ;
69 RETURN canget
70 END CanGetClock ;
71
72
73 PROCEDURE CanSetClock () : BOOLEAN ;
74 (* Tests if the clock can be set *)
75 BEGIN
76 determineAccess ;
77 RETURN canset
78 END CanSetClock ;
79
80
81 PROCEDURE IsValidDateTime (userData: DateTime) : BOOLEAN ;
82 (* Tests if the value of userData is a valid *)
83 BEGIN
84 WITH userData DO
85 CASE month OF
86
87 1: |
88 2: IF ((year MOD 4=0) AND (year MOD 100#0)) OR (year MOD 400=0)
89 THEN
90 RETURN day<=29
91 ELSE
92 RETURN day<=28
93 END |
94 3: |
95 4: RETURN day<=30 |
96 5: |
97 6: RETURN day<=30 |
98 7: |
99 8: |
100 9: RETURN day<=30 |
101 10: |
102 11: RETURN day<=30 |
103 12:
104
105 END
106 END ;
107 RETURN( TRUE )
108 END IsValidDateTime ;
109
110
111 (*
112 DivMod - returns seconds MOD modulus. It also divides seconds by modulus.
113 *)
114
115 PROCEDURE DivMod (VAR seconds: LONGCARD; modulus: LONGCARD) : LONGCARD ;
116 VAR
117 result: LONGCARD ;
118 BEGIN
119 result := seconds MOD modulus ;
120 seconds := seconds DIV modulus ;
121 RETURN result
122 END DivMod ;
123
124
125 (*
126 daysInYear - return the number of days in year up to month/day.
127 *)
128
129 PROCEDURE daysInYear (day, month, year: LONGCARD) : LONGCARD ;
130 BEGIN
131 WHILE month > 1 DO
132 INC (day, daysInMonth (year, month)) ;
133 DEC (month)
134 END ;
135 RETURN day
136 END daysInYear ;
137
138
139 (*
140 ExtractDate - extracts the year, month, day from secs. days is the
141 total days since 1970.
142 *)
143
144 PROCEDURE ExtractDate (days: LONGCARD;
145 VAR year: CARDINAL; VAR month: Month; VAR day: Day) ;
146 VAR
147 testMonth,
148 testYear : CARDINAL ;
149 monthOfDays,
150 yearOfDays : LONGCARD ;
151 BEGIN
152 testYear := 1970 ;
153 LOOP
154 yearOfDays := daysInYear (31, 12, testYear) ;
155 IF days < yearOfDays
156 THEN
157 year := testYear ;
158 testMonth := 1 ;
159 LOOP
160 monthOfDays := daysInMonth (year, testMonth) ;
161 IF days < monthOfDays
162 THEN
163 day := VAL (Day, days) + MIN (Day) ;
164 month := VAL (Month, testMonth) ;
165 RETURN
166 END ;
167 DEC (days, monthOfDays) ;
168 INC (testMonth)
169 END
170 ELSE
171 DEC (days, yearOfDays) ;
172 INC (testYear)
173 END
174 END
175 END ExtractDate ;
176
177
178 (*
179 EpochTime - assigns all fields of userData to 0 or FALSE.
180 *)
181
182 PROCEDURE EpochTime (VAR userData: DateTime) ;
183 BEGIN
184 WITH userData DO
185 second := 0 ;
186 minute := 0 ;
187 hour := 0 ;
188 year := 0 ;
189 month := 0 ;
190 day := 0 ;
191 fractions := 0 ;
192 zone := 0 ;
193 summerTimeFlag := FALSE
194 END
195 END EpochTime ;
196
197
198 PROCEDURE GetClock (VAR userData: DateTime) ;
199 (* Assigns local date and time of the day to userData *)
200 VAR
201 ts : timespec ;
202 nano, sec: LONGCARD ;
203 offset : LONGINT ;
204 BEGIN
205 IF CanGetClock ()
206 THEN
207 ts := InitTimespec () ;
208 IF GetTimeRealtime (ts) = 0
209 THEN
210 IF GetTimespec (ts, sec, nano) = 1
211 THEN
212 offset := timezone () ;
213 IF Debugging
214 THEN
215 printf ("getclock = %ld\n", sec)
216 END ;
217 sec := VAL (LONGINT, sec) + offset ;
218 IF Debugging
219 THEN
220 printf ("getclock = %ld\n", sec)
221 END ;
222 WITH userData DO
223 (* Here we keep dividing sec by max seconds, minutes, hours
224 to convert sec into total days since epoch. *)
225 second := VAL (Sec, DivMod (sec, MAX (Sec) + 1)) ;
226 minute := VAL (Min, DivMod (sec, MAX (Min) + 1)) ;
227 hour := VAL (Hour, DivMod (sec, MAX (Hour) + 1)) ;
228 ExtractDate (sec, year, month, day) ;
229 fractions := nano DIV ((1000 * 1000 * 1000) DIV maxSecondParts) ;
230 zone := - (offset DIV 60) ;
231 summerTimeFlag := (isdst () = 1)
232 END
233 ELSE
234 EpochTime (userData)
235 END
236 ELSE
237 EpochTime (userData)
238 END ;
239 ts := KillTimespec (ts)
240 END
241 END GetClock ;
242
243
244 (*
245 daysInMonth - returns how many days there are in a month.
246 *)
247
248 PROCEDURE daysInMonth (year, month: CARDINAL) : LONGCARD ;
249 BEGIN
250 CASE month OF
251
252 1: |
253 2: IF ((year MOD 4=0) AND (year MOD 100#0)) OR (year MOD 400=0)
254 THEN
255 RETURN 29
256 ELSE
257 RETURN 28
258 END |
259 3: |
260 4: RETURN 30 |
261 5: |
262 6: RETURN 30 |
263 7: |
264 8: |
265 9: RETURN 30 |
266 10: |
267 11: RETURN 30 |
268 12: |
269
270 END ;
271 RETURN 31
272 END daysInMonth ;
273
274
275 (*
276 totalYear - return the sum of all days prior to year from the epoch.
277 *)
278
279 PROCEDURE totalYear (year: LONGCARD) : LONGCARD ;
280 VAR
281 lastYear,
282 result : LONGCARD ;
283 BEGIN
284 lastYear := 1970 ;
285 result := 0 ;
286 WHILE lastYear < year DO
287 INC (result, daysInYear (31, 12, lastYear)) ;
288 INC (lastYear)
289 END ;
290 RETURN result
291 END totalYear ;
292
293
294 (*
295 totalSeconds - returns the total seconds
296 *)
297
298 PROCEDURE totalSeconds (second, minute, hour,
299 day, month, year: LONGCARD) : LONGCARD ;
300 VAR
301 result: LONGCARD ;
302 BEGIN
303 result := second
304 + minute * (MAX (Sec) + 1)
305 + hour * ((MAX (Min) + 1) * (MAX (Sec) + 1))
306 + ((daysInYear (day, month, year) + totalYear (year))
307 * ((MAX (Hour) + 1) * ((MAX (Min) + 1) * (MAX (Sec) + 1)))) ;
308 RETURN result
309 END totalSeconds ;
310
311
312 PROCEDURE SetClock (userData: DateTime);
313 VAR
314 ts : timespec ;
315 nano, sec: LONGCARD ;
316 offset : LONGINT ;
317 BEGIN
318 IF Debugging
319 THEN
320 sec := totalSeconds (userData.second, userData.minute, userData.hour,
321 VAL (CARDINAL, userData.day) - MIN (Day),
322 userData.month, userData.year) ;
323 printf ("setclock = %ld\n", sec);
324 offset := timezone () ;
325 sec := VAL (LONGINT, sec) - offset ;
326 printf ("setclock = %ld\n", sec);
327 END ;
328 IF CanSetClock ()
329 THEN
330 ts := InitTimespec () ;
331 nano := VAL (LONGCARD, userData.fractions * 1000) ;
332 sec := totalSeconds (userData.second, userData.minute, userData.hour,
333 VAL (CARDINAL, userData.day) - MIN (Day),
334 userData.month, userData.year) ;
335 offset := timezone () ;
336 sec := VAL (LONGINT, sec) - offset ;
337 IF SetTimespec (ts, sec, nano) = 1
338 THEN
339 IF SetTimeRealtime (ts) = 0
340 THEN
341 END
342 END ;
343 ts := KillTimespec (ts)
344 END
345 END SetClock ;
346
347
348 BEGIN
349 known := FALSE ;
350 canset := FALSE ;
351 canget := FALSE
352 END SysClock.