SDL_time.h 7.9 KB

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