Util: Refactor localtime_r replacement code into formatting.h

This commit is contained in:
Jeffrey Pfau 2015-09-15 22:23:32 -07:00
parent 590d23ea8a
commit bcf6e5879b
4 changed files with 37 additions and 18 deletions

View File

@ -190,6 +190,7 @@ endif()
include(CheckFunctionExists)
check_function_exists(strdup HAVE_STRDUP)
check_function_exists(strndup HAVE_STRNDUP)
check_function_exists(localtime_r HAVE_LOCALTIME_R)
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Generic")
check_function_exists(snprintf_l HAVE_SNPRINTF_L)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
@ -216,6 +217,10 @@ if(HAVE_STRNDUP)
add_definitions(-DHAVE_STRNDUP)
endif()
if(HAVE_LOCALTIME_R)
add_definitions(-DHAVE_LOCALTIME_R)
endif()
if(HAVE_NEWLOCALE AND HAVE_FREELOCALE AND HAVE_USELOCALE)
add_definitions(-DHAVE_LOCALE)
if (HAVE_STRTOF_L)

View File

@ -7,12 +7,9 @@
#include "gba/io.h"
#include "gba/serialize.h"
#include "util/formatting.h"
#include "util/hash.h"
#ifdef PSP2
#include <psp2/rtc.h>
#endif
const int GBA_LUX_LEVELS[10] = { 5, 11, 18, 27, 42, 62, 84, 109, 139, 183 };
static void _readPins(struct GBACartridgeHardware* hw);
@ -281,21 +278,7 @@ void _rtcUpdateClock(struct GBACartridgeHardware* hw) {
t = time(0);
}
struct tm date;
#ifdef _WIN32
localtime_s(&date, &t);
#elif defined(PSP2)
SceRtcTime sceRtc;
sceRtcSetTime_t(&sceRtc, t);
date.tm_year = sceRtc.year;
date.tm_mon = sceRtc.month;
date.tm_mday = sceRtc.day;
date.tm_hour = sceRtc.hour;
date.tm_min = sceRtc.minutes;
date.tm_sec = sceRtc.seconds;
date.tm_wday = sceRtcGetDayOfWeek(sceRtc.year, sceRtc.month, sceRtc.day);
#else
localtime_r(&t, &date);
#endif
hw->rtc.time[0] = _rtcBCD(date.tm_year - 100);
hw->rtc.time[1] = _rtcBCD(date.tm_mon + 1);
hw->rtc.time[2] = _rtcBCD(date.tm_mday);

View File

@ -70,3 +70,30 @@ float strtof_u(const char* restrict str, char** restrict end) {
#endif
return res;
}
#ifndef HAVE_LOCALTIME_R
#ifdef PSP2
#include <psp2/rtc.h>
#endif
struct tm* localtime_r(const time_t* t, struct tm* date) {
#ifdef _WIN32
localtime_s(date, t);
return date;
#elif defined(PSP2)
SceRtcTime sceRtc;
sceRtcSetTime_t(&sceRtc, *t);
date->tm_year = sceRtc.year;
date->tm_mon = sceRtc.month;
date->tm_mday = sceRtc.day;
date->tm_hour = sceRtc.hour;
date->tm_min = sceRtc.minutes;
date->tm_sec = sceRtc.seconds;
date->tm_wday = sceRtcGetDayOfWeek(sceRtc.year, sceRtc.month, sceRtc.day);
return date;
#else
#warning localtime_r not emulated on this platform
return 0;
#endif
}
#endif

View File

@ -27,4 +27,8 @@ float strtof_l(const char* restrict str, char** restrict end, locale_t locale);
int ftostr_u(char* restrict str, size_t size, float f);
float strtof_u(const char* restrict str, char** restrict end);
#ifndef HAVE_LOCALTIME_R
struct tm* localtime_r(const time_t* timep, struct tm* result);
#endif
#endif