Util: Refactor localtime_r replacement code into formatting.h
Jeffrey Pfau jeffrey@endrift.com
Tue, 15 Sep 2015 22:23:32 -0700
4 files changed,
37 insertions(+),
18 deletions(-)
M
CMakeLists.txt
→
CMakeLists.txt
@@ -190,6 +190,7 @@
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")@@ -214,6 +215,10 @@ endif()
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)
M
src/gba/hardware.c
→
src/gba/hardware.c
@@ -7,11 +7,8 @@ #include "hardware.h"
#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 };@@ -281,21 +278,7 @@ } else {
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);
M
src/util/formatting.c
→
src/util/formatting.c
@@ -70,3 +70,30 @@ freelocale(l);
#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
M
src/util/formatting.h
→
src/util/formatting.h
@@ -27,4 +27,8 @@
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