From 2e53dbd254bdcfee7083addab0f0d8589741ff3c Mon Sep 17 00:00:00 2001 From: Heiko Hund Date: Tue, 6 Nov 2012 14:54:53 +0100 Subject: [PATCH] localize connection time display in tray tooltip --- localization.c | 25 +++++++++++++++++++++++++ localization.h | 1 + tray.c | 7 +++---- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/localization.c b/localization.c index 8447073..17b4d4a 100644 --- a/localization.c +++ b/localization.c @@ -95,6 +95,31 @@ SetGUILanguage(LANGID langId) gui_language = langId; } +int +LocalizedTime(const time_t t, LPTSTR buf, size_t size) +{ + /* Convert Unix timestamp to Win32 SYSTEMTIME */ + SYSTEMTIME st; + LONGLONG tmp = Int32x32To64(t, 10000000) + 116444736000000000; + FILETIME ft = { .dwLowDateTime = (DWORD) tmp, .dwHighDateTime = tmp >> 32}; + FileTimeToSystemTime(&ft, &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; +} + static int LoadStringLang(UINT stringId, LANGID langId, PTSTR buffer, int bufferSize, va_list args) diff --git a/localization.h b/localization.h index 72b7e9d..7a58db3 100644 --- a/localization.h +++ b/localization.h @@ -22,6 +22,7 @@ #ifndef LOCALIZATION_H #define LOCALIZATION_H +int LocalizedTime(const time_t, LPTSTR, size_t); PTSTR LoadLocalizedString(const UINT, ...); int LoadLocalizedStringBuf(PTSTR, const int, const UINT, ...); void ShowLocalizedMsg(const UINT, ...); diff --git a/tray.c b/tray.c index 39be3dd..3959ae5 100644 --- a/tray.c +++ b/tray.c @@ -259,7 +259,6 @@ SetTrayIcon(conn_state_t state) TCHAR msg[500]; TCHAR msg_connected[100]; TCHAR msg_connecting[100]; - TCHAR connected_since[50]; int i, config = 0; BOOL first_conn; UINT icon_id; @@ -291,11 +290,11 @@ SetTrayIcon(conn_state_t state) if (CountConnState(connected) == 1) { /* Append "Connected since and assigned IP" to message */ - _tcsftime(connected_since, _countof(connected_since), _T("%b %d, %H:%M"), - localtime(&o.conn[config].connected_since)); + TCHAR time[50]; + LocalizedTime(o.conn[config].connected_since, time, _countof(time)); _tcsncat(msg, LoadLocalizedString(IDS_TIP_CONNECTED_SINCE), _countof(msg) - _tcslen(msg) - 1); - _tcsncat(msg, connected_since, _countof(msg) - _tcslen(msg) - 1); + _tcsncat(msg, time, _countof(msg) - _tcslen(msg) - 1); if (_tcslen(o.conn[config].ip) > 0) { TCHAR *assigned_ip = LoadLocalizedString(IDS_TIP_ASSIGNED_IP, o.conn[config].ip);