KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 machine/time.h
4 Copyright (C) 2023 Lawrence Sebald
5 Copyright (C) 2024 Falco Girgis
6*/
7
8/** \file machine/time.h
9 \brief KOS-implementation of select C11 and POSIX extensions
10
11 Add select POSIX extensions, C11, and C23 functionality to time.h which are not
12 present within Newlib.
13
14 \remark
15 This will probably go away at some point in the future, if/when Newlib gets
16 an implementation of this function. But for now, it's here.
17
18 \todo
19 - Implement _POSIX_TIMERS, which requires POSIX signals back-end.
20 - Implement thread-specific CPU time
21
22 \author Lawrence Sebald
23 \author Falco Girgis
24*/
25
26#ifndef _TIME_H_
27 #error "Do not include this file directly. Use <time.h> instead."
28#endif /* !_TIME_H_ */
29
30#ifndef __KOS_TIME_H
31#define __KOS_TIME_H
32
33#include <kos/cdefs.h>
34
35__BEGIN_DECLS
36
37/** \cond */
38
39/* Required definition for a fully C23-compliant <time.h> */
40#define __STDC_VERSION_TIME_H__ 202311L
41
42/* Microsecond resolution for clock(), per POSIX standard.. */
43#define _CLOCKS_PER_SEC_ 1000000
44
45/* =============== Enable the following for >=c11, >=c++17 =================== */
46#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
47 (defined(__cplusplus) && (__cplusplus >= 201703L))
48
49/* Only supported base time in C11. */
50#define TIME_UTC 1
51
52/* C11 nanosecond-resolution timing. */
53struct timespec;
54extern int timespec_get(struct timespec *ts, int base);
55#endif
56
57/* ============ Enable the following for >=C2x, >C++20+, KOS ============== */
58#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202000L)) || \
59 (defined(__cplusplus) && (__cplusplus > 202002L)) || \
60 defined(__KOS_LIBC)
61
62/* New POSIX-equivalent base times in C23. */
63#define TIME_MONOTONIC 2
64#define TIME_ACTIVE 3
65#define TIME_THREAD_ACTIVE 4
66
67/* Query for the resolution of a time base. */
68extern int timespec_getres(struct timespec *ts, int base);
69#endif
70
71/* ===================== Enable the following for >=c2y ================== */
72#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202300L)
73
74/* Deprecate legacy time formatters with static internal state. */
75struct tm;
76[[deprecated]] extern char *asctime(const struct tm *timeptr);
77[[deprecated]] extern char *ctime(const __time_t *timer);
78#endif
79
80/* =========== Enable the following for >=c2y, >c++20, KOS ============== */
81#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202300L)) || \
82 (defined(__cplusplus) && (__cplusplus > 202002L)) || \
83 defined(__KOS_LIBC)
84
85/* POSIX reentrant equivalents of gmtime and localtime added to C23. */
86struct tm;
87extern struct tm *gmtime_r(const __time_t *timer, struct tm *timeptr);
88extern struct tm *localtime_r(const __time_t *timer, struct tm *timeptr);
89
90/* C23 added POSIX gmtime() for UTC broken-down time to a Unix timestamp. */
91extern __time_t timegm(struct tm *timeptr);
92
93#endif
94
95/* ================= POSIX.1b (1993) realtime option macros ================= */
96
97/* KOS implements the clock half of the POSIX Timers option (clock_gettime(),
98 clock_settime(), clock_getres() and nanosleep()) along with the monotonic
99 and CPU-time clocks. */
100#ifndef _POSIX_TIMERS
101#define _POSIX_TIMERS 200809L
102#endif
103
104#ifndef _POSIX_MONOTONIC_CLOCK
105#define _POSIX_MONOTONIC_CLOCK 200809L
106#endif
107
108#ifndef _POSIX_CPUTIME
109#define _POSIX_CPUTIME 200809L
110#endif
111
112#ifndef _POSIX_THREAD_CPUTIME
113#define _POSIX_THREAD_CPUTIME 200809L
114#endif
115
116/** \endcond */
117
118__END_DECLS
119
120#endif /* !__KOS_TIME_H */
Various common macros used throughout the codebase.