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