time: Use a function instead of properties to retrieve the system date and time locale info

This allows applications to re-query the values if the system locale is changed during runtime, and better matches the other locale functions. A note is included in the documentation mentioning that this can be slow, as it has to call into OS functions.

Also allows for the removal of the init/quit time functions, as they are no longer needed.
This commit is contained in:
Frank Praznik 2024-05-08 13:32:37 -04:00
parent e909c0360f
commit 1f43c88220
14 changed files with 119 additions and 100 deletions

View file

@ -61,7 +61,7 @@ typedef struct SDL_DateTime
* *
* \since This enum is available since SDL 3.0.0. * \since This enum is available since SDL 3.0.0.
* *
* \sa SDL_PROP_GLOBAL_SYSTEM_DATE_FORMAT_NUMBER * \sa SDL_GetDateTimeLocalePreferences
*/ */
typedef enum SDL_DateFormat typedef enum SDL_DateFormat
{ {
@ -75,7 +75,7 @@ typedef enum SDL_DateFormat
* *
* \since This enum is available since SDL 3.0.0. * \since This enum is available since SDL 3.0.0.
* *
* \sa SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER * \sa SDL_GetDateTimeLocalePreferences
*/ */
typedef enum SDL_TimeFormat typedef enum SDL_TimeFormat
{ {
@ -83,28 +83,20 @@ typedef enum SDL_TimeFormat
SDL_TIME_FORMAT_12HR = 1 /**< 12 hour time */ SDL_TIME_FORMAT_12HR = 1 /**< 12 hour time */
} SDL_TimeFormat; } SDL_TimeFormat;
/* Global date/time properties. */
/** /**
* The SDL_DateFormat to use as the preferred date display format for the * Gets the current preferred date and time format for the system locale.
* current system locale.
* *
* \since This macro is available since SDL 3.0.0. * This might be a "slow" call that has to query the operating system. It's
* best to ask for this once and save the results. However, the preferred formats
* can change, usually because the user has changed a system preference outside of
* your program.
* *
* \sa SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER * \param dateFormat a pointer to the SDL_DateFormat to hold the returned date format, may be NULL
* \param timeFormat a pointer to the SDL_TimeFormat to hold the returned time format, may be NULL
*
* \since This function is available since SDL 3.0.0.
*/ */
#define SDL_PROP_GLOBAL_SYSTEM_DATE_FORMAT_NUMBER "SDL.time.date_format" extern DECLSPEC int SDLCALL SDL_GetDateTimeLocalePreferences(SDL_DateFormat *dateFormat, SDL_TimeFormat *timeFormat);
/**
* The SDL_TimeFormat to use as the preferred time display format for the
* current system locale.
*
* \since This macro is available since SDL 3.0.0.
*
* \sa SDL_PROP_GLOBAL_SYSTEM_DATE_FORMAT_NUMBER
*/
#define SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER "SDL.time.time_format"
/** /**
* Gets the current value of the system realtime clock in nanoseconds since * Gets the current value of the system realtime clock in nanoseconds since

View file

@ -53,7 +53,6 @@
#define SDL_INIT_EVERYTHING ~0U #define SDL_INIT_EVERYTHING ~0U
/* Initialization/Cleanup routines */ /* Initialization/Cleanup routines */
#include "time/SDL_time_c.h"
#include "timer/SDL_timer_c.h" #include "timer/SDL_timer_c.h"
#ifdef SDL_VIDEO_DRIVER_WINDOWS #ifdef SDL_VIDEO_DRIVER_WINDOWS
extern int SDL_HelperWindowCreate(void); extern int SDL_HelperWindowCreate(void);
@ -210,7 +209,6 @@ int SDL_InitSubSystem(Uint32 flags)
} }
#endif #endif
SDL_InitTime();
SDL_InitTicks(); SDL_InitTicks();
/* Initialize the event subsystem */ /* Initialize the event subsystem */
@ -543,7 +541,6 @@ void SDL_Quit(void)
SDL_QuitSubSystem(SDL_INIT_EVERYTHING); SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
SDL_QuitTicks(); SDL_QuitTicks();
SDL_QuitTime();
#ifdef SDL_USE_LIBDBUS #ifdef SDL_USE_LIBDBUS
SDL_DBus_Quit(); SDL_DBus_Quit();

