1 | /* Copyright (C) 1991-2021 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 |
28 | typedef __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. */ |
52 | struct 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 | extern int gettimeofday (struct timeval *__restrict __tv, |
67 | void *__restrict __tz) __THROW __nonnull ((1)); |
68 | |
69 | #ifdef __USE_MISC |
70 | /* Set the current time of day and timezone information. |
71 | This call is restricted to the super-user. |
72 | Setting the timezone in this way is obsolete, but we don't yet |
73 | warn about it because it still has some uses for which there is |
74 | no alternative. */ |
75 | extern int settimeofday (const struct timeval *__tv, |
76 | const struct timezone *__tz) |
77 | __THROW; |
78 | |
79 | /* Adjust the current time of day by the amount in DELTA. |
80 | If OLDDELTA is not NULL, it is filled in with the amount |
81 | of time adjustment remaining to be done from the last `adjtime' call. |
82 | This call is restricted to the super-user. */ |
83 | extern int adjtime (const struct timeval *__delta, |
84 | struct timeval *__olddelta) __THROW; |
85 | #endif |
86 | |
87 | |
88 | /* Values for the first argument to `getitimer' and `setitimer'. */ |
89 | enum __itimer_which |
90 | { |
91 | /* Timers run in real time. */ |
92 | ITIMER_REAL = 0, |
93 | #define ITIMER_REAL ITIMER_REAL |
94 | /* Timers run only when the process is executing. */ |
95 | ITIMER_VIRTUAL = 1, |
96 | #define ITIMER_VIRTUAL ITIMER_VIRTUAL |
97 | /* Timers run when the process is executing and when |
98 | the system is executing on behalf of the process. */ |
99 | ITIMER_PROF = 2 |
100 | #define ITIMER_PROF ITIMER_PROF |
101 | }; |
102 | |
103 | /* Type of the second argument to `getitimer' and |
104 | the second and third arguments `setitimer'. */ |
105 | struct itimerval |
106 | { |
107 | /* Value to put into `it_value' when the timer expires. */ |
108 | struct timeval it_interval; |
109 | /* Time to the next timer expiration. */ |
110 | struct timeval it_value; |
111 | }; |
112 | |
113 | #if defined __USE_GNU && !defined __cplusplus |
114 | /* Use the nicer parameter type only in GNU mode and not for C++ since the |
115 | strict C++ rules prevent the automatic promotion. */ |
116 | typedef enum __itimer_which __itimer_which_t; |
117 | #else |
118 | typedef int __itimer_which_t; |
119 | #endif |
120 | |
121 | /* Set *VALUE to the current setting of timer WHICH. |
122 | Return 0 on success, -1 on errors. */ |
123 | extern int getitimer (__itimer_which_t __which, |
124 | struct itimerval *__value) __THROW; |
125 | |
126 | /* Set the timer WHICH to *NEW. If OLD is not NULL, |
127 | set *OLD to the old value of timer WHICH. |
128 | Returns 0 on success, -1 on errors. */ |
129 | extern int setitimer (__itimer_which_t __which, |
130 | const struct itimerval *__restrict __new, |
131 | struct itimerval *__restrict __old) __THROW; |
132 | |
133 | /* Change the access time of FILE to TVP[0] and the modification time of |
134 | FILE to TVP[1]. If TVP is a null pointer, use the current time instead. |
135 | Returns 0 on success, -1 on errors. */ |
136 | extern int utimes (const char *__file, const struct timeval __tvp[2]) |
137 | __THROW __nonnull ((1)); |
138 | |
139 | #ifdef __USE_MISC |
140 | /* Same as `utimes', but does not follow symbolic links. */ |
141 | extern int lutimes (const char *__file, const struct timeval __tvp[2]) |
142 | __THROW __nonnull ((1)); |
143 | |
144 | /* Same as `utimes', but takes an open file descriptor instead of a name. */ |
145 | extern int futimes (int __fd, const struct timeval __tvp[2]) __THROW; |
146 | #endif |
147 | |
148 | #ifdef __USE_GNU |
149 | /* Change the access time of FILE relative to FD to TVP[0] and the |
150 | modification time of FILE to TVP[1]. If TVP is a null pointer, use |
151 | the current time instead. Returns 0 on success, -1 on errors. */ |
152 | extern int futimesat (int __fd, const char *__file, |
153 | const struct timeval __tvp[2]) __THROW; |
154 | #endif |
155 | |
156 | |
157 | #ifdef __USE_MISC |
158 | /* Convenience macros for operations on timevals. |
159 | NOTE: `timercmp' does not work for >= or <=. */ |
160 | # define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) |
161 | # define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) |
162 | # define timercmp(a, b, CMP) \ |
163 | (((a)->tv_sec == (b)->tv_sec) \ |
164 | ? ((a)->tv_usec CMP (b)->tv_usec) \ |
165 | : ((a)->tv_sec CMP (b)->tv_sec)) |
166 | # define timeradd(a, b, result) \ |
167 | do { \ |
168 | (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \ |
169 | (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \ |
170 | if ((result)->tv_usec >= 1000000) \ |
171 | { \ |
172 | ++(result)->tv_sec; \ |
173 | (result)->tv_usec -= 1000000; \ |
174 | } \ |
175 | } while (0) |
176 | # define timersub(a, b, result) \ |
177 | do { \ |
178 | (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ |
179 | (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ |
180 | if ((result)->tv_usec < 0) { \ |
181 | --(result)->tv_sec; \ |
182 | (result)->tv_usec += 1000000; \ |
183 | } \ |
184 | } while (0) |
185 | #endif /* Misc. */ |
186 | |
187 | __END_DECLS |
188 | |
189 | #endif /* sys/time.h */ |
190 | |