libstdc++: Fix std::chrono::file_clock conversions for low-precision times
[official-gcc.git] / libgm2 / libm2iso / wraptime.cc
blobf1158ae7d38f05ef225eea17d879e9b988b07ad4
1 /* wraptime.c provides access to time related system calls.
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
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.
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.
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.
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/>. */
27 #include "config.h"
28 #include <m2rts.h>
30 #define EXPORT(FUNC) m2iso ## _wraptime_ ## FUNC
31 #define M2EXPORT(FUNC) m2iso ## _M2_wraptime_ ## FUNC
32 #define M2LIBNAME "m2iso"
34 #if defined(HAVE_SYS_TYPES_H)
35 #include "sys/types.h"
36 #endif
38 #if defined(HAVE_SYS_TIME_H)
39 #include "sys/time.h"
40 #endif
42 #if defined(HAVE_TIME_H)
43 #include "time.h"
44 #endif
46 #if defined(HAVE_MALLOC_H)
47 #include "malloc.h"
48 #endif
50 #if defined(HAVE_LIMITS_H)
51 #include "limits.h"
52 #endif
54 #if !defined(NULL)
55 #define NULL (void *)0
56 #endif
59 /* InitTimeval returns a newly created opaque type. */
61 #if defined(HAVE_STRUCT_TIMEVAL) && defined(HAVE_MALLOC_H)
62 extern "C" struct timeval *
63 EXPORT(InitTimeval) (void)
65 return (struct timeval *)malloc (sizeof (struct timeval));
67 #else
68 extern "C" void *
69 EXPORT(InitTimeval) (void)
71 return NULL;
73 #endif
75 /* KillTimeval deallocates the memory associated with an opaque type. */
77 extern "C" struct timeval *
78 EXPORT(KillTimeval) (void *tv)
80 #if defined(HAVE_MALLOC_H)
81 free (tv);
82 #endif
83 return NULL;
86 /* InitTimezone returns a newly created opaque type. */
88 #if defined(HAVE_STRUCT_TIMEZONE) && defined(HAVE_MALLOC_H)
89 extern "C" struct timezone *
90 EXPORT(InitTimezone) (void)
92 return (struct timezone *)malloc (sizeof (struct timezone));
94 #else
95 extern "C" void *
96 EXPORT(InitTimezone) (void)
98 return NULL;
100 #endif
102 /* KillTimezone - deallocates the memory associated with an opaque
103 type. */
105 extern "C" struct timezone *
106 EXPORT(KillTimezone) (struct timezone *tv)
108 #if defined(HAVE_MALLOC_H)
109 free (tv);
110 #endif
111 return NULL;
114 /* InitTM - returns a newly created opaque type. */
116 #if defined(HAVE_STRUCT_TM) && defined(HAVE_MALLOC_H)
117 extern "C" struct tm *
118 EXPORT(InitTM) (void)
120 return (struct tm *)malloc (sizeof (struct tm));
122 #else
123 extern "C" void *
124 EXPORT(InitTM) (void)
126 return NULL;
128 #endif
130 /* KillTM - deallocates the memory associated with an opaque type. */
132 extern "C" struct tm *
133 EXPORT(KillTM) (struct tm *tv)
135 #if defined(HAVE_MALLOC_H)
136 free (tv);
137 #endif
138 return NULL;
141 /* gettimeofday - calls gettimeofday(2) with the same parameters, tv,
142 and, tz. It returns 0 on success. */
144 #if defined(HAVE_STRUCT_TIMEZONE) && defined(HAVE_GETTIMEOFDAY)
145 extern "C" int
146 EXPORT(gettimeofday) (void *tv, struct timezone *tz)
148 return gettimeofday (tv, tz);
150 #else
151 extern "C" int
152 EXPORT(gettimeofday) (void *tv, void *tz)
154 return -1;
156 #endif
158 /* settimeofday - calls settimeofday(2) with the same parameters, tv,
159 and, tz. It returns 0 on success. */
161 #if defined(HAVE_STRUCT_TIMEZONE) && defined(HAVE_SETTIMEOFDAY)
162 extern "C" int
163 EXPORT(settimeofday) (void *tv, struct timezone *tz)
165 return settimeofday (tv, tz);
167 #else
168 extern "C" int
169 EXPORT(settimeofday) (void *tv, void *tz)
171 return -1;
173 #endif
175 /* wraptime_GetFractions - returns the tv_usec field inside the
176 timeval structure. */
178 #if defined(HAVE_STRUCT_TIMEVAL)
179 extern "C" unsigned int
180 EXPORT(GetFractions) (struct timeval *tv)
182 return (unsigned int)tv->tv_usec;
184 #else
185 extern "C" unsigned int
186 EXPORT(GetFractions) (void *tv)
188 return (unsigned int)-1;
190 #endif
192 /* localtime_r - returns the tm parameter, m, after it has been
193 assigned with appropriate contents determined by, tv. Notice that
194 this procedure function expects, timeval, as its first parameter
195 and not a time_t (as expected by the posix equivalent). */
197 #if defined(HAVE_STRUCT_TIMEVAL)
198 extern "C" struct tm *
199 EXPORT(localtime_r) (struct timeval *tv, struct tm *m)
201 return localtime_r (&tv->tv_sec, m);
203 #else
204 extern "C" struct tm *
205 EXPORT(localtime_r) (void *tv, struct tm *m)
207 return m;
209 #endif
211 /* wraptime_GetYear - returns the year from the structure, m. */
213 #if defined(HAVE_STRUCT_TM)
214 extern "C" unsigned int
215 EXPORT(GetYear) (struct tm *m)
217 return m->tm_year;
219 #else
220 extern "C" unsigned int
221 EXPORT(GetYear) (void *m)
223 return (unsigned int)-1;
225 #endif
227 /* wraptime_GetMonth - returns the month from the structure, m. */
229 #if defined(HAVE_STRUCT_TM)
230 extern "C" unsigned int
231 EXPORT(GetMonth) (struct tm *m)
233 return m->tm_mon;
235 #else
236 extern "C" unsigned int
237 EXPORT(GetMonth) (void *m)
239 return (unsigned int)-1;
241 #endif
243 /* wraptime_GetDay - returns the day of the month from the structure,
244 m. */
246 #if defined(HAVE_STRUCT_TM)
247 extern "C" unsigned int
248 EXPORT(GetDay) (struct tm *m)
250 return m->tm_mday;
252 #else
253 extern "C" unsigned int
254 EXPORT(GetDay) (void *m)
256 return (unsigned int)-1;
258 #endif
260 /* wraptime_GetHour - returns the hour of the day from the structure,
261 m. */
263 #if defined(HAVE_STRUCT_TM)
264 extern "C" unsigned int
265 EXPORT(GetHour) (struct tm *m)
267 return m->tm_hour;
269 #else
270 extern "C" unsigned int
271 EXPORT(GetHour) (void *m)
273 return (unsigned int)-1;
275 #endif
277 /* wraptime_GetMinute - returns the minute within the hour from the
278 structure, m. */
280 #if defined(HAVE_STRUCT_TM)
281 extern "C" unsigned int
282 EXPORT(GetMinute) (struct tm *m)
284 return m->tm_min;
286 #else
287 extern "C" unsigned int
288 EXPORT(GetMinute) (void *m)
290 return (unsigned int)-1;
292 #endif
294 /* wraptime_GetSecond - returns the seconds in the minute from the
295 structure, m. The return value will always be in the range 0..59.
296 A leap minute of value 60 will be truncated to 59. */
298 #if defined(HAVE_STRUCT_TM)
299 extern "C" unsigned int
300 EXPORT(GetSecond) (struct tm *m)
302 if (m->tm_sec == 60)
303 return 59;
304 else
305 return m->tm_sec;
307 #else
308 extern "C" unsigned int
309 EXPORT(GetSecond) (void *m)
311 return (unsigned int)-1;
313 #endif
315 /* wraptime_GetSummerTime - returns true if summer time is in effect. */
317 #if defined(HAVE_STRUCT_TIMEZONE)
318 extern "C" bool
319 EXPORT(GetSummerTime) (struct timezone *tz)
321 return tz->tz_dsttime != 0;
323 #else
324 extern "C" bool
325 EXPORT(GetSummerTime) (void *tz)
327 return false;
329 #endif
331 /* wraptime_GetDST - returns the number of minutes west of GMT. */
333 #if defined(HAVE_STRUCT_TIMEZONE)
334 extern "C" int
335 EXPORT(GetDST) (struct timezone *tz)
337 return tz->tz_minuteswest;
339 #else
340 extern "C" int
341 EXPORT(GetDST) (void *tz)
343 #if defined(INT_MIN)
344 return INT_MIN;
345 #else
346 return (int)((unsigned int)-1);
347 #endif
349 #endif
351 /* SetTimezone - set the timezone field inside timeval, tv. */
353 #if defined(HAVE_STRUCT_TIMEZONE)
354 extern "C" void
355 EXPORT(SetTimezone) (struct timezone *tz, int zone, int minuteswest)
357 tz->tz_dsttime = zone;
358 tz->tz_minuteswest = minuteswest;
360 #else
361 extern "C" void
362 EXPORT(SetTimezone) (void *tz, int zone, int minuteswest)
365 #endif
367 /* SetTimeval - sets the fields in tm, t, with: second, minute, hour,
368 day, month, year, fractions. */
370 #if defined(HAVE_STRUCT_TIMEVAL)
371 extern "C" void
372 EXPORT(SetTimeval) (struct tm *t, unsigned int second, unsigned int minute,
373 unsigned int hour, unsigned int day, unsigned int month,
374 unsigned int year, unsigned int yday, unsigned int wday,
375 unsigned int isdst)
377 t->tm_sec = second;
378 t->tm_min = minute;
379 t->tm_hour = hour;
380 t->tm_mday = day;
381 t->tm_mon = month;
382 t->tm_year = year;
383 t->tm_yday = yday;
384 t->tm_wday = wday;
385 t->tm_isdst = isdst;
387 #else
388 extern "C" void
389 EXPORT(SetTimeval) (void *t, unsigned int second, unsigned int minute,
390 unsigned int hour, unsigned int day, unsigned int month,
391 unsigned int year, unsigned int yday, unsigned int wday,
392 unsigned int isdst)
395 #endif
397 /* init - init/finish functions for the module */
399 /* GNU Modula-2 linking hooks. */
401 extern "C" void
402 M2EXPORT(init) (int, char **, char **)
406 extern "C" void
407 M2EXPORT(fini) (int, char **, char **)
411 extern "C" void
412 M2EXPORT(dep) (void)
416 extern "C" void __attribute__((__constructor__))
417 M2EXPORT(ctor) (void)
419 m2iso_M2RTS_RegisterModule ("wraptime", M2LIBNAME,
420 M2EXPORT(init), M2EXPORT(fini),
421 M2EXPORT(dep));