View file

@ -1015,6 +1015,7 @@ SDL3_0.0.0 {
SDL_wcsnstr; SDL_wcsnstr;
SDL_wcsstr; SDL_wcsstr;
SDL_wcstol; SDL_wcstol;
SDL_GetDateTimeLocalePreferences;
# extra symbols go here (don't modify this line) # extra symbols go here (don't modify this line)
local: *; local: *;
}; };

View file

@ -1039,3 +1039,4 @@
#define SDL_wcsnstr SDL_wcsnstr_REAL #define SDL_wcsnstr SDL_wcsnstr_REAL
#define SDL_wcsstr SDL_wcsstr_REAL #define SDL_wcsstr SDL_wcsstr_REAL
#define SDL_wcstol SDL_wcstol_REAL #define SDL_wcstol SDL_wcstol_REAL
#define SDL_GetDateTimeLocalePreferences SDL_GetDateTimeLocalePreferences_REAL

View file

@ -1055,3 +1055,4 @@ SDL_DYNAPI_PROC(size_t,SDL_wcsnlen,(const wchar_t *a, size_t b),(a,b),return)
SDL_DYNAPI_PROC(wchar_t*,SDL_wcsnstr,(const wchar_t *a, const wchar_t *b, size_t c),(a,b,c),return) SDL_DYNAPI_PROC(wchar_t*,SDL_wcsnstr,(const wchar_t *a, const wchar_t *b, size_t c),(a,b,c),return)
SDL_DYNAPI_PROC(wchar_t*,SDL_wcsstr,(const wchar_t *a, const wchar_t *b),(a,b),return) SDL_DYNAPI_PROC(wchar_t*,SDL_wcsstr,(const wchar_t *a, const wchar_t *b),(a,b),return)
SDL_DYNAPI_PROC(long,SDL_wcstol,(const wchar_t *a, wchar_t **b, int c),(a,b,c),return) SDL_DYNAPI_PROC(long,SDL_wcstol,(const wchar_t *a, wchar_t **b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_GetDateTimeLocalePreferences,(SDL_DateFormat *a, SDL_TimeFormat *b),(a,b),return)

View file

@ -22,8 +22,6 @@
#include "SDL_time_c.h" #include "SDL_time_c.h"
static SDL_bool time_initialized;
/* The following algorithms are based on those of Howard Hinnant and are in the public domain. /* The following algorithms are based on those of Howard Hinnant and are in the public domain.
* *
* http://howardhinnant.github.io/date_algorithms.html * http://howardhinnant.github.io/date_algorithms.html
@ -59,32 +57,19 @@ Sint64 SDL_CivilToDays(int year, int month, int day, int *day_of_week, int *day_
return z; return z;
} }
void SDL_InitTime() int SDL_GetDateTimeLocalePreferences(SDL_DateFormat *dateFormat, SDL_TimeFormat *timeFormat)
{ {
if (time_initialized) {
return;
}
/* Default to ISO 8061 date format, as it is unambiguous, and 24 hour time. */ /* Default to ISO 8061 date format, as it is unambiguous, and 24 hour time. */
SDL_DateFormat dateFormat = SDL_DATE_FORMAT_YYYYMMDD; if (dateFormat) {
SDL_TimeFormat timeFormat = SDL_TIME_FORMAT_24HR; *dateFormat = SDL_DATE_FORMAT_YYYYMMDD;
SDL_PropertiesID props = SDL_GetGlobalProperties();
SDL_GetSystemTimeLocalePreferences(&dateFormat, &timeFormat);
if (!SDL_HasProperty(props, SDL_PROP_GLOBAL_SYSTEM_DATE_FORMAT_NUMBER)) {
SDL_SetNumberProperty(props, SDL_PROP_GLOBAL_SYSTEM_DATE_FORMAT_NUMBER, dateFormat);
} }
if (!SDL_HasProperty(props, SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER)) { if (timeFormat) {
SDL_SetNumberProperty(props, SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER, timeFormat); *timeFormat = SDL_TIME_FORMAT_24HR;
} }
time_initialized = SDL_TRUE; SDL_GetSystemTimeLocalePreferences(dateFormat, timeFormat);
}
void SDL_QuitTime() return 0;
{
time_initialized = SDL_FALSE;
} }
int SDL_GetDaysInMonth(int year, int month) int SDL_GetDaysInMonth(int year, int month)

