Move strftime_am_pm to libretro-common rtime.c, remove duplicate

functions
This commit is contained in:
libretroadmin 2022-07-12 03:29:16 +02:00
parent f39df40728
commit 99186b1056
4 changed files with 40 additions and 58 deletions

View File

@ -40,6 +40,11 @@ void rtime_init(void);
/* Must be called upon program termination */
void rtime_deinit(void);
/* Time format strings with AM-PM designation require special
* handling due to platform dependence */
void strftime_am_pm(char *s, size_t len, const char* format,
const struct tm* timeptr);
/* Thread-safe wrapper for localtime() */
struct tm *rtime_localtime(const time_t *timep, struct tm *result);

View File

@ -27,7 +27,13 @@
#endif
#include <string.h>
#include <locale.h>
#include <time/rtime.h>
#include <string/stdstring.h>
#if !(defined(__linux__) && !defined(ANDROID))
#include <encodings/utf.h>
#endif
#ifdef HAVE_THREADS
/* TODO/FIXME - global */
@ -79,3 +85,27 @@ struct tm *rtime_localtime(const time_t *timep, struct tm *result)
return result;
}
/* Time format strings with AM-PM designation require special
* handling due to platform dependence */
void strftime_am_pm(char *s, size_t len, const char* format,
const struct tm* timeptr)
{
char *local = NULL;
/* Ensure correct locale is set
* > Required for localised AM/PM strings */
setlocale(LC_TIME, "");
strftime(s, len, format, timeptr);
#if !(defined(__linux__) && !defined(ANDROID))
if ((local = local_to_utf8_string_alloc(s)))
{
if (!string_is_empty(local))
strlcpy(s, local, len);
free(local);
local = NULL;
}
#endif
}

View File

@ -636,32 +636,6 @@ bool menu_entries_list_search(const char *needle, size_t *idx)
return match_found;
}
/* Time format strings with AM-PM designation require special
* handling due to platform dependence */
static void strftime_am_pm(char *s, size_t len, const char* format,
const struct tm* timeptr)
{
char *local = NULL;
/* Ensure correct locale is set
* > Required for localised AM/PM strings */
setlocale(LC_TIME, "");
strftime(s, len, format, timeptr);
#if !(defined(__linux__) && !defined(ANDROID))
local = local_to_utf8_string_alloc(s);
if (local)
{
if (!string_is_empty(local))
strlcpy(s, local, len);
free(local);
local = NULL;
}
#endif
}
/* Display the date and time - time_mode will influence how
* the time representation will look like.
* */

View File

@ -62,11 +62,9 @@ static bool RtlJSONObjectMemberHandler(void *ctx, const char *s, size_t len)
{
RtlJSONContext *p_ctx = (RtlJSONContext*)ctx;
/* something went wrong */
if (p_ctx->current_entry_val)
{
/* something went wrong */
return false;
}
if (len)
{
@ -129,8 +127,7 @@ static void runtime_log_read_file(runtime_log_t *runtime_log)
}
/* Initialise JSON parser */
parser = rjson_open_rfile(file);
if (!parser)
if (!(parser = rjson_open_rfile(file)))
{
RARCH_ERR("Failed to create JSON parser.\n");
goto end;
@ -377,9 +374,8 @@ runtime_log_t *runtime_log_init(
/* Phew... If we get this far then all is well.
* > Create 'runtime_log' object */
runtime_log = (runtime_log_t*)
malloc(sizeof(*runtime_log));
if (!runtime_log)
if (!(runtime_log = (runtime_log_t*)
malloc(sizeof(*runtime_log))))
return NULL;
/* > Populate default values */
@ -625,29 +621,6 @@ void runtime_log_get_last_played_time(runtime_log_t *runtime_log,
mktime(time_info);
}
static void last_played_strftime(char *s, size_t len, const char *format,
const struct tm *timeptr)
{
char *local = NULL;
/* Ensure correct locale is set */
setlocale(LC_TIME, "");
/* Generate string */
strftime(s, len, format, timeptr);
#if !(defined(__linux__) && !defined(ANDROID))
local = local_to_utf8_string_alloc(s);
if (local)
{
if (!string_is_empty(local))
strlcpy(s, local, len);
free(local);
local = NULL;
}
#endif
}
static void last_played_human(runtime_log_t *runtime_log,
char *str, size_t len)
{
@ -855,7 +828,7 @@ void runtime_log_get_last_played_str(runtime_log_t *runtime_log,
/* Get time */
struct tm time_info;
runtime_log_get_last_played_time(runtime_log, &time_info);
last_played_strftime(tmp, sizeof(tmp), format_str, &time_info);
strftime_am_pm(tmp, sizeof(tmp), format_str, &time_info);
}
snprintf(str, len, "%s%s",
msg_hash_to_str(