Atlas - SDL_systime.c
Home / ext / SDL / src / time / windows Lines: 1 | Size: 4898 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_WINDOWS 24 25#include "../../core/windows/SDL_windows.h" 26 27#include "../SDL_time_c.h" 28 29#define NS_PER_WINDOWS_TICK 100ULL 30#define WINDOWS_TICK 10000000ULL 31#define UNIX_EPOCH_OFFSET_SEC 11644473600ULL 32 33typedef void (WINAPI *pfnGetSystemTimePreciseAsFileTime)(FILETIME *); 34 35void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf) 36{ 37 WCHAR str[80]; // Per the docs, the time and short date format strings can be a max of 80 characters. 38 39 if (df && GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, str, sizeof(str) / sizeof(WCHAR))) { 40 LPWSTR s = str; 41 while (*s) { 42 switch (*s++) { 43 case L'y': 44 *df = SDL_DATE_FORMAT_YYYYMMDD; 45 goto found_date; 46 case L'd': 47 *df = SDL_DATE_FORMAT_DDMMYYYY; 48 goto found_date; 49 case L'M': 50 *df = SDL_DATE_FORMAT_MMDDYYYY; 51 goto found_date; 52 default: 53 break; 54 } 55 } 56 } 57 58found_date: 59 60 // Figure out the preferred system date format. 61 if (tf && GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, str, sizeof(str) / sizeof(WCHAR))) { 62 LPWSTR s = str; 63 while (*s) { 64 switch (*s++) { 65 case L'H': 66 *tf = SDL_TIME_FORMAT_24HR; 67 return; 68 case L'h': 69 *tf = SDL_TIME_FORMAT_12HR; 70 return; 71 default: 72 break; 73 } 74 } 75 } 76} 77 78bool SDL_GetCurrentTime(SDL_Time *ticks) 79{ 80 FILETIME ft; 81 82 CHECK_PARAM(!ticks) { 83 return SDL_InvalidParamError("ticks"); 84 } 85 86 SDL_zero(ft); 87 88 static pfnGetSystemTimePreciseAsFileTime pGetSystemTimePreciseAsFileTime = NULL; 89 static bool load_attempted = false; 90 91 // Only available in Win8/Server 2012 or higher. 92 if (!pGetSystemTimePreciseAsFileTime && !load_attempted) { 93 HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll")); 94 if (kernel32) { 95 pGetSystemTimePreciseAsFileTime = (pfnGetSystemTimePreciseAsFileTime)GetProcAddress(kernel32, "GetSystemTimePreciseAsFileTime"); 96 } 97 load_attempted = true; 98 } 99 100 if (pGetSystemTimePreciseAsFileTime) { 101 pGetSystemTimePreciseAsFileTime(&ft); 102 } else { 103 GetSystemTimeAsFileTime(&ft); 104 } 105 106 *ticks = SDL_TimeFromWindows(ft.dwLowDateTime, ft.dwHighDateTime); 107 108 return true; 109} 110 111bool SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, bool localTime) 112{ 113 FILETIME ft, local_ft; 114 SYSTEMTIME utc_st, local_st; 115 SYSTEMTIME *st = NULL; 116 Uint32 low, high; 117 118 CHECK_PARAM(!dt) { 119 return SDL_InvalidParamError("dt"); 120 } 121 122 SDL_TimeToWindows(ticks, &low, &high); 123 ft.dwLowDateTime = (DWORD)low; 124 ft.dwHighDateTime = (DWORD)high; 125 126 if (FileTimeToSystemTime(&ft, &utc_st)) { 127 if (localTime) { 128 if (SystemTimeToTzSpecificLocalTime(NULL, &utc_st, &local_st)) { 129 // Calculate the difference for the UTC offset. 130 SystemTimeToFileTime(&local_st, &local_ft); 131 const SDL_Time local_ticks = SDL_TimeFromWindows(local_ft.dwLowDateTime, local_ft.dwHighDateTime); 132 dt->utc_offset = (int)SDL_NS_TO_SECONDS(local_ticks - ticks); 133 st = &local_st; 134 } 135 } else { 136 dt->utc_offset = 0; 137 st = &utc_st; 138 } 139 140 if (st) { 141 dt->year = st->wYear; 142 dt->month = st->wMonth; 143 dt->day = st->wDay; 144 dt->hour = st->wHour; 145 dt->minute = st->wMinute; 146 dt->second = st->wSecond; 147 dt->nanosecond = ticks % SDL_NS_PER_SECOND; 148 dt->day_of_week = st->wDayOfWeek; 149 150 return true; 151 } 152 } 153 154 return SDL_SetError("SDL_DateTime conversion failed (%lu)", GetLastError()); 155} 156 157#endif // SDL_TIME_WINDOWS 158[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.