Skip to content

Commit 5db2b58

Browse files
cfriedtjhedberg
authored andcommitted
posix: headers: create a more conformant posix_time.h
Create a header for satisfying POSIX conformance requirements of time.h that is named posix_time.h rather than time.h. The primary reason for doing so, is that the de-facto owner of time.h is the C library. This new header only defines required POSIX symbols that form a strict superset over the ISO C time.h symbols. V2 Signed-off-by: Chris Friedt <[email protected]>
1 parent 9b9e834 commit 5db2b58

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed

include/zephyr/posix/posix_time.h

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/*
2+
* Copyright The Zephyr Project Contributors
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_ZEPHYR_POSIX_POSIX_TIME_H_
8+
#define ZEPHYR_INCLUDE_ZEPHYR_POSIX_POSIX_TIME_H_
9+
10+
#if defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__)
11+
12+
#include <stddef.h>
13+
14+
#include <zephyr/sys/clock.h>
15+
#include <zephyr/toolchain.h>
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
/* clock_t must be defined in the libc time.h */
22+
/* size_t must be defined in the libc stddef.h */
23+
/* time_t must be defined in the libc time.h */
24+
25+
#if !defined(_CLOCKID_T_DECLARED) && !defined(__clockid_t_defined)
26+
typedef unsigned long clockid_t;
27+
#define _CLOCKID_T_DECLARED
28+
#define __clockid_t_defined
29+
#endif
30+
31+
#if !defined(_TIMER_T_DECLARED) && !defined(__timer_t_defined)
32+
typedef unsigned long timer_t;
33+
#define _TIMER_T_DECLARED
34+
#define __timer_t_defined
35+
#endif
36+
37+
#if !defined(_LOCALE_T_DECLARED) && !defined(__locale_t_defined)
38+
#ifdef CONFIG_NEWLIB_LIBC
39+
struct __locale_t;
40+
typedef struct __locale_t *locale_t;
41+
#else
42+
typedef void *locale_t;
43+
#endif
44+
#define _LOCALE_T_DECLARED
45+
#define __locale_t_defined
46+
#endif
47+
48+
#if !defined(_PID_T_DECLARED) && !defined(__pid_t_defined)
49+
typedef int pid_t;
50+
#define _PID_T_DECLARED
51+
#define __pid_t_defined
52+
#endif
53+
54+
#if defined(_POSIX_REALTIME_SIGNALS)
55+
struct sigevent;
56+
#endif
57+
58+
/* struct tm must be defined in the libc time.h */
59+
60+
#if __STDC_VERSION__ >= 201112L
61+
/* struct timespec must be defined in the libc time.h */
62+
#else
63+
#if !defined(_TIMESPEC_DECLARED) && !defined(__timespec_defined)
64+
typedef struct {
65+
time_t tv_sec;
66+
long tv_nsec;
67+
} timespec_t;
68+
#define _TIMESPEC_DECLARED
69+
#define __timespec_defined
70+
#endif
71+
#endif
72+
73+
#if !defined(_ITIMERSPEC_DECLARED) && !defined(__itimerspec_defined)
74+
struct itimerspec {
75+
struct timespec it_interval;
76+
struct timespec it_value;
77+
};
78+
#define _ITIMERSPEC_DECLARED
79+
#define __itimerspec_defined
80+
#endif
81+
82+
/* NULL must be defined in the libc stddef.h */
83+
84+
#ifndef CLOCK_REALTIME
85+
#define CLOCK_REALTIME ((clockid_t)SYS_CLOCK_REALTIME)
86+
#endif
87+
88+
#ifndef CLOCKS_PER_SEC
89+
#if defined(_XOPEN_SOURCE)
90+
#define CLOCKS_PER_SEC 1000000
91+
#else
92+
#define CLOCKS_PER_SEC CONFIG_SYS_CLOCK_TICKS_PER_SEC
93+
#endif
94+
#endif
95+
96+
#if defined(_POSIX_CPUTIME) || defined(__DOXYGEN__)
97+
#ifndef CLOCK_PROCESS_CPUTIME_ID
98+
#define CLOCK_PROCESS_CPUTIME_ID ((clockid_t)2)
99+
#endif
100+
#endif
101+
102+
#if defined(_POSIX_THREAD_CPUTIME) || defined(__DOXYGEN__)
103+
#ifndef CLOCK_THREAD_CPUTIME_ID
104+
#define CLOCK_THREAD_CPUTIME_ID ((clockid_t)3)
105+
#endif
106+
#endif
107+
108+
#if defined(_POSIX_MONOTONIC_CLOCK) || defined(__DOXYGEN__)
109+
#ifndef CLOCK_MONOTONIC
110+
#define CLOCK_MONOTONIC ((clockid_t)SYS_CLOCK_MONOTONIC)
111+
#endif
112+
#endif
113+
114+
#ifndef TIMER_ABSTIME
115+
#define TIMER_ABSTIME SYS_TIMER_ABSTIME
116+
#endif
117+
118+
/* asctime() must be declared in the libc time.h */
119+
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(__DOXYGEN__)
120+
char *asctime_r(const struct tm *ZRESTRICT tm, char *ZRESTRICT buf);
121+
#endif
122+
/* clock() must be declared in the libc time.h */
123+
#if defined(_POSIX_CPUTIME) || defined(__DOXYGEN__)
124+
int clock_getcpuclockid(pid_t pid, clockid_t *clock_id);
125+
#endif
126+
#if defined(_POSIX_TIMERS) || defined(__DOXYGEN__)
127+
int clock_getres(clockid_t clock_id, struct timespec *ts);
128+
int clock_gettime(clockid_t clock_id, struct timespec *ts);
129+
#endif
130+
#if defined(_POSIX_CLOCK_SELECTION) || defined(__DOXYGEN__)
131+
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
132+
struct timespec *rmtp);
133+
#endif
134+
#if defined(_POSIX_TIMERS) || defined(__DOXYGEN__)
135+
int clock_settime(clockid_t clock_id, const struct timespec *ts);
136+
#endif
137+
/* ctime() must be declared in the libc time.h */
138+
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(__DOXYGEN__)
139+
char *ctime_r(const time_t *clock, char *buf);
140+
#endif
141+
/* difftime() must be declared in the libc time.h */
142+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
143+
struct tm *getdate(const char *string);
144+
#endif
145+
/* gmtime() must be declared in the libc time.h */
146+
#if __STDC_VERSION__ >= 202311L
147+
/* gmtime_r() must be declared in the libc time.h */
148+
#else
149+
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(__DOXYGEN__)
150+
struct tm *gmtime_r(const time_t *ZRESTRICT timer, struct tm *ZRESTRICT result);
151+
#endif
152+
#endif
153+
/* localtime() must be declared in the libc time.h */
154+
#if __STDC_VERSION__ >= 202311L
155+
/* localtime_r() must be declared in the libc time.h */
156+
#else
157+
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(__DOXYGEN__)
158+
struct tm *localtime_r(const time_t *ZRESTRICT timer, struct tm *ZRESTRICT result);
159+
#endif
160+
#endif
161+
/* mktime() must be declared in the libc time.h */
162+
#if defined(_POSIX_TIMERS) || defined(__DOXYGEN__)
163+
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
164+
#endif
165+
/* strftime() must be declared in the libc time.h */
166+
size_t strftime_l(char *ZRESTRICT s, size_t maxsize, const char *ZRESTRICT format,
167+
const struct tm *ZRESTRICT timeptr, locale_t locale);
168+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
169+
char *strptime(const char *ZRESTRICT s, const char *ZRESTRICT format, struct tm *ZRESTRICT tm);
170+
#endif
171+
/* time() must be declared in the libc time.h */
172+
#if defined(_POSIX_TIMERS) || defined(__DOXYGEN__)
173+
int timer_create(clockid_t clockId, struct sigevent *ZRESTRICT evp, timer_t *ZRESTRICT timerid);
174+
int timer_delete(timer_t timerid);
175+
int timer_getoverrun(timer_t timerid);
176+
int timer_gettime(timer_t timerid, struct itimerspec *its);
177+
int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
178+
struct itimerspec *ovalue);
179+
#endif
180+
181+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
182+
extern int daylight;
183+
extern long timezone;
184+
#endif
185+
186+
extern char *tzname[];
187+
188+
#ifdef __cplusplus
189+
}
190+
#endif
191+
192+
#endif /* defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__) */
193+
194+
#endif /* ZEPHYR_INCLUDE_ZEPHYR_POSIX_POSIX_TIME_H_ */

0 commit comments

Comments
 (0)