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

@@ -79,6 +79,18 @@ static void RenderDateTime(SDL_Renderer *r)
SDLTest_DrawString(r, 10, 15, str);
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",
WDAY[dt.day_of_week], dt.day, MNAME[dt.month - 1], dt.year, short_date,
dt.hour, dt.minute, dt.second, dt.nanosecond, postfix,
@@ -154,8 +166,7 @@ int main(int argc, char *argv[])
goto quit;
}
time_format = SDL_GetNumberProperty(SDL_GetGlobalProperties(), SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER, SDL_TIME_FORMAT_24HR);
date_format = SDL_GetNumberProperty(SDL_GetGlobalProperties(), SDL_PROP_GLOBAL_SYSTEM_DATE_FORMAT_NUMBER, SDL_DATE_FORMAT_YYYYMMDD);
SDL_GetDateTimeLocalePreferences(&date_format, &time_format);
/* Main render loop */
done = 0;
@@ -196,6 +207,8 @@ int main(int argc, char *argv[])
default:
break;
}
} else if (event.type == SDL_EVENT_LOCALE_CHANGED) {
SDL_GetDateTimeLocalePreferences(&date_format, &time_format);
}
}