From 5a079a2d114f96d4847d1ee305d5b7c16eeec50e Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 27 Dec 2025 12:03:39 -0800 Subject: Initial commit --- contrib/SDL-3.2.8/src/time/SDL_time.c | 222 +++++++++++++++++++++++ contrib/SDL-3.2.8/src/time/SDL_time_c.h | 36 ++++ contrib/SDL-3.2.8/src/time/n3ds/SDL_systime.c | 147 +++++++++++++++ contrib/SDL-3.2.8/src/time/ps2/SDL_systime.c | 65 +++++++ contrib/SDL-3.2.8/src/time/psp/SDL_systime.c | 137 ++++++++++++++ contrib/SDL-3.2.8/src/time/unix/SDL_systime.c | 197 ++++++++++++++++++++ contrib/SDL-3.2.8/src/time/vita/SDL_systime.c | 142 +++++++++++++++ contrib/SDL-3.2.8/src/time/windows/SDL_systime.c | 157 ++++++++++++++++ 8 files changed, 1103 insertions(+) create mode 100644 contrib/SDL-3.2.8/src/time/SDL_time.c create mode 100644 contrib/SDL-3.2.8/src/time/SDL_time_c.h create mode 100644 contrib/SDL-3.2.8/src/time/n3ds/SDL_systime.c create mode 100644 contrib/SDL-3.2.8/src/time/ps2/SDL_systime.c create mode 100644 contrib/SDL-3.2.8/src/time/psp/SDL_systime.c create mode 100644 contrib/SDL-3.2.8/src/time/unix/SDL_systime.c create mode 100644 contrib/SDL-3.2.8/src/time/vita/SDL_systime.c create mode 100644 contrib/SDL-3.2.8/src/time/windows/SDL_systime.c (limited to 'contrib/SDL-3.2.8/src/time') 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 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#include "SDL_time_c.h" + +/* The following algorithms are based on those of Howard Hinnant and are in the public domain. + * + * http://howardhinnant.github.io/date_algorithms.html + */ + +/* Given a calendar date, returns days since Jan 1 1970, and optionally + * the day of the week [0-6, 0 is Sunday] and day of the year [0-365]. + */ +Sint64 SDL_CivilToDays(int year, int month, int day, int *day_of_week, int *day_of_year) +{ + + year -= month <= 2; + const int era = (year >= 0 ? year : year - 399) / 400; + const unsigned yoe = (unsigned)(year - era * 400); // [0, 399] + const unsigned doy = (153 * (month > 2 ? month - 3 : month + 9) + 2) / 5 + day - 1; // [0, 365] + const unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096] + const Sint64 z = (Sint64)(era) * 146097 + (Sint64)(doe)-719468; + + if (day_of_week) { + *day_of_week = (int)(z >= -4 ? (z + 4) % 7 : (z + 5) % 7 + 6); + } + if (day_of_year) { + // This algorithm considers March 1 to be the first day of the year, so offset by Jan + Feb. + if (doy > 305) { + // Day 0 is the first day of the year. + *day_of_year = doy - 306; + } else { + const int doy_offset = 59 + (!(year % 4) && ((year % 100) || !(year % 400))); + *day_of_year = doy + doy_offset; + } + } + + return z; +} + +bool SDL_GetDateTimeLocalePreferences(SDL_DateFormat *dateFormat, SDL_TimeFormat *timeFormat) +{ + // Default to ISO 8061 date format, as it is unambiguous, and 24 hour time. + if (dateFormat) { + *dateFormat = SDL_DATE_FORMAT_YYYYMMDD; + } + if (timeFormat) { + *timeFormat = SDL_TIME_FORMAT_24HR; + } + + SDL_GetSystemTimeLocalePreferences(dateFormat, timeFormat); + + return true; +} + +int SDL_GetDaysInMonth(int year, int month) +{ + static const int DAYS_IN_MONTH[] = { + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 + }; + + if (month < 1 || month > 12) { + SDL_SetError("Month out of range [1-12], requested: %i", month); + return -1; + } + + int days = DAYS_IN_MONTH[month - 1]; + + /* A leap year occurs every 4 years... + * but not every 100 years... + * except for every 400 years. + */ + if (month == 2 && (!(year % 4) && ((year % 100) || !(year % 400)))) { + ++days; + } + + return days; +} + +int SDL_GetDayOfYear(int year, int month, int day) +{ + int dayOfYear; + + if (month < 1 || month > 12) { + SDL_SetError("Month out of range [1-12], requested: %i", month); + return -1; + } + if (day < 1 || day > SDL_GetDaysInMonth(year, month)) { + SDL_SetError("Day out of range [1-%i], requested: %i", SDL_GetDaysInMonth(year, month), month); + return -1; + } + + SDL_CivilToDays(year, month, day, NULL, &dayOfYear); + return dayOfYear; +} + +int SDL_GetDayOfWeek(int year, int month, int day) +{ + int dayOfWeek; + + if (month < 1 || month > 12) { + SDL_SetError("Month out of range [1-12], requested: %i", month); + return -1; + } + if (day < 1 || day > SDL_GetDaysInMonth(year, month)) { + SDL_SetError("Day out of range [1-%i], requested: %i", SDL_GetDaysInMonth(year, month), month); + return -1; + } + + SDL_CivilToDays(year, month, day, &dayOfWeek, NULL); + return dayOfWeek; +} + +static bool SDL_DateTimeIsValid(const SDL_DateTime *dt) +{ + if (dt->month < 1 || dt->month > 12) { + SDL_SetError("Malformed SDL_DateTime: month out of range [1-12], current: %i", dt->month); + return false; + } + + const int daysInMonth = SDL_GetDaysInMonth(dt->year, dt->month); + if (dt->day < 1 || dt->day > daysInMonth) { + SDL_SetError("Malformed SDL_DateTime: day of month out of range [1-%i], current: %i", daysInMonth, dt->month); + return false; + } + if (dt->hour < 0 || dt->hour > 23) { + SDL_SetError("Malformed SDL_DateTime: hour out of range [0-23], current: %i", dt->hour); + return false; + } + if (dt->minute < 0 || dt->minute > 59) { + SDL_SetError("Malformed SDL_DateTime: minute out of range [0-59], current: %i", dt->minute); + return false; + } + if (dt->second < 0 || dt->second > 60) { + SDL_SetError("Malformed SDL_DateTime: second out of range [0-60], current: %i", dt->second); + return false; // 60 accounts for a possible leap second. + } + if (dt->nanosecond < 0 || dt->nanosecond >= SDL_NS_PER_SECOND) { + SDL_SetError("Malformed SDL_DateTime: nanosecond out of range [0-999999999], current: %i", dt->nanosecond); + return false; + } + + return true; +} + +bool SDL_DateTimeToTime(const SDL_DateTime *dt, SDL_Time *ticks) +{ + static const Sint64 max_seconds = SDL_NS_TO_SECONDS(SDL_MAX_TIME) - 1; + static const Sint64 min_seconds = SDL_NS_TO_SECONDS(SDL_MIN_TIME) + 1; + bool result = true; + + if (!dt) { + return SDL_InvalidParamError("dt"); + } + if (!ticks) { + return SDL_InvalidParamError("ticks"); + } + if (!SDL_DateTimeIsValid(dt)) { + // The validation function sets the error string. + return false; + } + + *ticks = SDL_CivilToDays(dt->year, dt->month, dt->day, NULL, NULL) * SDL_SECONDS_PER_DAY; + *ticks += (((dt->hour * 60) + dt->minute) * 60) + dt->second - dt->utc_offset; + if (*ticks > max_seconds || *ticks < min_seconds) { + *ticks = SDL_clamp(*ticks, min_seconds, max_seconds); + result = SDL_SetError("Date out of range for SDL_Time representation; SDL_Time value clamped"); + } + *ticks = SDL_SECONDS_TO_NS(*ticks) + dt->nanosecond; + + return result; +} + +#define DELTA_EPOCH_1601_100NS (11644473600ll * 10000000ll) // [100 ns] (100 ns units between 1601-01-01 and 1970-01-01, 11644473600 seconds) + +void SDL_TimeToWindows(SDL_Time ticks, Uint32 *dwLowDateTime, Uint32 *dwHighDateTime) +{ + /* Convert nanoseconds to Win32 ticks. + * SDL_Time has a range of roughly 292 years, so even SDL_MIN_TIME can't underflow the Win32 epoch. + */ + const Uint64 wtime = (Uint64)((ticks / 100) + DELTA_EPOCH_1601_100NS); + + if (dwLowDateTime) { + *dwLowDateTime = (Uint32)wtime; + } + + if (dwHighDateTime) { + *dwHighDateTime = (Uint32)(wtime >> 32); + } +} + +SDL_Time SDL_TimeFromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime) +{ + static const Uint64 wintime_min = (Uint64)((SDL_MIN_TIME / 100) + DELTA_EPOCH_1601_100NS); + static const Uint64 wintime_max = (Uint64)((SDL_MAX_TIME / 100) + DELTA_EPOCH_1601_100NS); + + Uint64 wtime = (((Uint64)dwHighDateTime << 32) | dwLowDateTime); + + // Clamp the windows time range to the SDL_Time min/max + wtime = SDL_clamp(wtime, wintime_min, wintime_max); + + return (SDL_Time)(wtime - DELTA_EPOCH_1601_100NS) * 100; +} 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 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL_time_c_h_ +#define SDL_time_c_h_ + +#include "SDL_internal.h" + +#define SDL_SECONDS_PER_DAY 86400 + +/* Given a calendar date, returns days since Jan 1 1970, and optionally + * the day of the week (0-6, 0 is Sunday) and day of the year (0-365). + */ +extern Sint64 SDL_CivilToDays(int year, int month, int day, int *day_of_week, int *day_of_year); + +extern void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf); + +#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 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_TIME_N3DS + +#include "../SDL_time_c.h" +#include <3ds.h> + +/* + * The 3DS clock is essentially a simple digital watch and provides + * no timezone or DST functionality. + */ + +// 3DS epoch is Jan 1 1900 +#define DELTA_EPOCH_1900_OFFSET_MS 2208988800000LL + +/* Returns year/month/day triple in civil calendar + * Preconditions: z is number of days since 1970-01-01 and is in the range: + * [INT_MIN, INT_MAX-719468]. + * + * http://howardhinnant.github.io/date_algorithms.html#civil_from_days + */ +static void civil_from_days(int days, int *year, int *month, int *day) +{ + days += 719468; + const int era = (days >= 0 ? days : days - 146096) / 146097; + const unsigned doe = (unsigned)(days - era * 146097); // [0, 146096] + const unsigned yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399] + const int y = (int)(yoe) + era * 400; + const unsigned doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365] + const unsigned mp = (5 * doy + 2) / 153; // [0, 11] + const unsigned d = doy - (153 * mp + 2) / 5 + 1; // [1, 31] + const unsigned m = mp < 10 ? mp + 3 : mp - 9; // [1, 12] + + *year = y + (m <= 2); + *month = (int)m; + *day = (int)d; +} + +void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf) +{ + // The 3DS only has 12 supported languages, so take the standard for each + static const SDL_DateFormat LANG_TO_DATE_FORMAT[] = { + SDL_DATE_FORMAT_YYYYMMDD, // JP + SDL_DATE_FORMAT_DDMMYYYY, // EN, assume non-american format + SDL_DATE_FORMAT_DDMMYYYY, // FR + SDL_DATE_FORMAT_DDMMYYYY, // DE + SDL_DATE_FORMAT_DDMMYYYY, // IT + SDL_DATE_FORMAT_DDMMYYYY, // ES + SDL_DATE_FORMAT_YYYYMMDD, // ZH (CN) + SDL_DATE_FORMAT_YYYYMMDD, // KR + SDL_DATE_FORMAT_DDMMYYYY, // NL + SDL_DATE_FORMAT_DDMMYYYY, // PT + SDL_DATE_FORMAT_DDMMYYYY, // RU + SDL_DATE_FORMAT_YYYYMMDD // ZH (TW) + }; + u8 system_language, is_north_america; + Result result, has_region; + + if (R_FAILED(cfguInit())) { + return; + } + result = CFGU_GetSystemLanguage(&system_language); + has_region = CFGU_GetRegionCanadaUSA(&is_north_america); + cfguExit(); + if (R_FAILED(result)) { + return; + } + + if (df) { + *df = LANG_TO_DATE_FORMAT[system_language]; + } + if (tf) { + *tf = SDL_TIME_FORMAT_24HR; + } + + /* Only American English (en_US) uses MM/DD/YYYY and 12hr system, this gets + the formats wrong for canadians though (en_CA) */ + if (system_language == CFG_LANGUAGE_EN && + R_SUCCEEDED(has_region) && is_north_america) { + if (df) { + *df = SDL_DATE_FORMAT_MMDDYYYY; + } + if (tf) { + *tf = SDL_TIME_FORMAT_12HR; + } + } +} + +bool SDL_GetCurrentTime(SDL_Time *ticks) +{ + if (!ticks) { + return SDL_InvalidParamError("ticks"); + } + + // Returns milliseconds since the epoch. + const Uint64 ndsTicksMax = (SDL_MAX_TIME / SDL_NS_PER_MS) + DELTA_EPOCH_1900_OFFSET_MS; + const Uint64 ndsTicks = SDL_min(osGetTime(), ndsTicksMax); + + *ticks = SDL_MS_TO_NS(ndsTicks - DELTA_EPOCH_1900_OFFSET_MS); + + return true; +} + +bool SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, bool localTime) +{ + if (!dt) { + return SDL_InvalidParamError("dt"); + } + + const int days = (int)(SDL_NS_TO_SECONDS(ticks) / SDL_SECONDS_PER_DAY); + civil_from_days(days, &dt->year, &dt->month, &dt->day); + + int rem = (int)(SDL_NS_TO_SECONDS(ticks) - (days * SDL_SECONDS_PER_DAY)); + dt->hour = rem / (60 * 60); + rem -= dt->hour * 60 * 60; + dt->minute = rem / 60; + rem -= dt->minute * 60; + dt->second = rem; + dt->nanosecond = ticks % SDL_NS_PER_SECOND; + dt->utc_offset = 0; // Unknown + + SDL_CivilToDays(dt->year, dt->month, dt->day, &dt->day_of_week, NULL); + + return true; +} + +#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 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_TIME_PS2 + +#include "../SDL_time_c.h" + +// PS2 epoch is Jan 1 2000 JST (UTC +9) +#define UNIX_EPOCH_OFFSET_SEC 946717200 + +// TODO: Implement this... +void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf) +{ +} + +bool SDL_GetCurrentTime(SDL_Time *ticks) +{ + if (!ticks) { + return SDL_InvalidParamError("ticks"); + } + + *ticks = 0; + + return true; +} + +bool SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, bool localTime) +{ + if (!dt) { + return SDL_InvalidParamError("dt"); + } + + dt->year = 1970; + dt->month = 1; + dt->day = 1; + dt->hour = 0; + dt->minute = 0; + dt->second = 0; + dt->nanosecond = 0; + dt->day_of_week = 4; + dt->utc_offset = 0; + + return true; +} + +#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 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_TIME_PSP + +#include +#include +#include + +#include "../SDL_time_c.h" + +// Sony seems to use 0001-01-01T00:00:00 as an epoch. +#define DELTA_EPOCH_0001_OFFSET 62135596800ULL + +void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf) +{ + int val; + + if (df && sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_DATE_FORMAT, &val) == 0) { + switch (val) { + case PSP_SYSTEMPARAM_DATE_FORMAT_YYYYMMDD: + *df = SDL_DATE_FORMAT_YYYYMMDD; + break; + case PSP_SYSTEMPARAM_DATE_FORMAT_MMDDYYYY: + *df = SDL_DATE_FORMAT_MMDDYYYY; + break; + case PSP_SYSTEMPARAM_DATE_FORMAT_DDMMYYYY: + *df = SDL_DATE_FORMAT_DDMMYYYY; + break; + default: + break; + } + } + + if (tf && sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_TIME_FORMAT, &val) == 0) { + switch (val) { + case PSP_SYSTEMPARAM_TIME_FORMAT_24HR: + *tf = SDL_TIME_FORMAT_24HR; + break; + case PSP_SYSTEMPARAM_TIME_FORMAT_12HR: + *tf = SDL_TIME_FORMAT_12HR; + break; + default: + break; + } + } +} + +bool SDL_GetCurrentTime(SDL_Time *ticks) +{ + u64 sceTicks; + + if (!ticks) { + return SDL_InvalidParamError("ticks"); + } + + const int ret = sceRtcGetCurrentTick(&sceTicks); + if (!ret) { + const u32 res = sceRtcGetTickResolution(); + const u32 div = SDL_NS_PER_SECOND / res; + const Uint64 epoch_offset = DELTA_EPOCH_0001_OFFSET * res; + + const Uint64 scetime_min = (Uint64)((SDL_MIN_TIME / div) + epoch_offset); + const Uint64 scetime_max = (Uint64)((SDL_MAX_TIME / div) + epoch_offset); + + // Clamp to the valid SDL_Time range. + sceTicks = SDL_clamp(sceTicks, scetime_min, scetime_max); + + *ticks = (SDL_Time)(sceTicks - epoch_offset) * div; + + return true; + } + + return SDL_SetError("Failed to retrieve system time (%i)", ret); +} + +bool SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, bool localTime) +{ + ScePspDateTime t; + u64 local; + int ret = 0; + + if (!dt) { + return SDL_InvalidParamError("dt"); + } + + const u32 res = sceRtcGetTickResolution(); + const u32 div = (SDL_NS_PER_SECOND / res); + const u64 sceTicks = (u64)((ticks / div) + (DELTA_EPOCH_0001_OFFSET * div)); + + if (localTime) { + ret = sceRtcConvertUtcToLocalTime(&sceTicks, &local); + } else { + local = sceTicks; + } + + if (!ret) { + ret = sceRtcSetTick(&t, &local); + if (!ret) { + dt->year = t.year; + dt->month = t.month; + dt->day = t.day; + dt->hour = t.hour; + dt->minute = t.minute; + dt->second = t.second; + dt->nanosecond = ticks % SDL_NS_PER_SECOND; + dt->utc_offset = (int)(((Sint64)local - (Sint64)sceTicks) / (Sint64)res); + + SDL_CivilToDays(dt->year, dt->month, dt->day, &dt->day_of_week, NULL); + + return true; + } + } + + return SDL_SetError("Local time conversion failed (%i)", ret); +} + +#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 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_TIME_UNIX + +#include "../SDL_time_c.h" +#include +#include +#include +#include + +#if !defined(HAVE_CLOCK_GETTIME) && defined(SDL_PLATFORM_APPLE) +#include +#include +#include +#endif + +void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf) +{ + /* This *should* be well-supported aside from very old legacy systems, but apparently + * Android didn't add this until SDK version 26, so a check is needed... + */ +#ifdef HAVE_NL_LANGINFO + if (df) { + const char *s = nl_langinfo(D_FMT); + + // Figure out the preferred system date format from the first format character. + if (s) { + while (*s) { + switch (*s++) { + case 'Y': + case 'y': + case 'F': + case 'C': + *df = SDL_DATE_FORMAT_YYYYMMDD; + goto found_date; + case 'd': + case 'e': + *df = SDL_DATE_FORMAT_DDMMYYYY; + goto found_date; + case 'b': + case 'D': + case 'h': + case 'm': + *df = SDL_DATE_FORMAT_MMDDYYYY; + goto found_date; + default: + break; + } + } + } + } + +found_date: + + if (tf) { + const char *s = nl_langinfo(T_FMT); + + // Figure out the preferred system date format. + if (s) { + while (*s) { + switch (*s++) { + case 'H': + case 'k': + case 'T': + *tf = SDL_TIME_FORMAT_24HR; + return; + case 'I': + case 'l': + case 'r': + *tf = SDL_TIME_FORMAT_12HR; + return; + default: + break; + } + } + } + } +#endif +} + +bool SDL_GetCurrentTime(SDL_Time *ticks) +{ + if (!ticks) { + return SDL_InvalidParamError("ticks"); + } +#ifdef HAVE_CLOCK_GETTIME + struct timespec tp; + + if (clock_gettime(CLOCK_REALTIME, &tp) == 0) { + //tp.tv_sec = SDL_min(tp.tv_sec, SDL_NS_TO_SECONDS(SDL_MAX_TIME) - 1); + *ticks = SDL_SECONDS_TO_NS(tp.tv_sec) + tp.tv_nsec; + return true; + } + + SDL_SetError("Failed to retrieve system time (%i)", errno); + +#elif defined(SDL_PLATFORM_APPLE) + clock_serv_t cclock; + int ret = host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); + if (ret == 0) { + mach_timespec_t mts; + + SDL_zero(mts); + ret = clock_get_time(cclock, &mts); + if (ret == 0) { + // mach_timespec_t tv_sec is 32-bit, so no overflow possible + *ticks = SDL_SECONDS_TO_NS(mts.tv_sec) + mts.tv_nsec; + } + mach_port_deallocate(mach_task_self(), cclock); + + if (!ret) { + return true; + } + } + + SDL_SetError("Failed to retrieve system time (%i)", ret); + +#else + struct timeval tv; + SDL_zero(tv); + if (gettimeofday(&tv, NULL) == 0) { + tv.tv_sec = SDL_min(tv.tv_sec, SDL_NS_TO_SECONDS(SDL_MAX_TIME) - 1); + *ticks = SDL_SECONDS_TO_NS(tv.tv_sec) + SDL_US_TO_NS(tv.tv_usec); + return true; + } + + SDL_SetError("Failed to retrieve system time (%i)", errno); +#endif + + return false; +} + +bool SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, bool localTime) +{ +#if defined (HAVE_GMTIME_R) || defined(HAVE_LOCALTIME_R) + struct tm tm_storage; +#endif + struct tm *tm = NULL; + + if (!dt) { + return SDL_InvalidParamError("dt"); + } + + const time_t tval = (time_t)SDL_NS_TO_SECONDS(ticks); + + if (localTime) { +#ifdef HAVE_LOCALTIME_R + tm = localtime_r(&tval, &tm_storage); +#else + tm = localtime(&tval); +#endif + } else { +#ifdef HAVE_GMTIME_R + tm = gmtime_r(&tval, &tm_storage); +#else + tm = gmtime(&tval); +#endif + } + + if (tm) { + dt->year = tm->tm_year + 1900; + dt->month = tm->tm_mon + 1; + dt->day = tm->tm_mday; + dt->hour = tm->tm_hour; + dt->minute = tm->tm_min; + dt->second = tm->tm_sec; + dt->nanosecond = ticks % SDL_NS_PER_SECOND; + dt->day_of_week = tm->tm_wday; + dt->utc_offset = (int)tm->tm_gmtoff; + + return true; + } + + return SDL_SetError("SDL_DateTime conversion failed (%i)", errno); +} + +#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 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_TIME_VITA + +#include "../SDL_time_c.h" +#include +#include +#include + +// Sony seems to use 0001-01-01T00:00:00 as an epoch. +#define DELTA_EPOCH_0001_OFFSET 62135596800ULL + +void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf) +{ + int val; + SceAppUtilInitParam initParam; + SceAppUtilBootParam bootParam; + SDL_zero(initParam); + SDL_zero(bootParam); + sceAppUtilInit(&initParam, &bootParam); + + if (df && sceAppUtilSystemParamGetInt(SCE_SYSTEM_PARAM_ID_DATE_FORMAT, &val) == 0) { + switch (val) { + case SCE_SYSTEM_PARAM_DATE_FORMAT_YYYYMMDD: + *df = SDL_DATE_FORMAT_YYYYMMDD; + break; + case SCE_SYSTEM_PARAM_DATE_FORMAT_MMDDYYYY: + *df = SDL_DATE_FORMAT_MMDDYYYY; + break; + case SCE_SYSTEM_PARAM_DATE_FORMAT_DDMMYYYY: + *df = SDL_DATE_FORMAT_DDMMYYYY; + break; + default: + break; + } + } + + if (tf && sceAppUtilSystemParamGetInt(SCE_SYSTEM_PARAM_ID_DATE_FORMAT, &val) == 0) { + switch (val) { + case SCE_SYSTEM_PARAM_TIME_FORMAT_24HR: + *tf = SDL_TIME_FORMAT_24HR; + break; + case SCE_SYSTEM_PARAM_TIME_FORMAT_12HR: + *tf = SDL_TIME_FORMAT_12HR; + break; + default: + break; + } + } + + sceAppUtilShutdown(); +} + +bool SDL_GetCurrentTime(SDL_Time *ticks) +{ + SceRtcTick sceTicks; + + if (!ticks) { + return SDL_InvalidParamError("ticks"); + } + + const int ret = sceRtcGetCurrentTick(&sceTicks); + if (!ret) { + const unsigned int res = sceRtcGetTickResolution(); + const unsigned int div = SDL_NS_PER_SECOND / res; + const Uint64 epoch_offset = DELTA_EPOCH_0001_OFFSET * res; + + const Uint64 scetime_min = (Uint64)((SDL_MIN_TIME / div) + epoch_offset); + const Uint64 scetime_max = (Uint64)((SDL_MAX_TIME / div) + epoch_offset); + + // Clamp to the valid SDL_Time range. + sceTicks.tick = SDL_clamp(sceTicks.tick, scetime_min, scetime_max); + *ticks = (SDL_Time)(sceTicks.tick - epoch_offset) * div; + + return true; + } + + return SDL_SetError("Failed to retrieve system time (%i)", ret); +} + +bool SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, bool localTime) +{ + SceDateTime t; + SceRtcTick sceTicks, sceLocalTicks; + int ret = 0; + + if (!dt) { + return SDL_InvalidParamError("dt"); + } + + const unsigned int res = sceRtcGetTickResolution(); + const unsigned int div = (SDL_NS_PER_SECOND / res); + sceTicks.tick = (Uint64)((ticks / div) + (DELTA_EPOCH_0001_OFFSET * div)); + + if (localTime) { + ret = sceRtcConvertUtcToLocalTime(&sceTicks, &sceLocalTicks); + } else { + sceLocalTicks.tick = sceTicks.tick; + } + + if (!ret) { + ret = sceRtcSetTick(&t, &sceLocalTicks); + if (!ret) { + dt->year = t.year; + dt->month = t.month; + dt->day = t.day; + dt->hour = t.hour; + dt->minute = t.minute; + dt->second = t.second; + dt->nanosecond = ticks % SDL_NS_PER_SECOND; + dt->utc_offset = (int)(((Sint64)sceLocalTicks.tick - (Sint64)sceTicks.tick) / (Sint64)res); + + SDL_CivilToDays(dt->year, dt->month, dt->day, &dt->day_of_week, NULL); + + return true; + } + } + + return SDL_SetError("Local time conversion failed (%i)", ret); +} + +#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 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_TIME_WINDOWS + +#include "../../core/windows/SDL_windows.h" + +#include "../SDL_time_c.h" + +#define NS_PER_WINDOWS_TICK 100ULL +#define WINDOWS_TICK 10000000ULL +#define UNIX_EPOCH_OFFSET_SEC 11644473600ULL + +typedef void(WINAPI *pfnGetSystemTimePreciseAsFileTime)(FILETIME *); + +void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf) +{ + WCHAR str[80]; // Per the docs, the time and short date format strings can be a max of 80 characters. + + if (df && GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, str, sizeof(str) / sizeof(WCHAR))) { + LPWSTR s = str; + while (*s) { + switch (*s++) { + case L'y': + *df = SDL_DATE_FORMAT_YYYYMMDD; + goto found_date; + case L'd': + *df = SDL_DATE_FORMAT_DDMMYYYY; + goto found_date; + case L'M': + *df = SDL_DATE_FORMAT_MMDDYYYY; + goto found_date; + default: + break; + } + } + } + +found_date: + + // Figure out the preferred system date format. + if (tf && GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, str, sizeof(str) / sizeof(WCHAR))) { + LPWSTR s = str; + while (*s) { + switch (*s++) { + case L'H': + *tf = SDL_TIME_FORMAT_24HR; + return; + case L'h': + *tf = SDL_TIME_FORMAT_12HR; + return; + default: + break; + } + } + } +} + +bool SDL_GetCurrentTime(SDL_Time *ticks) +{ + FILETIME ft; + + if (!ticks) { + return SDL_InvalidParamError("ticks"); + } + + SDL_zero(ft); + + static pfnGetSystemTimePreciseAsFileTime pGetSystemTimePreciseAsFileTime = NULL; + static bool load_attempted = false; + + // Only available in Win8/Server 2012 or higher. + if (!pGetSystemTimePreciseAsFileTime && !load_attempted) { + HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll")); + if (kernel32) { + pGetSystemTimePreciseAsFileTime = (pfnGetSystemTimePreciseAsFileTime)GetProcAddress(kernel32, "GetSystemTimePreciseAsFileTime"); + } + load_attempted = true; + } + + if (pGetSystemTimePreciseAsFileTime) { + pGetSystemTimePreciseAsFileTime(&ft); + } else { + GetSystemTimeAsFileTime(&ft); + } + + *ticks = SDL_TimeFromWindows(ft.dwLowDateTime, ft.dwHighDateTime); + + return true; +} + +bool SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, bool localTime) +{ + FILETIME ft, local_ft; + SYSTEMTIME utc_st, local_st; + SYSTEMTIME *st = NULL; + Uint32 low, high; + + if (!dt) { + return SDL_InvalidParamError("dt"); + } + + SDL_TimeToWindows(ticks, &low, &high); + ft.dwLowDateTime = (DWORD)low; + ft.dwHighDateTime = (DWORD)high; + + if (FileTimeToSystemTime(&ft, &utc_st)) { + if (localTime) { + if (SystemTimeToTzSpecificLocalTime(NULL, &utc_st, &local_st)) { + // Calculate the difference for the UTC offset. + SystemTimeToFileTime(&local_st, &local_ft); + const SDL_Time local_ticks = SDL_TimeFromWindows(local_ft.dwLowDateTime, local_ft.dwHighDateTime); + dt->utc_offset = (int)SDL_NS_TO_SECONDS(local_ticks - ticks); + st = &local_st; + } + } else { + dt->utc_offset = 0; + st = &utc_st; + } + + if (st) { + dt->year = st->wYear; + dt->month = st->wMonth; + dt->day = st->wDay; + dt->hour = st->wHour; + dt->minute = st->wMinute; + dt->second = st->wSecond; + dt->nanosecond = ticks % SDL_NS_PER_SECOND; + dt->day_of_week = st->wDayOfWeek; + + return true; + } + } + + return SDL_SetError("SDL_DateTime conversion failed (%lu)", GetLastError()); +} + +#endif // SDL_TIME_WINDOWS -- cgit v1.2.3