summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/time/vita/SDL_systime.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/SDL-3.2.8/src/time/vita/SDL_systime.c')
-rw-r--r--contrib/SDL-3.2.8/src/time/vita/SDL_systime.c142
1 files changed, 142 insertions, 0 deletions
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