View file

@ -26,9 +26,6 @@
#define SDL_SECONDS_PER_DAY 86400 #define SDL_SECONDS_PER_DAY 86400
extern void SDL_InitTime(void);
extern void SDL_QuitTime(void);
/* Given a calendar date, returns days since Jan 1 1970, and optionally /* 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). * the day of the week (0-6, 0 is Sunday) and day of the year (0-365).
*/ */

View file

@ -86,15 +86,23 @@ void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
return; return;
} }
*df = LANG_TO_DATE_FORMAT[system_language]; if (df) {
*tf = SDL_TIME_FORMAT_24HR; *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 /* Only American English (en_US) uses MM/DD/YYYY and 12hr system, this gets
the formats wrong for canadians though (en_CA) */ the formats wrong for canadians though (en_CA) */
if (system_language == CFG_LANGUAGE_EN && if (system_language == CFG_LANGUAGE_EN &&
R_SUCCEEDED(has_region) && is_north_america) { R_SUCCEEDED(has_region) && is_north_america) {
*df = SDL_DATE_FORMAT_MMDDYYYY; if (df) {
*tf = SDL_TIME_FORMAT_12HR; *df = SDL_DATE_FORMAT_MMDDYYYY;
}
if (tf) {
*tf = SDL_TIME_FORMAT_12HR;
}
} }
} }

View file

