summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/time/windows/SDL_systime.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
committer3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
commit5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch)
tree8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/time/windows/SDL_systime.c
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/time/windows/SDL_systime.c')
-rw-r--r--contrib/SDL-3.2.8/src/time/windows/SDL_systime.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/time/windows/SDL_systime.c b/contrib/SDL-3.2.8/src/time/windows/SDL_systime.c
new file mode 100644
index 0000000..4bc1e99
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/time/windows/SDL_systime.c
@@ -0,0 +1,157 @@
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_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 if (!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 if (!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