Atlas - SDL_systime.c

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