From 05779fbb9b641aa19c4ca0dc2283d62eadaa18fc Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Sun, 26 Jun 2022 15:37:11 -0400 Subject: [PATCH] Add LocalizedFileTime function - refactor LocalizedTime Signed-off-by: Selva Nair --- localization.c | 61 ++++++++++++++++++++++++++++++++++++++++---------- localization.h | 1 + 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/localization.c b/localization.c index ca385f8..ea204ce 100644 --- a/localization.c +++ b/localization.c @@ -101,33 +101,70 @@ SetGUILanguage(LANGID langId) gui_language = langId; } -int -LocalizedTime(const time_t t, LPTSTR buf, size_t size) +static int +LocalizedSystemTime(const SYSTEMTIME *st, wchar_t *buf, size_t size) { - /* Convert Unix timestamp to Win32 SYSTEMTIME */ - FILETIME lft; - SYSTEMTIME st; - LONGLONG tmp = Int32x32To64(t, 10000000) + 116444736000000000; - FILETIME ft = { .dwLowDateTime = (DWORD) tmp, .dwHighDateTime = tmp >> 32}; - FileTimeToLocalFileTime(&ft, &lft); - FileTimeToSystemTime(&lft, &st); - int date_size = 0, time_size = 0; LCID locale = MAKELCID(GetGUILanguage(), SORT_DEFAULT); + if (size == 0 || buf == NULL) + { + date_size = GetDateFormat(locale, DATE_SHORTDATE, st, NULL, NULL, 0); + time_size = GetTimeFormat(locale, TIME_NOSECONDS, st, NULL, NULL, 0); + return date_size + time_size; + } + if (size > 0) { - date_size = GetDateFormat(locale, DATE_SHORTDATE, &st, NULL, + date_size = GetDateFormat(locale, DATE_SHORTDATE, st, NULL, buf, size); if (date_size) buf[date_size - 1] = ' '; } if (size - date_size > 0) { - time_size = GetTimeFormat(locale, TIME_NOSECONDS, &st, NULL, + time_size = GetTimeFormat(locale, TIME_NOSECONDS, st, NULL, buf + date_size, size - date_size); } return date_size + time_size; } +/* + * Convert filetime to a wide character string -- caller must free the + * result after use. + */ +wchar_t * +LocalizedFileTime(const FILETIME *ft) +{ + FILETIME lft; + SYSTEMTIME st; + FileTimeToLocalFileTime(ft, &lft); + FileTimeToSystemTime(&lft, &st); + wchar_t *buf = NULL; + + int size = LocalizedSystemTime(&st, NULL, 0); + if (size > 0) + { + buf = calloc(1, size*sizeof(wchar_t)); + if (buf) + { + LocalizedSystemTime(&st, buf, size); + } + } + return buf; +} + +int +LocalizedTime(const time_t t, LPTSTR buf, size_t size) +{ + /* Convert Unix timestamp to Win32 SYSTEMTIME */ + FILETIME lft; + SYSTEMTIME st; + LONGLONG tmp = Int32x32To64(t, 10000000) + 116444736000000000; + FILETIME ft = { .dwLowDateTime = (DWORD) tmp, .dwHighDateTime = tmp >> 32}; + FileTimeToLocalFileTime(&ft, &lft); + FileTimeToSystemTime(&lft, &st); + + return LocalizedSystemTime(&st, buf, size); +} static int LoadStringLang(UINT stringId, LANGID langId, PTSTR buffer, int bufferSize, va_list args) diff --git a/localization.h b/localization.h index 856ffe1..18eec80 100644 --- a/localization.h +++ b/localization.h @@ -23,6 +23,7 @@ #define LOCALIZATION_H int LocalizedTime(const time_t, LPTSTR, size_t); +wchar_t *LocalizedFileTime(const FILETIME *ft); PTSTR LoadLocalizedString(const UINT, ...); int LoadLocalizedStringBuf(PTSTR, const int, const UINT, ...); void ShowLocalizedMsg(const UINT, ...);