1/* $NetBSD: time.h,v 1.56.2.1 2026/05/07 15:43:06 martin Exp $ */
2
3/*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)time.h 8.3 (Berkeley) 1/21/94
37 */
38
39#ifndef _TIME_H_
40#define _TIME_H_
41
42#include <sys/cdefs.h>
43#include <sys/featuretest.h>
44#include <machine/ansi.h>
45
46#include <sys/null.h>
47
48#ifdef _BSD_CLOCK_T_
49typedef _BSD_CLOCK_T_ clock_t;
50#undef _BSD_CLOCK_T_
51#endif
52
53#ifdef _BSD_TIME_T_
54typedef _BSD_TIME_T_ time_t;
55#undef _BSD_TIME_T_
56#endif
57
58#ifdef _BSD_SIZE_T_
59typedef _BSD_SIZE_T_ size_t;
60#undef _BSD_SIZE_T_
61#endif
62
63#ifdef _BSD_CLOCKID_T_
64typedef _BSD_CLOCKID_T_ clockid_t;
65#undef _BSD_CLOCKID_T_
66#endif
67
68#ifdef _BSD_TIMER_T_
69typedef _BSD_TIMER_T_ timer_t;
70#undef _BSD_TIMER_T_
71#endif
72
73#define CLOCKS_PER_SEC 100
74
75struct tm {
76 int tm_sec; /* seconds after the minute [0-60] */
77 int tm_min; /* minutes after the hour [0-59] */
78 int tm_hour; /* hours since midnight [0-23] */
79 int tm_mday; /* day of the month [1-31] */
80 int tm_mon; /* months since January [0-11] */
81 int tm_year; /* years since 1900 */
82 int tm_wday; /* days since Sunday [0-6] */
83 int tm_yday; /* days since January 1 [0-365] */
84 int tm_isdst; /* Daylight Saving Time flag */
85 long tm_gmtoff; /* offset from UTC in seconds */
86 __aconst char *tm_zone; /* timezone abbreviation */
87};
88
89/*
90 * This represents the minimum value for how long a timezone abbreviation
91 * name is allowed to be, it is also (if >= 254) the default for the max length
92 * we currently allow (that can be changed by compiling libc(localtime.c)
93 * with -DTZNAME_MAXIMUM=N specifying a value at least this long, no less).
94 *
95 * This one should probably be made less, and localtime then use a longer value
96 * (it will use 254 at least), so this would never need to change in the future.
97 */
98#define _TZNAME_MAXIMUM 254
99
100__BEGIN_DECLS
101char *asctime(const struct tm *);
102clock_t clock(void);
103#ifndef __LIBC12_SOURCE__
104char *ctime(const time_t *) __RENAME(__ctime50);
105double difftime(time_t, time_t) __RENAME(__difftime50);
106struct tm *gmtime(const time_t *) __RENAME(__gmtime50);
107struct tm *localtime(const time_t *) __RENAME(__locatime50);
108time_t time(time_t *) __RENAME(__time50);
109time_t mktime(struct tm *) __RENAME(__mktime50);
110#endif
111size_t strftime(char * __restrict, size_t, const char * __restrict,
112 const struct tm * __restrict)
113 __attribute__((__format__(__strftime__, 3, 0)));
114
115#if defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE) || \
116 defined(_NETBSD_SOURCE)
117#ifndef __LIBC12_SOURCE__
118/*
119 * CLK_TCK uses libc's internal __sysconf() to retrieve the machine's
120 * HZ. The value of _SC_CLK_TCK is 39 -- we hard code it so we do not
121 * need to include unistd.h
122 */
123long __sysconf(int);
124#define CLK_TCK (__sysconf(39))
125#endif
126#endif
127
128extern __aconst char *tzname[2];
129#ifndef __LIBC12_SOURCE__
130void tzset(void) __RENAME(__tzset50);
131#endif
132
133/*
134 * X/Open Portability Guide >= Issue 4
135 */
136#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
137extern int daylight;
138#ifndef __LIBC12_SOURCE__
139extern long int timezone __RENAME(__timezone13);
140#endif
141char *strptime(const char * __restrict, const char * __restrict,
142 struct tm * __restrict);
143#endif
144
145#if (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \
146 defined(_NETBSD_SOURCE)
147struct tm *getdate(const char *);
148extern int getdate_err;
149#endif
150
151/* ISO/IEC 9899:2011 7.27.1/3 Components of time */
152#include <sys/timespec.h>
153
154#if (_POSIX_C_SOURCE - 0) >= 199309L || (_XOPEN_SOURCE - 0) >= 500 || \
155 defined(_NETBSD_SOURCE)
156#include <sys/time.h>
157struct sigevent;
158struct itimerspec;
159int clock_nanosleep(clockid_t, int, const struct timespec *, struct timespec *);
160#ifndef __LIBC12_SOURCE__
161int clock_getres(clockid_t, struct timespec *)
162 __RENAME(__clock_getres50);
163int clock_gettime(clockid_t, struct timespec *)
164 __RENAME(__clock_gettime50);
165int clock_settime(clockid_t, const struct timespec *)
166 __RENAME(__clock_settime50);
167int nanosleep(const struct timespec *, struct timespec *)
168 __RENAME(__nanosleep50);
169int timer_gettime(timer_t, struct itimerspec *) __RENAME(__timer_gettime50);
170int timer_settime(timer_t, int, const struct itimerspec * __restrict,
171 struct itimerspec * __restrict) __RENAME(__timer_settime50);
172#endif
173#ifdef _NETBSD_SOURCE
174#include <sys/idtype.h>
175int clock_getcpuclockid2(idtype_t, id_t, clockid_t *);
176#endif
177int clock_getcpuclockid(pid_t, clockid_t *);
178
179int timer_create(clockid_t, struct sigevent * __restrict,
180 timer_t * __restrict);
181int timer_delete(timer_t);
182int timer_getoverrun(timer_t);
183#endif /* _POSIX_C_SOURCE >= 199309 || _XOPEN_SOURCE >= 500 || ... */
184
185#if ((_POSIX_C_SOURCE - 0) >= 199506L && (_POSIX_C_SOURCE - 0) < 202405L) || \
186 (_XOPEN_SOURCE - 0) >= 500 || defined(_REENTRANT) || defined(_NETBSD_SOURCE)
187char *asctime_r(const struct tm * __restrict, char * __restrict);
188#ifndef __LIBC12_SOURCE__
189char *ctime_r(const time_t *, char *) __RENAME(__ctime_r50);
190#endif
191#endif
192
193#if (_POSIX_C_SOURCE - 0) >= 199506L || \
194 (_XOPEN_SOURCE - 0) >= 500 || defined(_REENTRANT) || defined(_NETBSD_SOURCE)
195#ifndef __LIBC12_SOURCE__
196struct tm *gmtime_r(const time_t * __restrict, struct tm * __restrict)
197 __RENAME(__gmtime_r50);
198struct tm *localtime_r(const time_t * __restrict, struct tm * __restrict)
199 __RENAME(__localtime_r50);
200#endif
201#endif
202
203#if (_POSIX_C_SOURCE - 0) >= 200809L || defined(_NETBSD_SOURCE)
204# ifndef __LOCALE_T_DECLARED
205typedef struct _locale *locale_t;
206# define __LOCALE_T_DECLARED
207# endif
208size_t strftime_l(char * __restrict, size_t, const char * __restrict,
209 const struct tm * __restrict, locale_t)
210 __attribute__((__format__(__strftime__, 3, 0)));
211#endif
212
213#if (__STDC_VERSION__ - 0 >= 202311L) || defined(_ISOC23_SOURCE) || \
214 defined(_NETBSD_SOURCE)
215#ifndef __LIBC12_SOURCE__
216time_t timegm(struct tm *) __RENAME(__timegm50);
217#endif
218#endif
219
220#if defined(_NETBSD_SOURCE)
221typedef struct __state *timezone_t;
222
223#ifndef __LIBC12_SOURCE__
224time_t time2posix(time_t) __RENAME(__time2posix50);
225time_t posix2time(time_t) __RENAME(__posix2time50);
226time_t timeoff(struct tm *, long) __RENAME(__timeoff50);
227time_t timelocal(struct tm *) __RENAME(__timelocal50);
228struct tm *offtime(const time_t *, long) __RENAME(__offtime50);
229void tzsetwall(void) __RENAME(__tzsetwall50);
230
231struct tm *offtime_r(const time_t *, long, struct tm *) __RENAME(__offtime_r50);
232struct tm *localtime_rz(timezone_t __restrict, const time_t * __restrict,
233 struct tm * __restrict) __RENAME(__localtime_rz50);
234char *ctime_rz(timezone_t __restrict, const time_t *, char *)
235 __RENAME(__ctime_rz50);
236time_t mktime_z(timezone_t __restrict, struct tm * __restrict)
237 __RENAME(__mktime_z50);
238time_t timelocal_z(timezone_t __restrict, struct tm *)
239 __RENAME(__timelocal_z50);
240time_t time2posix_z(timezone_t __restrict, time_t) __RENAME(__time2posix_z50);
241time_t posix2time_z(timezone_t __restrict, time_t) __RENAME(__posix2time_z50);
242timezone_t tzalloc(const char *) __RENAME(__tzalloc50);
243void tzfree(timezone_t __restrict) __RENAME(__tzfree50);
244const char *tzgetname(timezone_t __restrict, int) __RENAME(__tzgetname50);
245long tzgetgmtoff(timezone_t __restrict, int) __RENAME(__tzgetgmtoff50);
246#endif
247
248size_t strftime_lz(timezone_t __restrict, char * __restrict, size_t,
249 const char * __restrict, const struct tm * __restrict, locale_t)
250 __attribute__((__format__(__strftime__, 4, 0)));
251size_t strftime_z(timezone_t __restrict, char * __restrict, size_t,
252 const char * __restrict, const struct tm * __restrict)
253 __attribute__((__format__(__strftime__, 4, 0)));
254char *strptime_l(const char * __restrict, const char * __restrict,
255 struct tm * __restrict, locale_t);
256
257#endif /* _NETBSD_SOURCE */
258
259/* ISO/IEC 9899:2011 7.27.2.5 The timespec_get function */
260#if defined(_ISOC11_SOURCE) || (__STDC_VERSION__ - 0) >= 201101L || \
261 defined(_NETBSD_SOURCE) || (__cplusplus - 0) >= 201103L || \
262 (_POSIX_C_SOURCE - 0) >= 202405L
263
264#define TIME_UTC 1 /* time elapsed since epoch */
265#if (__STDC_VERSION__ - 0 >= 202311L) || defined(_ISOC23_SOURCE) || \
266 defined(_NETBSD_SOURCE)
267#define TIME_MONOTONIC 2
268#endif
269
270int timespec_get(struct timespec *ts, int base);
271#if (__STDC_VERSION__ - 0 >= 202311L) || defined(_ISOC23_SOURCE) || \
272 defined(_NETBSD_SOURCE)
273int timespec_getres(struct timespec *ts, int base);
274#endif
275#endif
276
277__END_DECLS
278
279#endif /* !_TIME_H_ */