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:
@@ -22,8 +22,6 @@
|
||||
|
||||
#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.
|
||||
*
|
||||
* 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;
|
||||
}
|
||||
|
||||
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. */
|
||||
SDL_DateFormat dateFormat = SDL_DATE_FORMAT_YYYYMMDD;
|
||||
SDL_TimeFormat timeFormat = SDL_TIME_FORMAT_24HR;
|
||||
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 (dateFormat) {
|
||||
*dateFormat = SDL_DATE_FORMAT_YYYYMMDD;
|
||||
}
|
||||
if (!SDL_HasProperty(props, SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER)) {
|
||||
SDL_SetNumberProperty(props, SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER, timeFormat);
|
||||
if (timeFormat) {
|
||||
*timeFormat = SDL_TIME_FORMAT_24HR;
|
||||
}
|
||||
|
||||
time_initialized = SDL_TRUE;
|
||||
}
|
||||
SDL_GetSystemTimeLocalePreferences(dateFormat, timeFormat);
|
||||
|
||||
void SDL_QuitTime()
|
||||
{
|
||||
time_initialized = SDL_FALSE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SDL_GetDaysInMonth(int year, int month)
|
||||
|
||||
@@ -26,9 +26,6 @@
|
||||
|
||||
#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
|
||||
* the day of the week (0-6, 0 is Sunday) and day of the year (0-365).
|
||||
*/
|
||||
|
||||
@@ -86,15 +86,23 @@ void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
|
||||
return;
|
||||
}
|
||||
|
||||
*df = LANG_TO_DATE_FORMAT[system_language];
|
||||
*tf = SDL_TIME_FORMAT_24HR;
|
||||
if (df) {
|
||||
*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
|
||||
the formats wrong for canadians though (en_CA) */
|
||||
if (system_language == CFG_LANGUAGE_EN &&
|
||||
R_SUCCEEDED(has_region) && is_north_america) {
|
||||
*df = SDL_DATE_FORMAT_MMDDYYYY;
|
||||
*tf = SDL_TIME_FORMAT_12HR;
|
||||
if (df) {
|
||||
*df = SDL_DATE_FORMAT_MMDDYYYY;
|
||||
}
|
||||
if (tf) {
|
||||
*tf = SDL_TIME_FORMAT_12HR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
|
||||
{
|
||||
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) {
|
||||
case PSP_SYSTEMPARAM_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) {
|
||||
case PSP_SYSTEMPARAM_TIME_FORMAT_24HR:
|
||||
*tf = SDL_TIME_FORMAT_24HR;
|
||||
|
||||
@@ -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...
|
||||
*/
|
||||
#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. */
|
||||
if (s) {
|
||||
while (*s) {
|
||||
switch (*s++) {
|
||||
case 'Y':
|
||||
case 'y':
|
||||
case 'F':
|
||||
case 'C':
|
||||
*df = SDL_DATE_FORMAT_YYYYMMDD;
|
||||
goto found_date;
|
||||
case 'd':
|
||||
case 'e':
|
||||
*df = SDL_DATE_FORMAT_DDMMYYYY;
|
||||
goto found_date;
|
||||
case 'b':
|
||||
case 'D':
|
||||
case 'h':
|
||||
case 'm':
|
||||
*df = SDL_DATE_FORMAT_MMDDYYYY;
|
||||
goto found_date;
|
||||
default:
|
||||
break;
|
||||
/* Figure out the preferred system date format from the first format character. */
|
||||
if (s) {
|
||||
while (*s) {
|
||||
switch (*s++) {
|
||||
case 'Y':
|
||||
case 'y':
|
||||
case 'F':
|
||||
case 'C':
|
||||
*df = SDL_DATE_FORMAT_YYYYMMDD;
|
||||
goto found_date;
|
||||
case 'd':
|
||||
case 'e':
|
||||
*df = SDL_DATE_FORMAT_DDMMYYYY;
|
||||
goto found_date;
|
||||
case 'b':
|
||||
case 'D':
|
||||
case 'h':
|
||||
case 'm':
|
||||
*df = SDL_DATE_FORMAT_MMDDYYYY;
|
||||
goto found_date;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
found_date:
|
||||
|
||||
s = nl_langinfo(T_FMT);
|
||||
if (tf) {
|
||||
const char *s = nl_langinfo(T_FMT);
|
||||
|
||||
/* Figure out the preferred system date format. */
|
||||
if (s) {
|
||||
while (*s) {
|
||||
switch (*s++) {
|
||||
case 'H':
|
||||
case 'k':
|
||||
case 'T':
|
||||
*tf = SDL_TIME_FORMAT_24HR;
|
||||
return;
|
||||
case 'I':
|
||||
case 'l':
|
||||
case 'r':
|
||||
*tf = SDL_TIME_FORMAT_12HR;
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
/* Figure out the preferred system date format. */
|
||||
if (s) {
|
||||
while (*s) {
|
||||
switch (*s++) {
|
||||
case 'H':
|
||||
case 'k':
|
||||
case 'T':
|
||||
*tf = SDL_TIME_FORMAT_24HR;
|
||||
return;
|
||||
case 'I':
|
||||
case 'l':
|
||||
case 'r':
|
||||
*tf = SDL_TIME_FORMAT_12HR;
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
|
||||
SDL_zero(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) {
|
||||
case SCE_SYSTEM_PARAM_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) {
|
||||
case SCE_SYSTEM_PARAM_TIME_FORMAT_24HR:
|
||||
*tf = SDL_TIME_FORMAT_24HR;
|
||||
|
||||
@@ -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. */
|
||||
|
||||
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;
|
||||
while (*s) {
|
||||
switch (*s++) {
|
||||
@@ -60,7 +60,7 @@ void SDL_GetSystemTimeLocalePreferences(SDL_DateFormat *df, SDL_TimeFormat *tf)
|
||||
found_date:
|
||||
|
||||
/* 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;
|
||||
while (*s) {
|
||||
switch (*s++) {
|
||||
|
||||
Reference in New Issue
Block a user