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)
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/>. */
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"
38 #if defined(HAVE_SYS_TIME_H)
42 #if defined(HAVE_TIME_H)
46 #if defined(HAVE_MALLOC_H)
50 #if defined(HAVE_LIMITS_H)
55 #define NULL (void *)0
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
));
69 EXPORT(InitTimeval
) (void)
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)
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
));
96 EXPORT(InitTimezone
) (void)
102 /* KillTimezone - deallocates the memory associated with an opaque
105 extern "C" struct timezone
*
106 EXPORT(KillTimezone
) (struct timezone
*tv
)
108 #if defined(HAVE_MALLOC_H)
114 /* InitTM - returns a newly created opaque type. */
116 #if defined(HAVE_SYS_TIME_H) && defined(HAVE_MALLOC_H)
117 extern "C" struct tm
*
118 EXPORT(InitTM
) (void)
120 return (struct tm
*)malloc (sizeof (struct tm
));
124 EXPORT(InitTM
) (void)
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)
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)
146 EXPORT(gettimeofday
) (void *tv
, struct timezone
*tz
)
148 return gettimeofday ((struct timeval
*) tv
, tz
);
152 EXPORT(gettimeofday
) (void *tv
, void *tz
)
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)
163 EXPORT(settimeofday
) (void *tv
, struct timezone
*tz
)
165 return settimeofday ((struct timeval
*) tv
, tz
);
169 EXPORT(settimeofday
) (void *tv
, void *tz
)
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
;
185 extern "C" unsigned int
186 EXPORT(GetFractions
) (void *tv
)
188 return (unsigned int)-1;
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
);
204 extern "C" struct tm
*
205 EXPORT(localtime_r
) (void *tv
, struct tm
*m
)
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
)
220 extern "C" unsigned int
221 EXPORT(GetYear
) (void *m
)
223 return (unsigned int)-1;
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
)
236 extern "C" unsigned int
237 EXPORT(GetMonth
) (void *m
)
239 return (unsigned int)-1;
243 /* wraptime_GetDay - returns the day of the month from the structure,
246 #if defined(HAVE_STRUCT_TM)
247 extern "C" unsigned int
248 EXPORT(GetDay
) (struct tm
*m
)
253 extern "C" unsigned int
254 EXPORT(GetDay
) (void *m
)
256 return (unsigned int)-1;
260 /* wraptime_GetHour - returns the hour of the day from the structure,
263 #if defined(HAVE_STRUCT_TM)
264 extern "C" unsigned int
265 EXPORT(GetHour
) (struct tm
*m
)
270 extern "C" unsigned int
271 EXPORT(GetHour
) (void *m
)
273 return (unsigned int)-1;
277 /* wraptime_GetMinute - returns the minute within the hour from the
280 #if defined(HAVE_STRUCT_TM)
281 extern "C" unsigned int
282 EXPORT(GetMinute
) (struct tm
*m
)
287 extern "C" unsigned int
288 EXPORT(GetMinute
) (void *m
)
290 return (unsigned int)-1;
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
)
308 extern "C" unsigned int
309 EXPORT(GetSecond
) (void *m
)
311 return (unsigned int)-1;
315 /* wraptime_GetSummerTime - returns true if summer time is in effect. */
317 #if defined(HAVE_STRUCT_TIMEZONE)
319 EXPORT(GetSummerTime
) (struct timezone
*tz
)
321 return tz
->tz_dsttime
!= 0;
325 EXPORT(GetSummerTime
) (void *tz
)
331 /* wraptime_GetDST - returns the number of minutes west of GMT. */
333 #if defined(HAVE_STRUCT_TIMEZONE)
335 EXPORT(GetDST
) (struct timezone
*tz
)
337 return tz
->tz_minuteswest
;
341 EXPORT(GetDST
) (void *tz
)
346 return (int)((unsigned int)-1);
351 /* SetTimezone - set the timezone field inside timeval, tv. */
353 #if defined(HAVE_STRUCT_TIMEZONE)
355 EXPORT(SetTimezone
) (struct timezone
*tz
, int zone
, int minuteswest
)
357 tz
->tz_dsttime
= zone
;
358 tz
->tz_minuteswest
= minuteswest
;
362 EXPORT(SetTimezone
) (void *tz
, int zone
, int minuteswest
)
367 /* SetTimeval - sets the fields in tm, t, with: second, minute, hour,
368 day, month, year, fractions. */
370 #if defined(HAVE_STRUCT_TIMEVAL)
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
,
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
,
397 /* init - init/finish functions for the module */
399 /* GNU Modula-2 linking hooks. */
402 M2EXPORT(init
) (int, char **, char **)
407 M2EXPORT(fini
) (int, char **, char **)
416 extern "C" void __attribute__((__constructor__
))
417 M2EXPORT(ctor
) (void)
419 m2iso_M2RTS_RegisterModule ("wraptime", M2LIBNAME
,
420 M2EXPORT(init
), M2EXPORT(fini
),