SDL 3.0
SDL_time.h
Go to the documentation of this file.
1/*
2Simple DirectMedia Layer
3Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5This software is provided 'as-is', without any express or implied
6warranty. In no event will the authors be held liable for any damages
7arising from the use of this software.
8
9Permission is granted to anyone to use this software for any purpose,
10including commercial applications, and to alter it and redistribute it
11freely, subject to the following restrictions:
12
131. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
172. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
193. This notice may not be removed or altered from any source distribution.
20*/
21
22#ifndef SDL_time_h_
23#define SDL_time_h_
24
25/**
26 * # CategoryTime
27 *
28 * SDL realtime clock and date/time routines.
29 */
30
31#include <SDL3/SDL_error.h>
32#include <SDL3/SDL_stdinc.h>
33
34#include <SDL3/SDL_begin_code.h>
35/* Set up for C function definitions, even when using C++ */
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/**
41 * A structure holding a calendar date and time broken down into its
42 * components.
43 *
44 * \since This struct is available since SDL 3.0.0.
45 */
46typedef struct SDL_DateTime
47{
48 int year; /**< Year */
49 int month; /**< Month [01-12] */
50 int day; /**< Day of the month [01-31] */
51 int hour; /**< Hour [0-23] */
52 int minute; /**< Minute [0-59] */
53 int second; /**< Seconds [0-60] */
54 int nanosecond; /**< Nanoseconds [0-999999999] */
55 int day_of_week; /**< Day of the week [0-6] (0 being Sunday) */
56 int utc_offset; /**< Seconds east of UTC */
58
59/**
60 * The preferred date format of the current system locale.
61 *
62 * \since This enum is available since SDL 3.0.0.
63 *
64 * \sa SDL_GetDateTimeLocalePreferences
65 */
66typedef enum SDL_DateFormat
67{
68 SDL_DATE_FORMAT_YYYYMMDD = 0, /**< Year/Month/Day */
69 SDL_DATE_FORMAT_DDMMYYYY = 1, /**< Day/Month/Year */
70 SDL_DATE_FORMAT_MMDDYYYY = 2 /**< Month/Day/Year */
72
73/**
74 * The preferred time format of the current system locale.
75 *
76 * \since This enum is available since SDL 3.0.0.
77 *
78 * \sa SDL_GetDateTimeLocalePreferences
79 */
80typedef enum SDL_TimeFormat
81{
82 SDL_TIME_FORMAT_24HR = 0, /**< 24 hour time */
83 SDL_TIME_FORMAT_12HR = 1 /**< 12 hour time */
85
86/**
87 * Gets the current preferred date and time format for the system locale.
88 *
89 * This might be a "slow" call that has to query the operating system. It's
90 * best to ask for this once and save the results. However, the preferred
91 * formats can change, usually because the user has changed a system
92 * preference outside of your program.
93 *
94 * \param dateFormat a pointer to the SDL_DateFormat to hold the returned date
95 * format, may be NULL.
96 * \param timeFormat a pointer to the SDL_TimeFormat to hold the returned time
97 * format, may be NULL.
98 * \returns 0 on success or a negative error code on failure; call
99 * SDL_GetError() for more information.
100 *
101 * \since This function is available since SDL 3.0.0.
102 */
103extern SDL_DECLSPEC int SDLCALL SDL_GetDateTimeLocalePreferences(SDL_DateFormat *dateFormat, SDL_TimeFormat *timeFormat);
104
105/**
106 * Gets the current value of the system realtime clock in nanoseconds since
107 * Jan 1, 1970 in Universal Coordinated Time (UTC).
108 *
109 * \param ticks the SDL_Time to hold the returned tick count.
110 * \returns 0 on success or a negative error code on failure; call
111 * SDL_GetError() for more information.
112 *
113 * \since This function is available since SDL 3.0.0.
114 */
115extern SDL_DECLSPEC int SDLCALL SDL_GetCurrentTime(SDL_Time *ticks);
116
117/**
118 * Converts an SDL_Time in nanoseconds since the epoch to a calendar time in
119 * the SDL_DateTime format.
120 *
121 * \param ticks the SDL_Time to be converted.
122 * \param dt the resulting SDL_DateTime.
123 * \param localTime the resulting SDL_DateTime will be expressed in local time
124 * if true, otherwise it will be in Universal Coordinated
125 * Time (UTC).
126 * \returns 0 on success or a negative error code on failure; call
127 * SDL_GetError() for more information.
128 *
129 * \since This function is available since SDL 3.0.0.
130 */
131extern SDL_DECLSPEC int SDLCALL SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, SDL_bool localTime);
132
133/**
134 * Converts a calendar time to an SDL_Time in nanoseconds since the epoch.
135 *
136 * This function ignores the day_of_week member of the SDL_DateTime struct, so
137 * it may remain unset.
138 *
139 * \param dt the source SDL_DateTime.
140 * \param ticks the resulting SDL_Time.
141 * \returns 0 on success or a negative error code on failure; call
142 * SDL_GetError() for more information.
143 *
144 * \since This function is available since SDL 3.0.0.
145 */
146extern SDL_DECLSPEC int SDLCALL SDL_DateTimeToTime(const SDL_DateTime *dt, SDL_Time *ticks);
147
148/**
149 * Converts an SDL time into a Windows FILETIME (100-nanosecond intervals
150 * since January 1, 1601).
151 *
152 * This function fills in the two 32-bit values of the FILETIME structure.
153 *
154 * \param ticks the time to convert.
155 * \param dwLowDateTime a pointer filled in with the low portion of the
156 * Windows FILETIME value.
157 * \param dwHighDateTime a pointer filled in with the high portion of the
158 * Windows FILETIME value.
159 *
160 * \since This function is available since SDL 3.0.0.
161 */
162extern SDL_DECLSPEC void SDLCALL SDL_TimeToWindows(SDL_Time ticks, Uint32 *dwLowDateTime, Uint32 *dwHighDateTime);
163
164/**
165 * Converts a Windows FILETIME (100-nanosecond intervals since January 1,
166 * 1601) to an SDL time.
167 *
168 * This function takes the two 32-bit values of the FILETIME structure as
169 * parameters.
170 *
171 * \param dwLowDateTime the low portion of the Windows FILETIME value.
172 * \param dwHighDateTime the high portion of the Windows FILETIME value.
173 * \returns the converted SDL time.
174 *
175 * \since This function is available since SDL 3.0.0.
176 */
177extern SDL_DECLSPEC SDL_Time SDLCALL SDL_TimeFromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime);
178
179/**
180 * Get the number of days in a month for a given year.
181 *
182 * \param year the year.
183 * \param month the month [1-12].
184 * \returns the number of days in the requested month or a negative error code
185 * on failure; call SDL_GetError() for more information.
186 *
187 * \since This function is available since SDL 3.0.0.
188 */
189extern SDL_DECLSPEC int SDLCALL SDL_GetDaysInMonth(int year, int month);
190
191/**
192 * Get the day of year for a calendar date.
193 *
194 * \param year the year component of the date.
195 * \param month the month component of the date.
196 * \param day the day component of the date.
197 * \returns the day of year [0-365] if the date is valid or a negative error
198 * code on failure; call SDL_GetError() for more information.
199 *
200 * \since This function is available since SDL 3.0.0.
201 */
202extern SDL_DECLSPEC int SDLCALL SDL_GetDayOfYear(int year, int month, int day);
203
204/**
205 * Get the day of week for a calendar date.
206 *
207 * \param year the year component of the date.
208 * \param month the month component of the date.
209 * \param day the day component of the date.
210 * \returns a value between 0 and 6 (0 being Sunday) if the date is valid or a
211 * negative error code on failure; call SDL_GetError() for more
212 * information.
213 *
214 * \since This function is available since SDL 3.0.0.
215 */
216extern SDL_DECLSPEC int SDLCALL SDL_GetDayOfWeek(int year, int month, int day);
217
218/* Ends C function definitions when using C++ */
219#ifdef __cplusplus
220}
221#endif
222#include <SDL3/SDL_close_code.h>
223
224#endif /* SDL_time_h_ */
int SDL_bool
Definition SDL_stdinc.h:213
uint32_t Uint32
Definition SDL_stdinc.h:265
Sint64 SDL_Time
Definition SDL_stdinc.h:304
int SDL_GetDayOfWeek(int year, int month, int day)
int SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, SDL_bool localTime)
SDL_DateFormat
Definition SDL_time.h:67
@ SDL_DATE_FORMAT_DDMMYYYY
Definition SDL_time.h:69
@ SDL_DATE_FORMAT_YYYYMMDD
Definition SDL_time.h:68
@ SDL_DATE_FORMAT_MMDDYYYY
Definition SDL_time.h:70
void SDL_TimeToWindows(SDL_Time ticks, Uint32 *dwLowDateTime, Uint32 *dwHighDateTime)
int SDL_GetDayOfYear(int year, int month, int day)
SDL_TimeFormat
Definition SDL_time.h:81
@ SDL_TIME_FORMAT_12HR
Definition SDL_time.h:83
@ SDL_TIME_FORMAT_24HR
Definition SDL_time.h:82
int SDL_GetCurrentTime(SDL_Time *ticks)
int SDL_GetDaysInMonth(int year, int month)
SDL_Time SDL_TimeFromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime)
int SDL_GetDateTimeLocalePreferences(SDL_DateFormat *dateFormat, SDL_TimeFormat *timeFormat)
int SDL_DateTimeToTime(const SDL_DateTime *dt, SDL_Time *ticks)
int nanosecond
Definition SDL_time.h:54
int utc_offset
Definition SDL_time.h:56
int day_of_week
Definition SDL_time.h:55