Atlas - SDL_systime.c

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