diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/time/psp/SDL_systime.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/time/psp/SDL_systime.c | 137 |
1 files changed, 137 insertions, 0 deletions
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 | |||
| 34 | void 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 | |||
| 68 | bool 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 | |||
| 96 | bool 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 | ||
