Add LocalizedFileTime function

- refactor LocalizedTime

Signed-off-by: Selva Nair <selva.nair@gmail.com>
pull/513/head
Selva Nair 2022-06-26 15:37:11 -04:00 committed by Gert Doering
parent 819629e2a5
commit 05779fbb9b
2 changed files with 53 additions and 15 deletions

View File

@ -101,6 +101,57 @@ SetGUILanguage(LANGID langId)
gui_language = 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 int
LocalizedTime(const time_t t, LPTSTR buf, size_t size) 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); FileTimeToLocalFileTime(&ft, &lft);
FileTimeToSystemTime(&lft, &st); FileTimeToSystemTime(&lft, &st);
int date_size = 0, time_size = 0; return LocalizedSystemTime(&st, buf, size);
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;
} }
static int static int
LoadStringLang(UINT stringId, LANGID langId, PTSTR buffer, int bufferSize, va_list args) LoadStringLang(UINT stringId, LANGID langId, PTSTR buffer, int bufferSize, va_list args)
{ {

View File

@ -23,6 +23,7 @@
#define LOCALIZATION_H #define LOCALIZATION_H
int LocalizedTime(const time_t, LPTSTR, size_t); int LocalizedTime(const time_t, LPTSTR, size_t);
wchar_t *LocalizedFileTime(const FILETIME *ft);
PTSTR LoadLocalizedString(const UINT, ...); PTSTR LoadLocalizedString(const UINT, ...);
int LoadLocalizedStringBuf(PTSTR, const int, const UINT, ...); int LoadLocalizedStringBuf(PTSTR, const int, const UINT, ...);
void ShowLocalizedMsg(const UINT, ...); void ShowLocalizedMsg(const UINT, ...);