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 | 67 +++++++++++++++++++++++++++++++++++++++----------- localization.h | 1 + 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/localization.c b/localization.c index ca385f8..ea204ce 100644 --- a/localization.c +++ b/localization.c @@ -101,6 +101,57 @@ SetGUILanguage(LANGID langId) gui_language = langId; } +static int +LocalizedSystemTime(const SYSTEMTIME *st, wchar_t *buf, size_t size) +{ + 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, + buf, size); + if (date_size) + buf[date_size - 1] = ' '; + } + if (size - date_size > 0) { + 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) { @@ -112,23 +163,9 @@ LocalizedTime(const time_t t, LPTSTR buf, size_t size) FileTimeToLocalFileTime(&ft, &lft); FileTimeToSystemTime(&lft, &st); - int date_size = 0, time_size = 0; - LCID locale = MAKELCID(GetGUILanguage(), SORT_DEFAULT); - - if (size > 0) { - 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, - buf + date_size, size - date_size); - } - return date_size + time_size; + 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, ...);