SDL_systime.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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. #include "SDL_internal.h"
  19. #ifdef SDL_TIME_UNIX
  20. #include "../SDL_time_c.h"
  21. #include <errno.h>
  22. #ifndef SDL_PLATFORM_DOS
  23. #include <langinfo.h>
  24. #endif
  25. #include <sys/time.h>
  26. #include <time.h>
  27. #include <unistd.h>
  28. #if !defined(HAVE_CLOCK_GETTIME) && defined(SDL_PLATFORM_APPLE)
  29. #include <mach/clock.h>
  30. #include <mach/mach.h>
  31. #include <mach/mach_time.h>
  32. #endif
  33. void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
  34. {
  35. /* This *should* be well-supported aside from very old legacy systems, but apparently
  36. * Android didn't add this until SDK version 26, so a check is needed...
  37. */
  38. #ifdef HAVE_NL_LANGINFO
  39. if (df) {
  40. const char *s = nl_langinfo(D_FMT);
  41. // Figure out the preferred system date format from the first format character.
  42. if (s) {
  43. while (*s) {
  44. switch (*s++) {
  45. case 'Y':
  46. case 'y':
  47. case 'F':
  48. case 'C':
  49. *df = SDL_DATE_FORMAT_YYYYMMDD;
  50. goto found_date;
  51. case 'd':
  52. case 'e':
  53. *df = SDL_DATE_FORMAT_DDMMYYYY;
  54. goto found_date;
  55. case 'b':
  56. case 'D':
  57. case 'h':
  58. case 'm':
  59. *df = SDL_DATE_FORMAT_MMDDYYYY;
  60. goto found_date;
  61. default:
  62. break;
  63. }
  64. }
  65. }
  66. }
  67. found_date:
  68. if (tf) {
  69. const char *s = nl_langinfo(T_FMT);
  70. // Figure out the preferred system date format.
  71. if (s) {
  72. while (*s) {
  73. switch (*s++) {
  74. case 'H':
  75. case 'k':
  76. case 'T':
  77. *tf = SDL_TIME_FORMAT_24HR;
  78. return;
  79. case 'I':
  80. case 'l':
  81. case 'r':
  82. *tf = SDL_TIME_FORMAT_12HR;
  83. return;
  84. default:
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. #endif
  91. }
  92. bool SDL_GetCurrentTime(SDL_Time *ticks)
  93. {
  94. CHECK_PARAM(!ticks) {
  95. return SDL_InvalidParamError("ticks");
  96. }
  97. #ifdef HAVE_CLOCK_GETTIME
  98. struct timespec tp;
  99. if (clock_gettime(CLOCK_REALTIME, &tp) == 0) {
  100. //tp.tv_sec = SDL_min(tp.tv_sec, SDL_NS_TO_SECONDS(SDL_MAX_TIME) - 1);
  101. *ticks = SDL_SECONDS_TO_NS(tp.tv_sec) + tp.tv_nsec;
  102. return true;
  103. }
  104. SDL_SetError("Failed to retrieve system time (%i)", errno);
  105. #elif defined(SDL_PLATFORM_APPLE)
  106. clock_serv_t cclock;
  107. int ret = host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
  108. if (ret == 0) {
  109. mach_timespec_t mts;
  110. SDL_zero(mts);
  111. ret = clock_get_time(cclock, &mts);
  112. if (ret == 0) {
  113. // mach_timespec_t tv_sec is 32-bit, so no overflow possible
  114. *ticks = SDL_SECONDS_TO_NS(mts.tv_sec) + mts.tv_nsec;
  115. }
  116. mach_port_deallocate(mach_task_self(), cclock);
  117. if (!ret) {
  118. return true;
  119. }
  120. }
  121. SDL_SetError("Failed to retrieve system time (%i)", ret);
  122. #else
  123. struct timeval tv;
  124. SDL_zero(tv);
  125. if (gettimeofday(&tv, NULL) == 0) {
  126. tv.tv_sec = SDL_min(tv.tv_sec, SDL_NS_TO_SECONDS(SDL_MAX_TIME) - 1);
  127. *ticks = SDL_SECONDS_TO_NS(tv.tv_sec) + SDL_US_TO_NS(tv.tv_usec);
  128. return true;
  129. }
  130. SDL_SetError("Failed to retrieve system time (%i)", errno);
  131. #endif
  132. return false;
  133. }
  134. bool SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, bool localTime)
  135. {
  136. #if defined (HAVE_GMTIME_R) || defined(HAVE_LOCALTIME_R)
  137. struct tm tm_storage;
  138. #endif
  139. struct tm *tm = NULL;
  140. CHECK_PARAM(!dt) {
  141. return SDL_InvalidParamError("dt");
  142. }
  143. const time_t tval = (time_t)SDL_NS_TO_SECONDS(ticks);
  144. if (localTime) {
  145. #ifdef HAVE_LOCALTIME_R
  146. tm = localtime_r(&tval, &tm_storage);
  147. #else
  148. tm = localtime(&tval);
  149. #endif
  150. } else {
  151. #ifdef HAVE_GMTIME_R
  152. tm = gmtime_r(&tval, &tm_storage);
  153. #else
  154. tm = gmtime(&tval);
  155. #endif
  156. }
  157. if (tm) {
  158. dt->year = tm->tm_year + 1900;
  159. dt->month = tm->tm_mon + 1;
  160. dt->day = tm->tm_mday;
  161. dt->hour = tm->tm_hour;
  162. dt->minute = tm->tm_min;
  163. dt->second = tm->tm_sec;
  164. dt->nanosecond = ticks % SDL_NS_PER_SECOND;
  165. dt->day_of_week = tm->tm_wday;
  166. /* tm_gmtoff wasn't formally standardized until POSIX.1-2024, but practically it has been available on desktop
  167. * *nix platforms such as Linux/glibc, FreeBSD, OpenBSD, NetBSD, OSX/macOS, and others since the 1990s.
  168. *
  169. * The notable exception is Solaris, where the timezone offset must still be retrieved in the strictly POSIX.1-2008
  170. * compliant way.
  171. */
  172. #if (_POSIX_VERSION >= 202405L) || (!defined(sun) && !defined(__sun))
  173. dt->utc_offset = (int)tm->tm_gmtoff;
  174. #else
  175. if (localTime) {
  176. tzset();
  177. dt->utc_offset = (int)timezone;
  178. } else {
  179. dt->utc_offset = 0;
  180. }
  181. #endif
  182. return true;
  183. }
  184. return SDL_SetError("SDL_DateTime conversion failed (%i)", errno);
  185. }
  186. #endif // SDL_TIME_UNIX