1/* Copyright (C) 1991-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#ifndef _SYS_TIME_H
19#define _SYS_TIME_H 1
20
21#include <features.h>
22
23#include <bits/types.h>
24#include <bits/types/time_t.h>
25#include <bits/types/struct_timeval.h>
26
27#ifndef __suseconds_t_defined
28typedef __suseconds_t suseconds_t;
29# define __suseconds_t_defined
30#endif
31
32#include <sys/select.h>
33
34__BEGIN_DECLS
35
36#ifdef __USE_GNU
37/* Macros for converting between `struct timeval' and `struct timespec'. */
38# define TIMEVAL_TO_TIMESPEC(tv, ts) { \
39 (ts)->tv_sec = (tv)->tv_sec; \
40 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
41}
42# define TIMESPEC_TO_TIMEVAL(tv, ts) { \
43 (tv)->tv_sec = (ts)->tv_sec; \
44 (tv)->tv_usec = (ts)->tv_nsec / 1000; \
45}
46#endif
47
48
49#ifdef __USE_MISC
50/* Structure crudely representing a timezone.
51 This is obsolete and should never be used. */
52struct timezone
53 {
54 int tz_minuteswest; /* Minutes west of GMT. */
55 int tz_dsttime; /* Nonzero if DST is ever in effect. */
56 };
57#endif
58
59/* Get the current time of day, putting it into *TV.
60 If TZ is not null, *TZ must be a struct timezone, and both fields
61 will be set to zero.
62 Calling this function with a non-null TZ is obsolete;
63 use localtime etc. instead.
64 This function itself is semi-obsolete;
65 most callers should use time or clock_gettime instead. */
66#ifndef __USE_TIME_BITS64
67extern int gettimeofday (struct timeval *__restrict __tv,
68 void *__restrict __tz) __THROW __nonnull ((1));
69#else
70# ifdef __REDIRECT_NTH
71extern int __REDIRECT_NTH (gettimeofday, (struct timeval *__restrict __tv,
72 void *__restrict __tz),
73 __gettimeofday64) __nonnull ((1));
74# else
75# define gettimeofday __gettimeofday64
76# endif
77#endif
78
79#ifdef __USE_MISC
80# ifndef __USE_TIME_BITS64
81/* Set the current time of day and timezone information.
82 This call is restricted to the super-user.
83 Setting the timezone in this way is obsolete, but we don't yet
84 warn about it because it still has some uses for which there is
85 no alternative. */
86extern int settimeofday (const struct timeval *__tv,
87 const struct timezone *__tz)
88 __THROW;
89
90/* Adjust the current time of day by the amount in DELTA.
91 If OLDDELTA is not NULL, it is filled in with the amount
92 of time adjustment remaining to be done from the last `adjtime' call.
93 This call is restricted to the super-user. */
94extern int adjtime (const struct timeval *__delta,
95 struct timeval *__olddelta) __THROW;
96# else
97# ifdef __REDIRECT_NTH
98extern int __REDIRECT_NTH (settimeofday, (const struct timeval *__tv,
99 const struct timezone *__tz),
100 __settimeofday64);
101
102extern int __REDIRECT_NTH (adjtime, (const struct timeval *__delta,
103 struct timeval *__olddelta),
104 __adjtime64);
105# else
106# define settimeofday __settimeofday64
107# define adjtime __adjtime64
108# endif
109# endif
110#endif
111
112
113/* Values for the first argument to `getitimer' and `setitimer'. */
114enum __itimer_which
115 {
116 /* Timers run in real time. */
117 ITIMER_REAL = 0,
118#define ITIMER_REAL ITIMER_REAL
119 /* Timers run only when the process is executing. */
120 ITIMER_VIRTUAL = 1,
121#define ITIMER_VIRTUAL ITIMER_VIRTUAL
122 /* Timers run when the process is executing and when
123 the system is executing on behalf of the process. */
124 ITIMER_PROF = 2
125#define ITIMER_PROF ITIMER_PROF
126 };
127
128/* Type of the second argument to `getitimer' and
129 the second and third arguments `setitimer'. */
130struct itimerval
131 {
132 /* Value to put into `it_value' when the timer expires. */
133 struct timeval it_interval;
134 /* Time to the next timer expiration. */
135 struct timeval it_value;
136 };
137
138#if defined __USE_GNU && !defined __cplusplus
139/* Use the nicer parameter type only in GNU mode and not for C++ since the
140 strict C++ rules prevent the automatic promotion. */
141typedef enum __itimer_which __itimer_which_t;
142#else
143typedef int __itimer_which_t;
144#endif
145
146#ifndef __USE_TIME_BITS64
147/* Set *VALUE to the current setting of timer WHICH.
148 Return 0 on success, -1 on errors. */
149extern int getitimer (__itimer_which_t __which,
150 struct itimerval *__value) __THROW;
151
152/* Set the timer WHICH to *NEW. If OLD is not NULL,
153 set *OLD to the old value of timer WHICH.
154 Returns 0 on success, -1 on errors. */
155extern int setitimer (__itimer_which_t __which,
156 const struct itimerval *__restrict __new,
157 struct itimerval *__restrict __old) __THROW;
158
159/* Change the access time of FILE to TVP[0] and the modification time of
160 FILE to TVP[1]. If TVP is a null pointer, use the current time instead.
161 Returns 0 on success, -1 on errors. */
162extern int utimes (const char *__file, const struct timeval __tvp[2])
163 __THROW __nonnull ((1));
164
165#else
166# ifdef __REDIRECT_NTH
167extern int __REDIRECT_NTH (getitimer, (__itimer_which_t __which,
168 struct itimerval *__value),
169 __getitimer64);
170
171extern int __REDIRECT_NTH (setitimer, (__itimer_which_t __which,
172 const struct itimerval *__restrict __new,
173 struct itimerval *__restrict __old),
174 __setitimer64);
175
176extern int __REDIRECT_NTH (utimes, (const char *__file,
177 const struct timeval __tvp[2]),
178 __utimes64) __nonnull ((1));
179# else
180# define getitimer __getitimer64
181# define setitimer __setitimer64
182# define utimes __utimes64
183# endif
184#endif
185
186#ifdef __USE_MISC
187# ifndef __USE_TIME_BITS64
188/* Same as `utimes', but does not follow symbolic links. */
189extern int lutimes (const char *__file, const struct timeval __tvp[2])
190 __THROW __nonnull ((1));
191
192/* Same as `utimes', but takes an open file descriptor instead of a name. */
193extern int futimes (int __fd, const struct timeval __tvp[2]) __THROW;
194# else
195# ifdef __REDIRECT_NTH
196extern int __REDIRECT_NTH (lutimes, (const char *__file,
197 const struct timeval __tvp[2]),
198 __lutimes64) __nonnull ((1));
199
200extern int __REDIRECT_NTH (futimes, (int __fd, const struct timeval __tvp[2]),
201 __futimes64);
202# else
203# define lutimes __lutimes64
204# define futimes __futimes64
205# endif
206# endif
207#endif
208
209#ifdef __USE_GNU
210# ifndef __USE_TIME_BITS64
211/* Change the access time of FILE relative to FD to TVP[0] and the
212 modification time of FILE to TVP[1]. If TVP is a null pointer, use
213 the current time instead. Returns 0 on success, -1 on errors. */
214extern int futimesat (int __fd, const char *__file,
215 const struct timeval __tvp[2]) __THROW;
216# else
217# ifdef __REDIRECT_NTH
218extern int __REDIRECT_NTH (futimesat, (int __fd, const char *__file,
219 const struct timeval __tvp[2]),
220 __futimesat64);
221# else
222# define futimesat __futimesat64
223# endif
224# endif
225#endif
226
227
228#ifdef __USE_MISC
229/* Convenience macros for operations on timevals.
230 NOTE: `timercmp' does not work for >= or <=. */
231# define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
232# define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
233# define timercmp(a, b, CMP) \
234 (((a)->tv_sec == (b)->tv_sec) \
235 ? ((a)->tv_usec CMP (b)->tv_usec) \
236 : ((a)->tv_sec CMP (b)->tv_sec))
237# define timeradd(a, b, result) \
238 do { \
239 (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
240 (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
241 if ((result)->tv_usec >= 1000000) \
242 { \
243 ++(result)->tv_sec; \
244 (result)->tv_usec -= 1000000; \
245 } \
246 } while (0)
247# define timersub(a, b, result) \
248 do { \
249 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
250 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
251 if ((result)->tv_usec < 0) { \
252 --(result)->tv_sec; \
253 (result)->tv_usec += 1000000; \
254 } \
255 } while (0)
256#endif /* Misc. */
257
258__END_DECLS
259
260#endif /* sys/time.h */
261