summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/time
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
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/time')
-rw-r--r--contrib/SDL-3.2.8/src/time/SDL_time.c222
-rw-r--r--contrib/SDL-3.2.8/src/time/SDL_time_c.h36
-rw-r--r--contrib/SDL-3.2.8/src/time/n3ds/SDL_systime.c147
-rw-r--r--contrib/SDL-3.2.8/src/time/ps2/SDL_systime.c65
-rw-r--r--contrib/SDL-3.2.8/src/time/psp/SDL_systime.c137
-rw-r--r--contrib/SDL-3.2.8/src/time/unix/SDL_systime.c197
-rw-r--r--contrib/SDL-3.2.8/src/time/vita/SDL_systime.c142
-rw-r--r--contrib/SDL-3.2.8/src/time/windows/SDL_systime.c157
8 files changed, 1103 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/time/SDL_time.c b/contrib/SDL-3.2.8/src/time/SDL_time.c
new file mode 100644
index 0000000..54059de
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/time/SDL_time.c
@@ -0,0 +1,222 @@
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#include "SDL_time_c.h"
24
25/* The following algorithms are based on those of Howard Hinnant and are in the public domain.
26 *
27 * http://howardhinnant.github.io/date_algorithms.html
28 */
29
30/* Given a calendar date, returns days since Jan 1 1970, and optionally
31 * the day of the week [0-6, 0 is Sunday] and day of the year [0-365].
32 */
33Sint64 SDL_CivilToDays(int year, int month, int day, int *day_of_week, int *day_of_year)
34{
35
36 year -= month <= 2;
37 const int era = (year >= 0 ? year : year - 399) / 400;
38 const unsigned yoe = (unsigned)(year - era * 400); // [0, 399]
39 const unsigned doy = (153 * (month > 2 ? month - 3 : month + 9) + 2) / 5 + day - 1; // [0, 365]
40 const unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
41 const Sint64 z = (Sint64)(era) * 146097 + (Sint64)(doe)-719468;
42
43 if (day_of_week) {
44 *day_of_week = (int)(z >= -4 ? (z + 4) % 7 : (z + 5) % 7 + 6);
45 }
46 if (day_of_year) {
47 // This algorithm considers March 1 to be the first day of the year, so offset by Jan + Feb.
48 if (doy > 305) {
49 // Day 0 is the first day of the year.
50 *day_of_year = doy - 306;
51 } else {
52 const int doy_offset = 59 + (!(year % 4) && ((year % 100) || !(year % 400)));
53 *day_of_year = doy + doy_offset;
54 }
55 }
56
57 return z;
58}
59
60bool SDL_GetDateTimeLocalePreferences(SDL_DateFormat *dateFormat, SDL_TimeFormat *timeFormat)
61{
62 // Default to ISO 8061 date format, as it is unambiguous, and 24 hour time.
63 if (dateFormat) {
64 *dateFormat = SDL_DATE_FORMAT_YYYYMMDD;
65 }
66 if (timeFormat) {
67 *timeFormat = SDL_TIME_FORMAT_24HR;
68 }
69
70 SDL_GetSystemTimeLocalePreferences(dateFormat, timeFormat);
71
72 return true;
73}
74
75int SDL_GetDaysInMonth(int year, int month)
76{
77 static const int DAYS_IN_MONTH[] = {
78 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
79 };
80
81 if (month < 1 || month > 12) {
82 SDL_SetError("Month out of range [1-12], requested: %i", month);
83 return -1;
84 }
85
86 int days = DAYS_IN_MONTH[month - 1];
87
88 /* A leap year occurs every 4 years...
89 * but not every 100 years...
90 * except for every 400 years.
91 */
92 if (month == 2 && (!(year % 4) && ((year % 100) || !(year % 400)))) {
93 ++days;
94 }
95
96 return days;
97}
98
99int SDL_GetDayOfYear(int year, int month, int day)
100{
101 int dayOfYear;
102
103 if (month < 1 || month > 12) {
104 SDL_SetError("Month out of range [1-12], requested: %i", month);
105 return -1;
106 }
107 if (day < 1 || day > SDL_GetDaysInMonth(year, month)) {
108 SDL_SetError("Day out of range [1-%i], requested: %i", SDL_GetDaysInMonth(year, month), month);
109 return -1;
110 }
111
112 SDL_CivilToDays(year, month, day, NULL, &dayOfYear);
113 return dayOfYear;
114}
115
116int SDL_GetDayOfWeek(int year, int month, int day)
117{
118 int dayOfWeek;
119
120 if (month < 1 || month > 12) {
121 SDL_SetError("Month out of range [1-12], requested: %i", month);
122 return -1;
123 }
124 if (day < 1 || day > SDL_GetDaysInMonth(year, month)) {
125 SDL_SetError("Day out of range [1-%i], requested: %i", SDL_GetDaysInMonth(year, month), month);
126 return -1;
127 }
128
129 SDL_CivilToDays(year, month, day, &dayOfWeek, NULL);
130 return dayOfWeek;
131}
132
133static bool SDL_DateTimeIsValid(const SDL_DateTime *dt)
134{
135 if (dt->month < 1 || dt->month > 12) {
136 SDL_SetError("Malformed SDL_DateTime: month out of range [1-12], current: %i", dt->month);
137 return false;
138 }
139
140 const int daysInMonth = SDL_GetDaysInMonth(dt->year, dt->month);
141 if (dt->day < 1 || dt->day > daysInMonth) {
142 SDL_SetError("Malformed SDL_DateTime: day of month out of range [1-%i], current: %i", daysInMonth, dt->month);
143 return false;
144 }
145 if (dt->hour < 0 || dt->hour > 23) {
146 SDL_SetError("Malformed SDL_DateTime: hour out of range [0-23], current: %i", dt->hour);
147 return false;
148 }
149 if (dt->minute < 0 || dt->minute > 59) {
150 SDL_SetError("Malformed SDL_DateTime: minute out of range [0-59], current: %i", dt->minute);
151 return false;
152 }
153 if (dt->second < 0 || dt->second > 60) {
154 SDL_SetError("Malformed SDL_DateTime: second out of range [0-60], current: %i", dt->second);
155 return false; // 60 accounts for a possible leap second.
156 }
157 if (dt->nanosecond < 0 || dt->nanosecond >= SDL_NS_PER_SECOND) {
158 SDL_SetError("Malformed SDL_DateTime: nanosecond out of range [0-999999999], current: %i", dt->nanosecond);
159 return false;
160 }
161
162 return true;
163}
164
165bool SDL_DateTimeToTime(const SDL_DateTime *dt, SDL_Time *ticks)
166{
167 static const Sint64 max_seconds = SDL_NS_TO_SECONDS(SDL_MAX_TIME) - 1;
168 static const Sint64 min_seconds = SDL_NS_TO_SECONDS(SDL_MIN_TIME) + 1;
169 bool result = true;
170
171 if (!dt) {
172 return SDL_InvalidParamError("dt");
173 }
174 if (!ticks) {
175 return SDL_InvalidParamError("ticks");
176 }
177 if (!SDL_DateTimeIsValid(dt)) {
178 // The validation function sets the error string.
179 return false;
180 }
181
182 *ticks = SDL_CivilToDays(dt->year, dt->month, dt->day, NULL, NULL) * SDL_SECONDS_PER_DAY;
183 *ticks += (((dt->hour * 60) + dt->minute) * 60) + dt->second - dt->utc_offset;
184 if (*ticks > max_seconds || *ticks < min_seconds) {
185 *ticks = SDL_clamp(*ticks, min_seconds, max_seconds);
186 result = SDL_SetError("Date out of range for SDL_Time representation; SDL_Time value clamped");
187 }
188 *ticks = SDL_SECONDS_TO_NS(*ticks) + dt->nanosecond;
189
190 return result;
191}
192
193#define DELTA_EPOCH_1601_100NS (11644473600ll * 10000000ll) // [100 ns] (100 ns units between 1601-01-01 and 1970-01-01, 11644473600 seconds)
194
195void SDL_TimeToWindows(SDL_Time ticks, Uint32 *dwLowDateTime, Uint32 *dwHighDateTime)
196{
197 /* Convert nanoseconds to Win32 ticks.
198 * SDL_Time has a range of roughly 292 years, so even SDL_MIN_TIME can't underflow the Win32 epoch.
199 */
200 const Uint64 wtime = (Uint64)((ticks / 100) + DELTA_EPOCH_1601_100NS);
201
202 if (dwLowDateTime) {
203 *dwLowDateTime = (Uint32)wtime;
204 }
205
206 if (dwHighDateTime) {
207 *dwHighDateTime = (Uint32)(wtime >> 32);
208 }
209}
210
211SDL_Time SDL_TimeFromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime)
212{
213 static const Uint64 wintime_min = (Uint64)((SDL_MIN_TIME / 100) + DELTA_EPOCH_1601_100NS);
214 static const Uint64 wintime_max = (Uint64)((SDL_MAX_TIME / 100) + DELTA_EPOCH_1601_100NS);
215
216 Uint64 wtime = (((Uint64)dwHighDateTime << 32) | dwLowDateTime);
217
218 // Clamp the windows time range to the SDL_Time min/max
219 wtime = SDL_clamp(wtime, wintime_min, wintime_max);
220
221 return (SDL_Time)(wtime - DELTA_EPOCH_1601_100NS) * 100;
222}
diff --git a/contrib/SDL-3.2.8/src/time/SDL_time_c.h b/contrib/SDL-3.2.8/src/time/SDL_time_c.h
new file mode 100644
index 0000000..46b8cb4
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/time/SDL_time_c.h
@@ -0,0 +1,36 @@
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
22#ifndef SDL_time_c_h_
23#define SDL_time_c_h_
24
25#include "SDL_internal.h"
26
27#define SDL_SECONDS_PER_DAY 86400
28
29/* Given a calendar date, returns days since Jan 1 1970, and optionally
30 * the day of the week (0-6, 0 is Sunday) and day of the year (0-365).
31 */
32extern Sint64 SDL_CivilToDays(int year, int month, int day, int *day_of_week, int *day_of_year);
33
34extern void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf);
35
36#endif // SDL_time_c_h_
diff --git a/contrib/SDL-3.2.8/src/time/n3ds/SDL_systime.c b/contrib/SDL-3.2.8/src/time/n3ds/SDL_systime.c
new file mode 100644
index 0000000..61ab9fa
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/time/n3ds/SDL_systime.c
@@ -0,0 +1,147 @@
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_N3DS
24
25#include "../SDL_time_c.h"
26#include <3ds.h>
27
28/*
29 * The 3DS clock is essentially a simple digital watch and provides
30 * no timezone or DST functionality.
31 */
32
33// 3DS epoch is Jan 1 1900
34#define DELTA_EPOCH_1900_OFFSET_MS 2208988800000LL
35
36/* Returns year/month/day triple in civil calendar
37 * Preconditions: z is number of days since 1970-01-01 and is in the range:
38 * [INT_MIN, INT_MAX-719468].
39 *
40 * http://howardhinnant.github.io/date_algorithms.html#civil_from_days
41 */
42static void civil_from_days(int days, int *year, int *month, int *day)
43{
44 days += 719468;
45 const int era = (days >= 0 ? days : days - 146096) / 146097;
46 const unsigned doe = (unsigned)(days - era * 146097); // [0, 146096]
47 const unsigned yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399]
48 const int y = (int)(yoe) + era * 400;
49 const unsigned doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
50 const unsigned mp = (5 * doy + 2) / 153; // [0, 11]
51 const unsigned d = doy - (153 * mp + 2) / 5 + 1; // [1, 31]
52 const unsigned m = mp < 10 ? mp + 3 : mp - 9; // [1, 12]
53
54 *year = y + (m <= 2);
55 *month = (int)m;
56 *day = (int)d;
57}
58
59void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
60{
61 // The 3DS only has 12 supported languages, so take the standard for each
62 static const SDL_DateFormat LANG_TO_DATE_FORMAT[] = {
63 SDL_DATE_FORMAT_YYYYMMDD, // JP
64 SDL_DATE_FORMAT_DDMMYYYY, // EN, assume non-american format
65 SDL_DATE_FORMAT_DDMMYYYY, // FR
66 SDL_DATE_FORMAT_DDMMYYYY, // DE
67 SDL_DATE_FORMAT_DDMMYYYY, // IT
68 SDL_DATE_FORMAT_DDMMYYYY, // ES
69 SDL_DATE_FORMAT_YYYYMMDD, // ZH (CN)
70 SDL_DATE_FORMAT_YYYYMMDD, // KR
71 SDL_DATE_FORMAT_DDMMYYYY, // NL
72 SDL_DATE_FORMAT_DDMMYYYY, // PT
73 SDL_DATE_FORMAT_DDMMYYYY, // RU
74 SDL_DATE_FORMAT_YYYYMMDD // ZH (TW)
75 };
76 u8 system_language, is_north_america;
77 Result result, has_region;
78
79 if (R_FAILED(cfguInit())) {
80 return;
81 }
82 result = CFGU_GetSystemLanguage(&system_language);
83 has_region = CFGU_GetRegionCanadaUSA(&is_north_america);
84 cfguExit();
85 if (R_FAILED(result)) {
86 return;
87 }
88
89 if (df) {
90 *df = LANG_TO_DATE_FORMAT[system_language];
91 }
92 if (tf) {
93 *tf = SDL_TIME_FORMAT_24HR;
94 }
95
96 /* Only American English (en_US) uses MM/DD/YYYY and 12hr system, this gets
97 the formats wrong for canadians though (en_CA) */
98 if (system_language == CFG_LANGUAGE_EN &&
99 R_SUCCEEDED(has_region) && is_north_america) {
100 if (df) {
101 *df = SDL_DATE_FORMAT_MMDDYYYY;
102 }
103 if (tf) {
104 *tf = SDL_TIME_FORMAT_12HR;
105 }
106 }
107}
108
109bool SDL_GetCurrentTime(SDL_Time *ticks)
110{
111 if (!ticks) {
112 return SDL_InvalidParamError("ticks");
113 }
114
115 // Returns milliseconds since the epoch.
116 const Uint64 ndsTicksMax = (SDL_MAX_TIME / SDL_NS_PER_MS) + DELTA_EPOCH_1900_OFFSET_MS;
117 const Uint64 ndsTicks = SDL_min(osGetTime(), ndsTicksMax);
118
119 *ticks = SDL_MS_TO_NS(ndsTicks - DELTA_EPOCH_1900_OFFSET_MS);
120
121 return true;
122}
123
124bool SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, bool localTime)
125{
126 if (!dt) {
127 return SDL_InvalidParamError("dt");
128 }
129
130 const int days = (int)(SDL_NS_TO_SECONDS(ticks) / SDL_SECONDS_PER_DAY);
131 civil_from_days(days, &dt->year, &dt->month, &dt->day);
132
133 int rem = (int)(SDL_NS_TO_SECONDS(ticks) - (days * SDL_SECONDS_PER_DAY));
134 dt->hour = rem / (60 * 60);
135 rem -= dt->hour * 60 * 60;
136 dt->minute = rem / 60;
137 rem -= dt->minute * 60;
138 dt->second = rem;
139 dt->nanosecond = ticks % SDL_NS_PER_SECOND;
140 dt->utc_offset = 0; // Unknown
141
142 SDL_CivilToDays(dt->year, dt->month, dt->day, &dt->day_of_week, NULL);
143
144 return true;
145}
146
147#endif // SDL_TIME_N3DS
diff --git a/contrib/SDL-3.2.8/src/time/ps2/SDL_systime.c b/contrib/SDL-3.2.8/src/time/ps2/SDL_systime.c
new file mode 100644
index 0000000..e49d7d5
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/time/ps2/SDL_systime.c
@@ -0,0 +1,65 @@
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_PS2
24
25#include "../SDL_time_c.h"
26
27// PS2 epoch is Jan 1 2000 JST (UTC +9)
28#define UNIX_EPOCH_OFFSET_SEC 946717200
29
30// TODO: Implement this...
31void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
32{
33}
34
35bool SDL_GetCurrentTime(SDL_Time *ticks)
36{
37 if (!ticks) {
38 return SDL_InvalidParamError("ticks");
39 }
40
41 *ticks = 0;
42
43 return true;
44}
45
46bool SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, bool localTime)
47{
48 if (!dt) {
49 return SDL_InvalidParamError("dt");
50 }
51
52 dt->year = 1970;
53 dt->month = 1;
54 dt->day = 1;
55 dt->hour = 0;
56 dt->minute = 0;
57 dt->second = 0;
58 dt->nanosecond = 0;
59 dt->day_of_week = 4;
60 dt->utc_offset = 0;
61
62 return true;
63}
64
65#endif // SDL_TIME_PS2
diff --git a/contrib/SDL-3.2.8/src/time/psp/SDL_systime.c b/contrib/SDL-3.2.8/src/time/psp/SDL_systime.c
new file mode 100644
index 0000000..cab2429
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/time/psp/SDL_systime.c
@@ -0,0 +1,137 @@
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_PSP
24
25#include <psptypes.h>
26#include <psprtc.h>
27#include <psputility_sysparam.h>
28
29#include "../SDL_time_c.h"
30
31// Sony seems to use 0001-01-01T00:00:00 as an epoch.
32#define DELTA_EPOCH_0001_OFFSET 62135596800ULL
33
34void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
35{
36 int val;
37
38 if (df && sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_DATE_FORMAT, &val) == 0) {
39 switch (val) {
40 case PSP_SYSTEMPARAM_DATE_FORMAT_YYYYMMDD:
41 *df = SDL_DATE_FORMAT_YYYYMMDD;
42 break;
43 case PSP_SYSTEMPARAM_DATE_FORMAT_MMDDYYYY:
44 *df = SDL_DATE_FORMAT_MMDDYYYY;
45 break;
46 case PSP_SYSTEMPARAM_DATE_FORMAT_DDMMYYYY:
47 *df = SDL_DATE_FORMAT_DDMMYYYY;
48 break;
49 default:
50 break;
51 }
52 }
53
54 if (tf && sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_TIME_FORMAT, &val) == 0) {
55 switch (val) {
56 case PSP_SYSTEMPARAM_TIME_FORMAT_24HR:
57 *tf = SDL_TIME_FORMAT_24HR;
58 break;
59 case PSP_SYSTEMPARAM_TIME_FORMAT_12HR:
60 *tf = SDL_TIME_FORMAT_12HR;
61 break;
62 default:
63 break;
64 }
65 }
66}
67
68bool SDL_GetCurrentTime(SDL_Time *ticks)
69{
70 u64 sceTicks;
71
72 if (!ticks) {
73 return SDL_InvalidParamError("ticks");
74 }
75
76 const int ret = sceRtcGetCurrentTick(&sceTicks);
77 if (!ret) {
78 const u32 res = sceRtcGetTickResolution();
79 const u32 div = SDL_NS_PER_SECOND / res;
80 const Uint64 epoch_offset = DELTA_EPOCH_0001_OFFSET * res;
81
82 const Uint64 scetime_min = (Uint64)((SDL_MIN_TIME / div) + epoch_offset);
83 const Uint64 scetime_max = (Uint64)((SDL_MAX_TIME / div) + epoch_offset);
84
85 // Clamp to the valid SDL_Time range.
86 sceTicks = SDL_clamp(sceTicks, scetime_min, scetime_max);
87
88 *ticks = (SDL_Time)(sceTicks - epoch_offset) * div;
89
90 return true;
91 }
92
93 return SDL_SetError("Failed to retrieve system time (%i)", ret);
94}
95
96bool SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, bool localTime)
97{
98 ScePspDateTime t;
99 u64 local;
100 int ret = 0;
101
102 if (!dt) {
103 return SDL_InvalidParamError("dt");
104 }
105
106 const u32 res = sceRtcGetTickResolution();
107 const u32 div = (SDL_NS_PER_SECOND / res);
108 const u64 sceTicks = (u64)((ticks / div) + (DELTA_EPOCH_0001_OFFSET * div));
109
110 if (localTime) {
111 ret = sceRtcConvertUtcToLocalTime(&sceTicks, &local);
112 } else {
113 local = sceTicks;
114 }
115
116 if (!ret) {
117 ret = sceRtcSetTick(&t, &local);
118 if (!ret) {
119 dt->year = t.year;
120 dt->month = t.month;
121 dt->day = t.day;
122 dt->hour = t.hour;
123 dt->minute = t.minute;
124 dt->second = t.second;
125 dt->nanosecond = ticks % SDL_NS_PER_SECOND;
126 dt->utc_offset = (int)(((Sint64)local - (Sint64)sceTicks) / (Sint64)res);
127
128 SDL_CivilToDays(dt->year, dt->month, dt->day, &dt->day_of_week, NULL);
129
130 return true;
131 }
132 }
133
134 return SDL_SetError("Local time conversion failed (%i)", ret);
135}
136
137#endif // SDL_TIME_PSP
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
37void 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
73found_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
101bool 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
153bool 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
diff --git a/contrib/SDL-3.2.8/src/time/vita/SDL_systime.c b/contrib/SDL-3.2.8/src/time/vita/SDL_systime.c
new file mode 100644
index 0000000..4438699
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/time/vita/SDL_systime.c
@@ -0,0 +1,142 @@
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_VITA
24
25#include "../SDL_time_c.h"
26#include <psp2/apputil.h>
27#include <psp2/rtc.h>
28#include <psp2/system_param.h>
29
30// Sony seems to use 0001-01-01T00:00:00 as an epoch.
31#define DELTA_EPOCH_0001_OFFSET 62135596800ULL
32
33void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
34{
35 int val;
36 SceAppUtilInitParam initParam;
37 SceAppUtilBootParam bootParam;
38 SDL_zero(initParam);
39 SDL_zero(bootParam);
40 sceAppUtilInit(&initParam, &bootParam);
41
42 if (df && sceAppUtilSystemParamGetInt(SCE_SYSTEM_PARAM_ID_DATE_FORMAT, &val) == 0) {
43 switch (val) {
44 case SCE_SYSTEM_PARAM_DATE_FORMAT_YYYYMMDD:
45 *df = SDL_DATE_FORMAT_YYYYMMDD;
46 break;
47 case SCE_SYSTEM_PARAM_DATE_FORMAT_MMDDYYYY:
48 *df = SDL_DATE_FORMAT_MMDDYYYY;
49 break;
50 case SCE_SYSTEM_PARAM_DATE_FORMAT_DDMMYYYY:
51 *df = SDL_DATE_FORMAT_DDMMYYYY;
52 break;
53 default:
54 break;
55 }
56 }
57
58 if (tf && sceAppUtilSystemParamGetInt(SCE_SYSTEM_PARAM_ID_DATE_FORMAT, &val) == 0) {
59 switch (val) {
60 case SCE_SYSTEM_PARAM_TIME_FORMAT_24HR:
61 *tf = SDL_TIME_FORMAT_24HR;
62 break;
63 case SCE_SYSTEM_PARAM_TIME_FORMAT_12HR:
64 *tf = SDL_TIME_FORMAT_12HR;
65 break;
66 default:
67 break;
68 }
69 }
70
71 sceAppUtilShutdown();
72}
73
74bool SDL_GetCurrentTime(SDL_Time *ticks)
75{
76 SceRtcTick sceTicks;
77
78 if (!ticks) {
79 return SDL_InvalidParamError("ticks");
80 }
81
82 const int ret = sceRtcGetCurrentTick(&sceTicks);
83 if (!ret) {
84 const unsigned int res = sceRtcGetTickResolution();
85 const unsigned int div = SDL_NS_PER_SECOND / res;
86 const Uint64 epoch_offset = DELTA_EPOCH_0001_OFFSET * res;
87
88 const Uint64 scetime_min = (Uint64)((SDL_MIN_TIME / div) + epoch_offset);
89 const Uint64 scetime_max = (Uint64)((SDL_MAX_TIME / div) + epoch_offset);
90
91 // Clamp to the valid SDL_Time range.
92 sceTicks.tick = SDL_clamp(sceTicks.tick, scetime_min, scetime_max);
93 *ticks = (SDL_Time)(sceTicks.tick - epoch_offset) * div;
94
95 return true;
96 }
97
98 return SDL_SetError("Failed to retrieve system time (%i)", ret);
99}
100
101bool SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, bool localTime)
102{
103 SceDateTime t;
104 SceRtcTick sceTicks, sceLocalTicks;
105 int ret = 0;
106
107 if (!dt) {
108 return SDL_InvalidParamError("dt");
109 }
110
111 const unsigned int res = sceRtcGetTickResolution();
112 const unsigned int div = (SDL_NS_PER_SECOND / res);
113 sceTicks.tick = (Uint64)((ticks / div) + (DELTA_EPOCH_0001_OFFSET * div));
114
115 if (localTime) {
116 ret = sceRtcConvertUtcToLocalTime(&sceTicks, &sceLocalTicks);
117 } else {
118 sceLocalTicks.tick = sceTicks.tick;
119 }
120
121 if (!ret) {
122 ret = sceRtcSetTick(&t, &sceLocalTicks);
123 if (!ret) {
124 dt->year = t.year;
125 dt->month = t.month;
126 dt->day = t.day;
127 dt->hour = t.hour;
128 dt->minute = t.minute;
129 dt->second = t.second;
130 dt->nanosecond = ticks % SDL_NS_PER_SECOND;
131 dt->utc_offset = (int)(((Sint64)sceLocalTicks.tick - (Sint64)sceTicks.tick) / (Sint64)res);
132
133 SDL_CivilToDays(dt->year, dt->month, dt->day, &dt->day_of_week, NULL);
134
135 return true;
136 }
137 }
138
139 return SDL_SetError("Local time conversion failed (%i)", ret);
140}
141
142#endif // SDL_TIME_VITA
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