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 * \file SDL_time.h
27 *
28 * Header for the 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 components.
42 */
43typedef struct SDL_DateTime
44{
45 int year; /**< Year */
46 int month; /**< Month [01-12] */
47 int day; /**< Day of the month [01-31] */
48 int hour; /**< Hour [0-23] */
49 int minute; /**< Minute [0-59] */
50 int second; /**< Seconds [0-60] */
51 int nanosecond; /**< Nanoseconds [0-999999999] */
52 int day_of_week; /**< Day of the week [0-6] (0 being Sunday) */
53 int utc_offset; /**< Seconds east of UTC */
55
56/**
57 * The preferred date format of the current system locale.
58 *
59 * \sa SDL_PROP_GLOBAL_SYSTEM_DATE_FORMAT_NUMBER
60 */
61typedef enum SDL_DATE_FORMAT
62{
63 SDL_DATE_FORMAT_YYYYMMDD = 0, /**< Year/Month/Day */
64 SDL_DATE_FORMAT_DDMMYYYY = 1, /**< Day/Month/Year */
65 SDL_DATE_FORMAT_MMDDYYYY = 2, /**< Month/Day/Year */
67
68/**
69 * The preferred time format of the current system locale.
70 *
71 * \sa SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER
72 */
73typedef enum SDL_TIME_FORMAT
74{
75 SDL_TIME_FORMAT_24HR = 0, /**< 24 hour time */
76 SDL_TIME_FORMAT_12HR = 1, /**< 12 hour time */
78
79/**
80 * Global date/time properties
81 *
82 * - `SDL_PROP_GLOBAL_SYSTEM_DATE_FORMAT_NUMBER`: the SDL_DATE_FORMAT to use as the preferred date display format
83 * for the current system locale.
84 * - `SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER`: the SDL_TIME_FORMAT to use as the preferred time display format
85 * for the current system locale.
86 */
87#define SDL_PROP_GLOBAL_SYSTEM_DATE_FORMAT_NUMBER "SDL.time.date_format"
88#define SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER "SDL.time.time_format"
89
90/**
91 * Gets the current value of the system realtime clock in nanoseconds since
92 * Jan 1, 1970 in Universal Coordinated Time (UTC).
93 *
94 * \param ticks the SDL_Time to hold the returned tick count
95 * \returns 0 on success or -1 on error; call SDL_GetError() for more
96 * information.
97 *
98 * \since This function is available since SDL 3.0.0
99 */
100extern DECLSPEC int SDLCALL SDL_GetCurrentTime(SDL_Time *ticks);
101
102/**
103 * Converts an SDL_Time in nanoseconds since the epoch to a calendar time in
104 * the SDL_DateTime format.
105 *
106 * \param ticks the SDL_Time to be converted
107 * \param dt the resulting SDL_DateTime
108 * \param localTime the resulting SDL_DateTime will be expressed in local time
109 * if true, otherwise it will be in Universal Coordinated
110 * Time (UTC)
111 * \returns 0 on success or -1 on error; call SDL_GetError() for more
112 * information.
113 *
114 * \since This function is available since SDL 3.0.0
115 */
116extern DECLSPEC int SDLCALL SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, SDL_bool localTime);
117
118/**
119 * Converts a calendar time to an SDL_Time in nanoseconds since the epoch.
120 *
121 * This function ignores the day_of_week member of the SDL_DateTime struct, so
122 * it may remain unset.
123 *
124 * \param dt the source SDL_DateTime
125 * \param ticks the resulting SDL_Time
126 * \returns 0 on success or -1 on error; call SDL_GetError() for more
127 * information.
128 *
129 * \since This function is available since SDL 3.0.0
130 */
131extern DECLSPEC int SDLCALL SDL_DateTimeToTime(const SDL_DateTime *dt, SDL_Time *ticks);
132
133/**
134 * Converts an SDL time into a Windows FILETIME (100-nanosecond intervals
135 * since January 1, 1601).
136 *
137 * This function fills in the two 32-bit values of the FILETIME structure.
138 *
139 * \param ticks the time to convert
140 * \param dwLowDateTime a pointer filled in with the low portion of the
141 * Windows FILETIME value
142 * \param dwHighDateTime a pointer filled in with the high portion of the
143 * Windows FILETIME value
144 *
145 * \since This function is available since SDL 3.0.0.
146 */
147extern DECLSPEC void SDLCALL SDL_TimeToWindows(SDL_Time ticks, Uint32 *dwLowDateTime, Uint32 *dwHighDateTime);
148
149/**
150 * Converts a Windows FILETIME (100-nanosecond intervals since January 1,
151 * 1601) to an SDL time
152 *
153 * This function takes the two 32-bit values of the FILETIME structure as
154 * parameters.
155 *
156 * \param dwLowDateTime the low portion of the Windows FILETIME value
157 * \param dwHighDateTime the high portion of the Windows FILETIME value
158 * \returns the converted SDL time
159 *
160 * \since This function is available since SDL 3.0.0.
161 */
162extern DECLSPEC SDL_Time SDLCALL SDL_TimeFromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime);
163
164/**
165 * Get the number of days in a month for a given year.
166 *
167 * \param year the year
168 * \param month the month [1-12]
169 * \returns the number of days in the requested month, otherwise -1; call
170 * SDL_GetError() for more information.
171 *
172 * \since This function is available since SDL 3.0.0
173 */
174extern DECLSPEC int SDLCALL SDL_GetDaysInMonth(int year, int month);
175
176/**
177 * Get the day of year for a calendar date.
178 *
179 * \param year the year component of the date
180 * \param month the month component of the date
181 * \param day the day component of the date
182 * \returns the day of year [0-365] if the date is valid, otherwise -1; call
183 * SDL_GetError() for more information.
184 *
185 * \since This function is available since SDL 3.0.0
186 */
187extern DECLSPEC int SDLCALL SDL_GetDayOfYear(int year, int month, int day);
188
189/**
190 * Get the day of week for a calendar date.
191 *
192 * \param year the year component of the date
193 * \param month the month component of the date
194 * \param day the day component of the date
195 * \returns a value between 0 and 6 (0 being Sunday) if the date is valid,
196 * otherwise -1; call SDL_GetError() for more information.
197 *
198 * \since This function is available since SDL 3.0.0
199 */
200extern DECLSPEC int SDLCALL SDL_GetDayOfWeek(int year, int month, int day);
201
202/* Ends C function definitions when using C++ */
203#ifdef __cplusplus
204}
205#endif
206#include <SDL3/SDL_close_code.h>
207
208#endif /* SDL_time_h_ */
int SDL_bool
Definition SDL_stdinc.h:137
uint32_t Uint32
Definition SDL_stdinc.h:174
Sint64 SDL_Time
Definition SDL_stdinc.h:197
SDL_TIME_FORMAT
Definition SDL_time.h:74
@ SDL_TIME_FORMAT_12HR
Definition SDL_time.h:76
@ SDL_TIME_FORMAT_24HR
Definition SDL_time.h:75
int SDL_GetDayOfWeek(int year, int month, int day)
int SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, SDL_bool localTime)
void SDL_TimeToWindows(SDL_Time ticks, Uint32 *dwLowDateTime, Uint32 *dwHighDateTime)
int SDL_GetDayOfYear(int year, int month, int day)
int SDL_GetCurrentTime(SDL_Time *ticks)
SDL_DATE_FORMAT
Definition SDL_time.h:62
@ SDL_DATE_FORMAT_DDMMYYYY
Definition SDL_time.h:64
@ SDL_DATE_FORMAT_YYYYMMDD
Definition SDL_time.h:63
@ SDL_DATE_FORMAT_MMDDYYYY
Definition SDL_time.h:65
int SDL_GetDaysInMonth(int year, int month)
SDL_Time SDL_TimeFromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime)
int SDL_DateTimeToTime(const SDL_DateTime *dt, SDL_Time *ticks)
int nanosecond
Definition SDL_time.h:51
int utc_offset
Definition SDL_time.h:53
int day_of_week
Definition SDL_time.h:52