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