diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/time/unix | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/time/unix')
| -rw-r--r-- | contrib/SDL-3.2.8/src/time/unix/SDL_systime.c | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/time/unix/SDL_systime.c b/contrib/SDL-3.2.8/src/time/unix/SDL_systime.c new file mode 100644 index 0000000..296175a --- /dev/null +++ b/contrib/SDL-3.2.8/src/time/unix/SDL_systime.c | |||
| @@ -0,0 +1,197 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. 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. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #ifdef SDL_TIME_UNIX | ||
| 24 | |||
| 25 | #include "../SDL_time_c.h" | ||
| 26 | #include <errno.h> | ||
| 27 | #include <langinfo.h> | ||
| 28 | #include <sys/time.h> | ||
| 29 | #include <time.h> | ||
| 30 | |||
| 31 | #if !defined(HAVE_CLOCK_GETTIME) && defined(SDL_PLATFORM_APPLE) | ||
| 32 | #include <mach/clock.h> | ||
| 33 | #include <mach/mach.h> | ||
| 34 | #include <mach/mach_time.h> | ||
| 35 | #endif | ||
| 36 | |||
| 37 | void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf) | ||
| 38 | { | ||
| 39 | /* This *should* be well-supported aside from very old legacy systems, but apparently | ||
| 40 | * Android didn't add this until SDK version 26, so a check is needed... | ||
| 41 | */ | ||
| 42 | #ifdef HAVE_NL_LANGINFO | ||
| 43 | if (df) { | ||
| 44 | const char *s = nl_langinfo(D_FMT); | ||
| 45 | |||
| 46 | // Figure out the preferred system date format from the first format character. | ||
| 47 | if (s) { | ||
| 48 | while (*s) { | ||
| 49 | switch (*s++) { | ||
| 50 | case 'Y': | ||
| 51 | case 'y': | ||
| 52 | case 'F': | ||
| 53 | case 'C': | ||
| 54 | *df = SDL_DATE_FORMAT_YYYYMMDD; | ||
| 55 | goto found_date; | ||
| 56 | case 'd': | ||
| 57 | case 'e': | ||
| 58 | *df = SDL_DATE_FORMAT_DDMMYYYY; | ||
| 59 | goto found_date; | ||
| 60 | case 'b': | ||
| 61 | case 'D': | ||
| 62 | case 'h': | ||
| 63 | case 'm': | ||
| 64 | *df = SDL_DATE_FORMAT_MMDDYYYY; | ||
| 65 | goto found_date; | ||
| 66 | default: | ||
| 67 | break; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | found_date: | ||
| 74 | |||
| 75 | if (tf) { | ||
| 76 | const char *s = nl_langinfo(T_FMT); | ||
| 77 | |||
| 78 | // Figure out the preferred system date format. | ||
| 79 | if (s) { | ||
| 80 | while (*s) { | ||
| 81 | switch (*s++) { | ||
| 82 | case 'H': | ||
| 83 | case 'k': | ||
| 84 | case 'T': | ||
| 85 | *tf = SDL_TIME_FORMAT_24HR; | ||
| 86 | return; | ||
| 87 | case 'I': | ||
| 88 | case 'l': | ||
| 89 | case 'r': | ||
| 90 | *tf = SDL_TIME_FORMAT_12HR; | ||
| 91 | return; | ||
| 92 | default: | ||
| 93 | break; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | #endif | ||
| 99 | } | ||
| 100 | |||
| 101 | bool SDL_GetCurrentTime(SDL_Time *ticks) | ||
| 102 | { | ||
| 103 | if (!ticks) { | ||
| 104 | return SDL_InvalidParamError("ticks"); | ||
| 105 | } | ||
| 106 | #ifdef HAVE_CLOCK_GETTIME | ||
| 107 | struct timespec tp; | ||
| 108 | |||
| 109 | if (clock_gettime(CLOCK_REALTIME, &tp) == 0) { | ||
| 110 | //tp.tv_sec = SDL_min(tp.tv_sec, SDL_NS_TO_SECONDS(SDL_MAX_TIME) - 1); | ||
| 111 | *ticks = SDL_SECONDS_TO_NS(tp.tv_sec) + tp.tv_nsec; | ||
| 112 | return true; | ||
| 113 | } | ||
| 114 | |||
| 115 | SDL_SetError("Failed to retrieve system time (%i)", errno); | ||
| 116 | |||
| 117 | #elif defined(SDL_PLATFORM_APPLE) | ||
| 118 | clock_serv_t cclock; | ||
| 119 | int ret = host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); | ||
| 120 | if (ret == 0) { | ||
| 121 | mach_timespec_t mts; | ||
| 122 | |||
| 123 | SDL_zero(mts); | ||
| 124 | ret = clock_get_time(cclock, &mts); | ||
| 125 | if (ret == 0) { | ||
| 126 | // mach_timespec_t tv_sec is 32-bit, so no overflow possible | ||
| 127 | *ticks = SDL_SECONDS_TO_NS(mts.tv_sec) + mts.tv_nsec; | ||
| 128 | } | ||
| 129 | mach_port_deallocate(mach_task_self(), cclock); | ||
| 130 | |||
| 131 | if (!ret) { | ||
| 132 | return true; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | SDL_SetError("Failed to retrieve system time (%i)", ret); | ||
| 137 | |||
| 138 | #else | ||
| 139 | struct timeval tv; | ||
| 140 | SDL_zero(tv); | ||
| 141 | if (gettimeofday(&tv, NULL) == 0) { | ||
| 142 | tv.tv_sec = SDL_min(tv.tv_sec, SDL_NS_TO_SECONDS(SDL_MAX_TIME) - 1); | ||
| 143 | *ticks = SDL_SECONDS_TO_NS(tv.tv_sec) + SDL_US_TO_NS(tv.tv_usec); | ||
| 144 | return true; | ||
| 145 | } | ||
| 146 | |||
| 147 | SDL_SetError("Failed to retrieve system time (%i)", errno); | ||
| 148 | #endif | ||
| 149 | |||
| 150 | return false; | ||
| 151 | } | ||
| 152 | |||
| 153 | bool SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, bool localTime) | ||
| 154 | { | ||
| 155 | #if defined (HAVE_GMTIME_R) || defined(HAVE_LOCALTIME_R) | ||
| 156 | struct tm tm_storage; | ||
| 157 | #endif | ||
| 158 | struct tm *tm = NULL; | ||
| 159 | |||
| 160 | if (!dt) { | ||
| 161 | return SDL_InvalidParamError("dt"); | ||
| 162 | } | ||
| 163 | |||
| 164 | const time_t tval = (time_t)SDL_NS_TO_SECONDS(ticks); | ||
| 165 | |||
| 166 | if (localTime) { | ||
| 167 | #ifdef HAVE_LOCALTIME_R | ||
| 168 | tm = localtime_r(&tval, &tm_storage); | ||
| 169 | #else | ||
| 170 | tm = localtime(&tval); | ||
| 171 | #endif | ||
| 172 | } else { | ||
| 173 | #ifdef HAVE_GMTIME_R | ||
| 174 | tm = gmtime_r(&tval, &tm_storage); | ||
| 175 | #else | ||
| 176 | tm = gmtime(&tval); | ||
| 177 | #endif | ||
| 178 | } | ||
| 179 | |||
| 180 | if (tm) { | ||
| 181 | dt->year = tm->tm_year + 1900; | ||
| 182 | dt->month = tm->tm_mon + 1; | ||
| 183 | dt->day = tm->tm_mday; | ||
| 184 | dt->hour = tm->tm_hour; | ||
| 185 | dt->minute = tm->tm_min; | ||
| 186 | dt->second = tm->tm_sec; | ||
| 187 | dt->nanosecond = ticks % SDL_NS_PER_SECOND; | ||
| 188 | dt->day_of_week = tm->tm_wday; | ||
| 189 | dt->utc_offset = (int)tm->tm_gmtoff; | ||
| 190 | |||
| 191 | return true; | ||
| 192 | } | ||
| 193 | |||
| 194 | return SDL_SetError("SDL_DateTime conversion failed (%i)", errno); | ||
| 195 | } | ||
| 196 | |||
| 197 | #endif // SDL_TIME_UNIX | ||
