diff --git a/src/openrct2-ui/windows/LoadSave.cpp b/src/openrct2-ui/windows/LoadSave.cpp index 3495993861..271137e484 100644 --- a/src/openrct2-ui/windows/LoadSave.cpp +++ b/src/openrct2-ui/windows/LoadSave.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #pragma region Widgets @@ -128,8 +129,8 @@ typedef struct loadsave_list_item { char name[256]; char path[MAX_PATH]; time_t date_modified; - char date_formatted[20]; - char time_formatted[20]; + std::string date_formatted; + std::string time_formatted; uint8 type; bool loaded; } loadsave_list_item; @@ -524,13 +525,13 @@ static void window_loadsave_compute_max_date_width() tm.tm_yday = 294; tm.tm_isdst = -1; - char date[20]; - std::strftime(date, sizeof(date), "%x", &tm); - maxDateWidth = gfx_get_string_width(date); + std::time_t long_time = mktime(&tm); - char time[20]; - std::strftime(time, sizeof(time), "%X", &tm); - maxTimeWidth = gfx_get_string_width(time); + std::string date = Platform::FormatShortDate(long_time); + maxDateWidth = gfx_get_string_width(date.c_str()); + + std::string time = Platform::FormatTime(long_time); + maxTimeWidth = gfx_get_string_width(time.c_str()); } static void window_loadsave_invalidate(rct_window *w) @@ -644,10 +645,10 @@ static void window_loadsave_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, s sint32 offset = w->widgets[WIDX_SORT_DATE].left + maxDateWidth; set_format_arg(0, rct_string_id, STR_STRING); - set_format_arg(2, char*, _listItems[i].date_formatted); + set_format_arg(2, char*, _listItems[i].date_formatted.c_str()); gfx_draw_string_right_clipped(dpi, stringId, gCommonFormatArgs, COLOUR_BLACK, offset - 2, y, maxDateWidth); - set_format_arg(2, char*, _listItems[i].time_formatted); + set_format_arg(2, char*, _listItems[i].time_formatted.c_str()); gfx_draw_string_left_clipped(dpi, stringId, gCommonFormatArgs, COLOUR_BLACK, offset + 2, y, maxTimeWidth); } } @@ -810,8 +811,8 @@ static void window_loadsave_populate_list(rct_window *w, sint32 includeNewItem, listItem->date_modified = platform_file_get_modified_time(listItem->path); // Cache a human-readable version of the modified date. - std::strftime(listItem->date_formatted, sizeof(listItem->date_formatted), "%x", std::localtime(&listItem->date_modified)); - std::strftime(listItem->time_formatted, sizeof(listItem->time_formatted), "%X", std::localtime(&listItem->date_modified)); + listItem->date_formatted = Platform::FormatShortDate(listItem->date_modified); + listItem->time_formatted = Platform::FormatTime(listItem->date_modified); // Mark if file is the currently loaded game listItem->loaded = strcmp(listItem->path, gCurrentLoadedPath) == 0; diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index 15f5f5faae..caa4a65839 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "../core/String.hpp" #include "Platform2.h" #include "platform.h" @@ -82,6 +83,20 @@ namespace Platform platform_get_openrct_data_path(path, sizeof(path)); return path; } + + std::string FormatShortDate(std::time_t timestamp) + { + char date[20]; + std::strftime(date, sizeof(date), "%x", std::gmtime(×tamp)); + return std::string(date); + } + + std::string FormatTime(std::time_t timestamp) + { + char time[20]; + std::strftime(time, sizeof(time), "%X", std::gmtime(×tamp)); + return std::string(time); + } } #endif diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 3b94c7f818..589b2b68c4 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -18,6 +18,7 @@ #define WIN32_LEAN_AND_MEAN #include +#include #include #undef GetEnvironmentVariable @@ -144,6 +145,37 @@ namespace Platform platform_get_openrct_data_path(path, sizeof(path)); return path; } + + SYSTEMTIME * TimeToSystemTime(std::time_t timestamp) + { + LONGLONG ll = Int32x32To64(timestamp, 10000000) + 116444736000000000; + + FILETIME ft; + ft.dwLowDateTime = (DWORD) ll; + ft.dwHighDateTime = ll >> 32; + + SYSTEMTIME st; + FileTimeToSystemTime(&ft, &st); + return &st; + } + + std::string FormatShortDate(std::time_t timestamp) + { + SYSTEMTIME * st = TimeToSystemTime(timestamp); + + wchar_t date[20]; + GetDateFormatEx(LOCALE_NAME_USER_DEFAULT, DATE_SHORTDATE, st, nullptr, &date, sizeof(date), nullptr); + return String::ToUtf8(std::wstring(date)); + } + + std::string FormatTime(std::time_t timestamp) + { + SYSTEMTIME * st = TimeToSystemTime(timestamp); + + wchar_t time[20]; + GetTimeFormatEx(LOCALE_NAME_USER_DEFAULT, 0, st, nullptr, &time, sizeof(time)); + return String::ToUtf8(std::wstring(time)); + } } #endif diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index 16f5eb9575..9ae79139e1 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -18,6 +18,7 @@ #ifdef __cplusplus +#include #include #include "../common.h" @@ -40,6 +41,9 @@ namespace Platform std::string GetEnvironmentPath(const char * name); std::string GetHomePath(); #endif + + std::string FormatShortDate(std::time_t timestamp); + std::string FormatTime(std::time_t timestamp); } #endif