@ -34,7 +34,7 @@ void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
{ {
int val; int val;
if (sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_DATE_FORMAT, &val) == 0) { if (df && sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_DATE_FORMAT, &val) == 0) {
switch (val) { switch (val) {
case PSP_SYSTEMPARAM_DATE_FORMAT_YYYYMMDD: case PSP_SYSTEMPARAM_DATE_FORMAT_YYYYMMDD:
*df = SDL_DATE_FORMAT_YYYYMMDD; *df = SDL_DATE_FORMAT_YYYYMMDD;
@ -50,7 +50,7 @@ void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
} }
} }
if (sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_TIME_FORMAT, &val) == 0) { if (tf && sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_TIME_FORMAT, &val) == 0) {
switch (val) { switch (val) {
case PSP_SYSTEMPARAM_TIME_FORMAT_24HR: case PSP_SYSTEMPARAM_TIME_FORMAT_24HR:
*tf = SDL_TIME_FORMAT_24HR; *tf = SDL_TIME_FORMAT_24HR;

View file

@ -40,54 +40,58 @@ void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
* Android didn't add this until SDK version 26, so a check is needed... * Android didn't add this until SDK version 26, so a check is needed...
*/ */
#ifdef HAVE_NL_LANGINFO #ifdef HAVE_NL_LANGINFO
const char *s = nl_langinfo(D_FMT); if (df) {
const char *s = nl_langinfo(D_FMT);
/* Figure out the preferred system date format from the first format character. */ /* Figure out the preferred system date format from the first format character. */
if (s) { if (s) {
while (*s) { while (*s) {
switch (*s++) { switch (*s++) {
case 'Y': case 'Y':
case 'y': case 'y':
case 'F': case 'F':
case 'C': case 'C':
*df = SDL_DATE_FORMAT_YYYYMMDD; *df = SDL_DATE_FORMAT_YYYYMMDD;
goto found_date; goto found_date;
case 'd': case 'd':
case 'e': case 'e':
*df = SDL_DATE_FORMAT_DDMMYYYY; *df = SDL_DATE_FORMAT_DDMMYYYY;
goto found_date; goto found_date;
case 'b': case 'b':
case 'D': case 'D':
case 'h': case 'h':
case 'm': case 'm':
*df = SDL_DATE_FORMAT_MMDDYYYY; *df = SDL_DATE_FORMAT_MMDDYYYY;
goto found_date; goto found_date;
default: default:
break; break;
}
} }
} }
} }
found_date: found_date:
s = nl_langinfo(T_FMT); if (tf) {
const char *s = nl_langinfo(T_FMT);
/* Figure out the preferred system date format. */ /* Figure out the preferred system date format. */
if (s) { if (s) {
while (*s) { while (*s) {
switch (*s++) { switch (*s++) {
case 'H': case 'H':
case 'k': case 'k':
case 'T': case 'T':
*tf = SDL_TIME_FORMAT_24HR; *tf = SDL_TIME_FORMAT_24HR;
return; return;
case 'I': case 'I':
case 'l': case 'l':
case 'r': case 'r':
*tf = SDL_TIME_FORMAT_12HR; *tf = SDL_TIME_FORMAT_12HR;
return; return;
default: default:
break; break;
}
} }
} }
} }

View file

@ -39,7 +39,7 @@ void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
SDL_zero(bootParam); SDL_zero(bootParam);
sceAppUtilInit(&initParam, &bootParam); sceAppUtilInit(&initParam, &bootParam);
if (sceAppUtilSystemParamGetInt(SCE_SYSTEM_PARAM_ID_DATE_FORMAT, &val) == 0) { if (df && sceAppUtilSystemParamGetInt(SCE_SYSTEM_PARAM_ID_DATE_FORMAT, &val) == 0) {
switch (val) { switch (val) {
case SCE_SYSTEM_PARAM_DATE_FORMAT_YYYYMMDD: case SCE_SYSTEM_PARAM_DATE_FORMAT_YYYYMMDD:
*df = SDL_DATE_FORMAT_YYYYMMDD; *df = SDL_DATE_FORMAT_YYYYMMDD;
@ -55,7 +55,7 @@ void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
} }
} }
if (sceAppUtilSystemParamGetInt(SCE_SYSTEM_PARAM_ID_DATE_FORMAT, &val) == 0) { if (tf && sceAppUtilSystemParamGetInt(SCE_SYSTEM_PARAM_ID_DATE_FORMAT, &val) == 0) {
switch (val) { switch (val) {
case SCE_SYSTEM_PARAM_TIME_FORMAT_24HR: case SCE_SYSTEM_PARAM_TIME_FORMAT_24HR:
*tf = SDL_TIME_FORMAT_24HR; *tf = SDL_TIME_FORMAT_24HR;

View file

@ -38,7 +38,7 @@ 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. */ WCHAR str[80]; /* Per the docs, the time and short date format strings can be a max of 80 characters. */
if (GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, str, sizeof(str) / sizeof(WCHAR))) { if (df && GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, str, sizeof(str) / sizeof(WCHAR))) {
LPWSTR s = str; LPWSTR s = str;
while (*s) { while (*s) {
switch (*s++) { switch (*s++) {
@ -60,7 +60,7 @@ void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
found_date: found_date:
/* Figure out the preferred system date format. */ /* Figure out the preferred system date format. */
if (GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, str, sizeof(str) / sizeof(WCHAR))) { if (tf && GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, str, sizeof(str) / sizeof(WCHAR))) {
LPWSTR s = str; LPWSTR s = str;
while (*s) { while (*s) {
switch (*s++) { switch (*s++) {

View file

@ -169,6 +169,26 @@ static int time_dateTimeUtilities(void *arg)
SDLTest_AssertPass("Call to SDL_TimeFromWindows()"); SDLTest_AssertPass("Call to SDL_TimeFromWindows()");
SDLTest_AssertCheck(ticks > 0 && ticks <= SDL_MAX_TIME, "Check result value, expected >0 && <=%" SDL_PRIs64 ", got: %" SDL_PRIs64, SDL_MAX_TIME, ticks); SDLTest_AssertCheck(ticks > 0 && ticks <= SDL_MAX_TIME, "Check result value, expected >0 && <=%" SDL_PRIs64 ", got: %" SDL_PRIs64, SDL_MAX_TIME, ticks);
/* Test time locale functions */
SDL_DateFormat dateFormat;
SDL_TimeFormat timeFormat;
result = SDL_GetDateTimeLocalePreferences(&dateFormat, &timeFormat);
SDLTest_AssertPass("Call to SDL_GetDateTimeLocalePreferences(&dateFormat, &timeFormat)");
SDLTest_AssertCheck(result == 0, "Check result value, expected 0, got: %i", result);
result = SDL_GetDateTimeLocalePreferences(&dateFormat, NULL);
SDLTest_AssertPass("Call to SDL_GetDateTimeLocalePreferences(&dateFormat, NULL)");
SDLTest_AssertCheck(result == 0, "Check result value, expected 0, got: %i", result);
result = SDL_GetDateTimeLocalePreferences(NULL, &timeFormat);
SDLTest_AssertPass("Call to SDL_GetDateTimeLocalePreferences(NULL, &timeFormat)");
SDLTest_AssertCheck(result == 0, "Check result value, expected 0, got: %i", result);
result = SDL_GetDateTimeLocalePreferences(NULL, NULL);
SDLTest_AssertPass("Call to SDL_GetDateTimeLocalePreferences(NULL, NULL)");
SDLTest_AssertCheck(result == 0, "Check result value, expected 0, got: %i", result);
return TEST_COMPLETED; return TEST_COMPLETED;
} }

View file

@ -79,6 +79,18 @@ static void RenderDateTime(SDL_Renderer *r)
SDLTest_DrawString(r, 10, 15, str); SDLTest_DrawString(r, 10, 15, str);
SDL_TimeToDateTime(ticks, &dt, SDL_TRUE); SDL_TimeToDateTime(ticks, &dt, SDL_TRUE);
if (time_format) {
if (dt.hour > 12) { /* PM */
dt.hour -= 12;
postfix = TIMEPOST[2];
} else {
if (!dt.hour) { /* AM */
dt.hour = 12; /* Midnight */
}
postfix = TIMEPOST[1];
}
}
SDL_snprintf(str, sizeof(str), "Local: %s %02d %s %04d (%s) %02d:%02d:%02d.%09d%s %+05d", SDL_snprintf(str, sizeof(str), "Local: %s %02d %s %04d (%s) %02d:%02d:%02d.%09d%s %+05d",
WDAY[dt.day_of_week], dt.day, MNAME[dt.month - 1], dt.year, short_date, WDAY[dt.day_of_week], dt.day, MNAME[dt.month - 1], dt.year, short_date,
dt.hour, dt.minute, dt.second, dt.nanosecond, postfix, dt.hour, dt.minute, dt.second, dt.nanosecond, postfix,
@ -154,8 +166,7 @@ int main(int argc, char *argv[])
goto quit; goto quit;
} }
time_format = SDL_GetNumberProperty(SDL_GetGlobalProperties(), SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER, SDL_TIME_FORMAT_24HR); SDL_GetDateTimeLocalePreferences(&date_format, &time_format);
date_format = SDL_GetNumberProperty(SDL_GetGlobalProperties(), SDL_PROP_GLOBAL_SYSTEM_DATE_FORMAT_NUMBER, SDL_DATE_FORMAT_YYYYMMDD);
/* Main render loop */ /* Main render loop */
done = 0; done = 0;
@ -196,6 +207,8 @@ int main(int argc, char *argv[])
default: default:
break; break;
} }
} else if (event.type == SDL_EVENT_LOCALE_CHANGED) {
SDL_GetDateTimeLocalePreferences(&date_format, &time_format);
} }
} }