diff options
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testtime.c')
-rw-r--r-- | src/contrib/SDL-3.2.20/test/testtime.c | 224 |
1 files changed, 224 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testtime.c b/src/contrib/SDL-3.2.20/test/testtime.c new file mode 100644 index 0000000..008d868 --- /dev/null +++ b/src/contrib/SDL-3.2.20/test/testtime.c | |||
@@ -0,0 +1,224 @@ | |||
1 | /* | ||
2 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
3 | |||
4 | This software is provided 'as-is', without any express or implied | ||
5 | warranty. In no event will the authors be held liable for any damages | ||
6 | arising from the use of this software. | ||
7 | |||
8 | Permission is granted to anyone to use this software for any purpose, | ||
9 | including commercial applications, and to alter it and redistribute it | ||
10 | freely. | ||
11 | */ | ||
12 | |||
13 | /* Test program to verify the SDL date/time APIs */ | ||
14 | #include <SDL3/SDL.h> | ||
15 | #include <SDL3/SDL_main.h> | ||
16 | #include <SDL3/SDL_test.h> | ||
17 | |||
18 | #define CAL_Y_OFF 100.0f | ||
19 | #define CAL_X_OFF 19.0f | ||
20 | #define CELL_WIDTH 86.0f | ||
21 | #define CELL_HEIGHT 60.0f | ||
22 | |||
23 | static int cal_year; | ||
24 | static int cal_month; | ||
25 | static SDL_TimeFormat time_format; | ||
26 | static SDL_DateFormat date_format; | ||
27 | |||
28 | static void RenderDateTime(SDL_Renderer *r) | ||
29 | { | ||
30 | const char *const WDAY[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; | ||
31 | const char *const MNAME[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", | ||
32 | "Aug", "Sep", "Oct", "Nov", "Dec" }; | ||
33 | const char *const TIMEPOST[] = { "", " AM", " PM" }; | ||
34 | |||
35 | int day, len; | ||
36 | const char *postfix = TIMEPOST[0]; | ||
37 | float x, y; | ||
38 | const float x_max = CAL_X_OFF + (CELL_WIDTH * 7); | ||
39 | const float y_max = CAL_Y_OFF + (CELL_HEIGHT * 6); | ||
40 | char str[256]; | ||
41 | char short_date[128]; | ||
42 | SDL_Time ticks; | ||
43 | SDL_DateTime dt; | ||
44 | |||
45 | SDL_SetRenderDrawColor(r, 0xFF, 0xFF, 0xFF, 0xFF); | ||
46 | |||
47 | /* Query the current time and print it. */ | ||
48 | SDL_GetCurrentTime(&ticks); | ||
49 | SDL_TimeToDateTime(ticks, &dt, false); | ||
50 | |||
51 | switch (date_format) { | ||
52 | case SDL_DATE_FORMAT_YYYYMMDD: | ||
53 | SDL_snprintf(short_date, sizeof(short_date), "%04d-%02d-%02d", dt.year, dt.month, dt.day); | ||
54 | break; | ||
55 | case SDL_DATE_FORMAT_DDMMYYYY: | ||
56 | SDL_snprintf(short_date, sizeof(short_date), "%02d.%02d.%04d", dt.day, dt.month, dt.year); | ||
57 | break; | ||
58 | case SDL_DATE_FORMAT_MMDDYYYY: | ||
59 | SDL_snprintf(short_date, sizeof(short_date), "%02d/%02d/%04d", dt.month, dt.day, dt.year); | ||
60 | break; | ||
61 | } | ||
62 | |||
63 | if (time_format) { | ||
64 | if (dt.hour > 12) { /* PM */ | ||
65 | dt.hour -= 12; | ||
66 | postfix = TIMEPOST[2]; | ||
67 | } else { | ||
68 | if (!dt.hour) { /* AM */ | ||
69 | dt.hour = 12; /* Midnight */ | ||
70 | } | ||
71 | postfix = TIMEPOST[1]; | ||
72 | } | ||
73 | } | ||
74 | |||
75 | SDL_snprintf(str, sizeof(str), "UTC: %s %02d %s %04d (%s) %02d:%02d:%02d.%09d%s %+05d", | ||
76 | WDAY[dt.day_of_week], dt.day, MNAME[dt.month - 1], dt.year, short_date, | ||
77 | dt.hour, dt.minute, dt.second, dt.nanosecond, postfix, ((dt.utc_offset / 3600) * 100) + (dt.utc_offset % 3600)); | ||
78 | |||
79 | SDLTest_DrawString(r, 10, 15, str); | ||
80 | |||
81 | SDL_TimeToDateTime(ticks, &dt, true); | ||
82 | if (time_format) { | ||
83 | if (dt.hour > 12) { /* PM */ | ||
84 | dt.hour -= 12; | ||
85 | postfix = TIMEPOST[2]; | ||
86 | } else { | ||
87 | if (!dt.hour) { /* AM */ | ||
88 | dt.hour = 12; /* Midnight */ | ||
89 | } | ||
90 | postfix = TIMEPOST[1]; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | SDL_snprintf(str, sizeof(str), "Local: %s %02d %s %04d (%s) %02d:%02d:%02d.%09d%s %+05d", | ||
95 | WDAY[dt.day_of_week], dt.day, MNAME[dt.month - 1], dt.year, short_date, | ||
96 | dt.hour, dt.minute, dt.second, dt.nanosecond, postfix, | ||
97 | ((dt.utc_offset / 3600) * 100) + (dt.utc_offset % 3600)); | ||
98 | SDLTest_DrawString(r, 10, 30, str); | ||
99 | |||
100 | /* Draw a calendar. */ | ||
101 | if (!cal_month) { | ||
102 | cal_month = dt.month; | ||
103 | cal_year = dt.year; | ||
104 | } | ||
105 | |||
106 | for (y = CAL_Y_OFF; y <= CAL_Y_OFF + (CELL_HEIGHT * 6); y += CELL_HEIGHT) { | ||
107 | SDL_RenderLine(r, CAL_X_OFF, y, x_max, y); | ||
108 | } | ||
109 | for (x = CAL_X_OFF; x <= CAL_X_OFF + (CELL_WIDTH * 7); x += CELL_WIDTH) { | ||
110 | SDL_RenderLine(r, x, CAL_Y_OFF, x, y_max); | ||
111 | } | ||
112 | |||
113 | /* Draw the month and year. */ | ||
114 | len = SDL_snprintf(str, sizeof(str), "%s %04d", MNAME[cal_month - 1], cal_year); | ||
115 | SDLTest_DrawString(r, (CAL_X_OFF + ((x_max - CAL_X_OFF) / 2)) - ((FONT_CHARACTER_SIZE * len) / 2), CAL_Y_OFF - (FONT_LINE_HEIGHT * 3), str); | ||
116 | |||
117 | /* Draw day names */ | ||
118 | for (x = 0; x < 7; ++x) { | ||
119 | float offset = ((CAL_X_OFF + (CELL_WIDTH * x)) + (CELL_WIDTH / 2)) - ((FONT_CHARACTER_SIZE * 3) / 2); | ||
120 | SDLTest_DrawString(r, offset, CAL_Y_OFF - FONT_LINE_HEIGHT, WDAY[(int)x]); | ||
121 | } | ||
122 | |||
123 | day = SDL_GetDayOfWeek(cal_year, cal_month, 1); | ||
124 | x = CAL_X_OFF + (day * CELL_WIDTH + (CELL_WIDTH - (FONT_CHARACTER_SIZE * 3))); | ||
125 | day = 0; | ||
126 | y = CAL_Y_OFF + FONT_LINE_HEIGHT; | ||
127 | while (++day <= SDL_GetDaysInMonth(cal_year, cal_month)) { | ||
128 | SDL_snprintf(str, sizeof(str), "%02d", day); | ||
129 | |||
130 | /* Highlight the current day in red. */ | ||
131 | if (cal_year == dt.year && cal_month == dt.month && day == dt.day) { | ||
132 | SDL_SetRenderDrawColor(r, 0xFF, 0, 0, 0xFF); | ||
133 | } | ||
134 | SDLTest_DrawString(r, x, y, str); | ||
135 | SDL_SetRenderDrawColor(r, 0xFF, 0xFF, 0xFF, 0xFF); | ||
136 | |||
137 | x += CELL_WIDTH; | ||
138 | if (x >= x_max) { | ||
139 | x = CAL_X_OFF + (CELL_WIDTH - (FONT_CHARACTER_SIZE * 3)); | ||
140 | y += CELL_HEIGHT; | ||
141 | } | ||
142 | } | ||
143 | } | ||
144 | |||
145 | int main(int argc, char *argv[]) | ||
146 | { | ||
147 | SDLTest_CommonState *state; | ||
148 | SDL_Event event; | ||
149 | int done; | ||
150 | |||
151 | /* Initialize test framework */ | ||
152 | state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); | ||
153 | if (!state) { | ||
154 | return 1; | ||
155 | } | ||
156 | |||
157 | /* Parse commandline */ | ||
158 | if (!SDLTest_CommonDefaultArgs(state, argc, argv)) { | ||
159 | return 1; | ||
160 | } | ||
161 | |||
162 | if (!SDLTest_CommonInit(state)) { | ||
163 | goto quit; | ||
164 | } | ||
165 | |||
166 | SDL_GetDateTimeLocalePreferences(&date_format, &time_format); | ||
167 | |||
168 | /* Main render loop */ | ||
169 | done = 0; | ||
170 | |||
171 | while (!done) { | ||
172 | /* Check for events */ | ||
173 | while (SDL_PollEvent(&event)) { | ||
174 | SDLTest_CommonEvent(state, &event, &done); | ||
175 | if (event.type == SDL_EVENT_KEY_DOWN) { | ||
176 | switch (event.key.key) { | ||
177 | case SDLK_UP: | ||
178 | if (++cal_month > 12) { | ||
179 | cal_month = 1; | ||
180 | ++cal_year; | ||
181 | } | ||
182 | break; | ||
183 | case SDLK_DOWN: | ||
184 | if (--cal_month < 1) { | ||
185 | cal_month = 12; | ||
186 | --cal_year; | ||
187 | } | ||
188 | break; | ||
189 | case SDLK_1: | ||
190 | time_format = SDL_TIME_FORMAT_24HR; | ||
191 | break; | ||
192 | case SDLK_2: | ||
193 | time_format = SDL_TIME_FORMAT_12HR; | ||
194 | break; | ||
195 | case SDLK_3: | ||
196 | date_format = SDL_DATE_FORMAT_YYYYMMDD; | ||
197 | break; | ||
198 | case SDLK_4: | ||
199 | date_format = SDL_DATE_FORMAT_DDMMYYYY; | ||
200 | break; | ||
201 | case SDLK_5: | ||
202 | date_format = SDL_DATE_FORMAT_MMDDYYYY; | ||
203 | break; | ||
204 | default: | ||
205 | break; | ||
206 | } | ||
207 | } else if (event.type == SDL_EVENT_LOCALE_CHANGED) { | ||
208 | SDL_GetDateTimeLocalePreferences(&date_format, &time_format); | ||
209 | } | ||
210 | } | ||
211 | |||
212 | SDL_SetRenderDrawColor(state->renderers[0], 0x00, 0x00, 0x00, 0xFF); | ||
213 | SDL_RenderClear(state->renderers[0]); | ||
214 | |||
215 | RenderDateTime(state->renderers[0]); | ||
216 | |||
217 | SDL_RenderPresent(state->renderers[0]); | ||
218 | } | ||
219 | |||
220 | quit: | ||
221 | SDLTest_CleanupTextDrawing(); | ||
222 | SDLTest_CommonQuit(state); | ||
223 | return 0; | ||
224 | } | ||