1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
|
/*
Miranda NG: the free IM client for Microsoft* Windows*
Copyright (c) 2012-14 Miranda NG project (http://miranda-ng.org),
Copyright (c) 2000-12 Miranda IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
implements services to handle location - based timezones, instead of
simple UTC offsets.
*/
#include "..\..\core\commonheaders.h"
TIME_API tmi;
typedef DWORD (WINAPI *pfnGetDynamicTimeZoneInformation_t)(DYNAMIC_TIME_ZONE_INFORMATION *pdtzi);
static pfnGetDynamicTimeZoneInformation_t pfnGetDynamicTimeZoneInformation;
typedef struct _REG_TZI_FORMAT
{
LONG Bias;
LONG StandardBias;
LONG DaylightBias;
SYSTEMTIME StandardDate;
SYSTEMTIME DaylightDate;
} REG_TZI_FORMAT;
#define MIM_TZ_DISPLAYLEN 128
struct MIM_TIMEZONE
{
unsigned hash;
int offset;
TCHAR tszName[MIM_TZ_NAMELEN]; // windows name for the time zone
wchar_t szDisplay[MIM_TZ_DISPLAYLEN]; // more descriptive display name (that's what usually appears in dialogs)
// every hour should be sufficient.
TIME_ZONE_INFORMATION tzi;
static int compareBias(const MIM_TIMEZONE* p1, const MIM_TIMEZONE* p2)
{ return p2->tzi.Bias - p1->tzi.Bias; }
};
typedef struct
{
DWORD timestamp; // last time updated
MIM_TIMEZONE myTZ; // set to my own timezone
} TZ_INT_INFO;
static TZ_INT_INFO myInfo;
static OBJLIST<MIM_TIMEZONE> g_timezones(55, NumericKeySortT);
static LIST<MIM_TIMEZONE> g_timezonesBias(55, MIM_TIMEZONE::compareBias);
void FormatTime (const SYSTEMTIME *st, const TCHAR *szFormat, TCHAR *szDest, int cbDest);
void UnixTimeToFileTime(mir_time ts, LPFILETIME pft);
mir_time FileTimeToUnixTime(LPFILETIME pft);
#define fnSystemTimeToTzSpecificLocalTime SystemTimeToTzSpecificLocalTime
static int timeapiGetTimeZoneTime(HANDLE hTZ, SYSTEMTIME *st)
{
if (st == NULL) return 1;
MIM_TIMEZONE *tz = (MIM_TIMEZONE*)hTZ;
if (tz == UTC_TIME_HANDLE)
GetSystemTime(st);
else if (tz && tz != &myInfo.myTZ)
{
SYSTEMTIME sto;
GetSystemTime(&sto);
return !fnSystemTimeToTzSpecificLocalTime(&tz->tzi, &sto, st);
}
else
GetLocalTime(st);
return 0;
}
static LPCTSTR timeapiGetTzName(HANDLE hTZ)
{
MIM_TIMEZONE *tz = (MIM_TIMEZONE*)hTZ;
if (tz == NULL)
return myInfo.myTZ.tszName;
else if (tz == UTC_TIME_HANDLE)
return _T("UTC");
return tz->tszName;
}
static LPCTSTR timeapiGetTzDescription(LPCTSTR TZname)
{
for (int i=0; i < g_timezonesBias.getCount(); i++)
{
MIM_TIMEZONE *tz = g_timezonesBias[i];
if (!lstrcmp(tz->tszName, TZname))
return tz->szDisplay;
}
return _T("");
}
static void CalcTsOffset(MIM_TIMEZONE *tz)
{
SYSTEMTIME st, stl;
GetSystemTime(&st);
FILETIME ft;
SystemTimeToFileTime(&st, &ft);
mir_time ts1 = FileTimeToUnixTime(&ft);
if ( !fnSystemTimeToTzSpecificLocalTime(&tz->tzi, &st, &stl))
return;
SystemTimeToFileTime(&stl, &ft);
mir_time ts2 = FileTimeToUnixTime(&ft);
tz->offset = ts2 - ts1;
}
static bool IsSameTime(MIM_TIMEZONE *tz)
{
SYSTEMTIME st, stl;
if (tz == &myInfo.myTZ)
return true;
timeapiGetTimeZoneTime(tz, &stl);
timeapiGetTimeZoneTime(NULL, &st);
return st.wHour == stl.wHour && st.wMinute == stl.wMinute;
}
static HANDLE timeapiGetInfoByName(LPCTSTR tszName, DWORD dwFlags)
{
if (tszName == NULL)
return (dwFlags & (TZF_DIFONLY | TZF_KNOWNONLY)) ? NULL : &myInfo.myTZ;
if (_tcscmp(myInfo.myTZ.tszName, tszName) == 0)
return (dwFlags & TZF_DIFONLY) ? NULL : &myInfo.myTZ;
MIM_TIMEZONE tzsearch;
tzsearch.hash = mir_hashstrT(tszName);
MIM_TIMEZONE *tz = g_timezones.find(&tzsearch);
if (tz == NULL)
return (dwFlags & (TZF_DIFONLY | TZF_KNOWNONLY)) ? NULL : &myInfo.myTZ;
if (dwFlags & TZF_DIFONLY)
return IsSameTime(tz) ? NULL : tz;
return tz;
}
static HANDLE timeapiGetInfoByContact(HANDLE hContact, DWORD dwFlags)
{
if (hContact == NULL)
return (dwFlags & (TZF_DIFONLY | TZF_KNOWNONLY)) ? NULL : &myInfo.myTZ;
DBVARIANT dbv;
if ( !db_get_ts(hContact, "UserInfo", "TzName", &dbv))
{
HANDLE res = timeapiGetInfoByName(dbv.ptszVal, dwFlags);
db_free(&dbv);
if (res) return res;
}
signed char timezone = (signed char)db_get_b(hContact, "UserInfo", "Timezone", -1);
if (timezone == -1)
{
char *szProto = GetContactProto(hContact);
if ( !db_get_ts(hContact, szProto, "TzName", &dbv))
{
HANDLE res = timeapiGetInfoByName(dbv.ptszVal, dwFlags);
db_free(&dbv);
if (res) return res;
}
timezone = (signed char)db_get_b(hContact, szProto, "Timezone", -1);
}
if (timezone != -1)
{
MIM_TIMEZONE tzsearch;
tzsearch.tzi.Bias = timezone * 30;
if (myInfo.myTZ.tzi.Bias == tzsearch.tzi.Bias)
{
if (dwFlags & TZF_DIFONLY) return NULL;
return &myInfo.myTZ;
}
int i = g_timezonesBias.getIndex(&tzsearch);
while (i >= 0 && g_timezonesBias[i]->tzi.Bias == tzsearch.tzi.Bias) --i;
int delta = LONG_MAX;
for (int j = ++i; j < g_timezonesBias.getCount() && g_timezonesBias[j]->tzi.Bias == tzsearch.tzi.Bias; ++j)
{
int delta1 = abs(g_timezonesBias[j]->tzi.DaylightDate.wMonth - myInfo.myTZ.tzi.DaylightDate.wMonth);
if (delta1 <= delta)
{
delta = delta1;
i = j;
}
}
if (i >= 0)
{
MIM_TIMEZONE *tz = g_timezonesBias[i];
return ((dwFlags & TZF_DIFONLY) && IsSameTime(tz)) ? NULL : tz;
}
}
return (dwFlags & (TZF_DIFONLY | TZF_KNOWNONLY)) ? NULL : &myInfo.myTZ;
}
static void timeapiSetInfoByContact(HANDLE hContact, HANDLE hTZ)
{
MIM_TIMEZONE *tz = (MIM_TIMEZONE*)hTZ;
if (hContact == NULL) return;
if (tz)
{
db_set_ts(hContact, "UserInfo", "TzName", tz->tszName);
db_set_b(hContact, "UserInfo", "Timezone", (char)((tz->tzi.Bias + tz->tzi.StandardBias) / 30));
}
else
{
db_unset(hContact, "UserInfo", "TzName");
db_unset(hContact, "UserInfo", "Timezone");
}
}
static int timeapiPrintDateTime(HANDLE hTZ, LPCTSTR szFormat, LPTSTR szDest, int cbDest, DWORD dwFlags)
{
MIM_TIMEZONE *tz = (MIM_TIMEZONE*)hTZ;
if (tz == NULL && (dwFlags & (TZF_DIFONLY | TZF_KNOWNONLY)))
return 1;
SYSTEMTIME st;
if (timeapiGetTimeZoneTime(tz, &st))
return 1;
FormatTime(&st, szFormat, szDest, cbDest);
return 0;
}
static int timeapiPrintTimeStamp(HANDLE hTZ, mir_time ts, LPCTSTR szFormat, LPTSTR szDest, int cbDest, DWORD dwFlags)
{
MIM_TIMEZONE *tz = (MIM_TIMEZONE*)hTZ;
if (tz == NULL && (dwFlags & (TZF_DIFONLY | TZF_KNOWNONLY)))
return 1;
FILETIME ft;
if (tz == NULL) tz = &myInfo.myTZ;
if (tz == NULL)
{
FILETIME lft;
UnixTimeToFileTime(ts, &lft);
FileTimeToLocalFileTime(&lft, &ft);
}
else if (tz == UTC_TIME_HANDLE)
UnixTimeToFileTime(ts, &ft);
else
{
if (tz->offset == INT_MIN)
CalcTsOffset(tz);
UnixTimeToFileTime(ts + tz->offset, &ft);
}
SYSTEMTIME st;
FileTimeToSystemTime(&ft, &st);
FormatTime(&st, szFormat, szDest, cbDest);
return 0;
}
static LPTIME_ZONE_INFORMATION timeapiGetTzi(HANDLE hTZ)
{
MIM_TIMEZONE *tz = (MIM_TIMEZONE*)hTZ;
return tz ? &tz->tzi : &myInfo.myTZ.tzi;
}
static mir_time timeapiTimeStampToTimeZoneTimeStamp(HANDLE hTZ, mir_time ts)
{
MIM_TIMEZONE *tz = (MIM_TIMEZONE*)hTZ;
if (tz == NULL) tz = &myInfo.myTZ;
if (tz == NULL)
{
FILETIME ft, lft;
UnixTimeToFileTime(ts, &ft);
FileTimeToLocalFileTime(&ft, &lft);
return FileTimeToUnixTime(&lft);
}
else if (tz == UTC_TIME_HANDLE)
return ts;
if (tz->offset == INT_MIN)
CalcTsOffset(tz);
return ts + tz->offset;
}
typedef struct
{
UINT addStr, getSel, setSel, getData, setData;
} ListMessages;
static const ListMessages lbMessages =
{ LB_ADDSTRING, LB_GETCURSEL, LB_SETCURSEL, LB_GETITEMDATA, LB_SETITEMDATA };
static const ListMessages cbMessages =
{ CB_ADDSTRING, CB_GETCURSEL, CB_SETCURSEL, CB_GETITEMDATA, CB_SETITEMDATA };
static const ListMessages *GetListMessages(HWND hWnd, DWORD dwFlags)
{
if ( !(dwFlags & (TZF_PLF_CB | TZF_PLF_LB)))
{
TCHAR tszClassName[128];
GetClassName(hWnd, tszClassName, SIZEOF(tszClassName));
if ( !_tcsicmp(tszClassName, _T("COMBOBOX")))
dwFlags |= TZF_PLF_CB;
else if ( !_tcsicmp(tszClassName, _T("LISTBOX")))
dwFlags |= TZF_PLF_LB;
}
if (dwFlags & TZF_PLF_CB)
return & cbMessages;
else if (dwFlags & TZF_PLF_LB)
return & lbMessages;
else
return NULL;
}
static int timeapiSelectListItem(HANDLE hContact, HWND hWnd, DWORD dwFlags)
{
if (hWnd == NULL) // nothing to do
return -1;
const ListMessages *lstMsg = GetListMessages(hWnd, dwFlags);
if (lstMsg == NULL) return -1;
int iSelection = 0;
if (hContact)
{
DBVARIANT dbv;
if ( !db_get_ts(hContact, "UserInfo", "TzName", &dbv))
{
unsigned hash = mir_hashstrT(dbv.ptszVal);
for (int i=0; i < g_timezonesBias.getCount(); i++)
{
if (hash == g_timezonesBias[i]->hash)
{
iSelection = i + 1;
break;
}
}
db_free(&dbv);
}
}
SendMessage(hWnd, lstMsg->setSel, iSelection, 0);
return iSelection;
}
static int timeapiPrepareList(HANDLE hContact, HWND hWnd, DWORD dwFlags)
{
if (hWnd == NULL) // nothing to do
return 0;
const ListMessages *lstMsg = GetListMessages(hWnd, dwFlags);
if (lstMsg == NULL) return 0;
SendMessage(hWnd, lstMsg->addStr, 0, (LPARAM)TranslateT("<unspecified>"));
for (int i=0; i < g_timezonesBias.getCount(); i++)
{
MIM_TIMEZONE *tz = g_timezonesBias[i];
SendMessage(hWnd, lstMsg->addStr, 0, (LPARAM)tz->szDisplay);
SendMessage(hWnd, lstMsg->setData, i + 1, (LPARAM)tz);
}
return timeapiSelectListItem(hContact, hWnd, dwFlags);
}
static void timeapiStoreListResult(HANDLE hContact, HWND hWnd, DWORD dwFlags)
{
const ListMessages *lstMsg = GetListMessages(hWnd, dwFlags);
if (lstMsg == NULL) return;
LRESULT offset = SendMessage(hWnd, lstMsg->getSel, 0, 0);
if (offset > 0)
{
MIM_TIMEZONE *tz = (MIM_TIMEZONE*)SendMessage(hWnd, lstMsg->getData, offset, 0);
if ((INT_PTR)tz != CB_ERR && tz != NULL)
timeapiSetInfoByContact(hContact, tz);
}
else
timeapiSetInfoByContact(hContact, NULL);
}
static INT_PTR GetTimeApi(WPARAM, LPARAM lParam)
{
TIME_API* tmi = (TIME_API*)lParam;
if (tmi == NULL)
return FALSE;
if (tmi->cbSize != sizeof(TIME_API))
return FALSE;
tmi->createByName = timeapiGetInfoByName;
tmi->createByContact = timeapiGetInfoByContact;
tmi->storeByContact = timeapiSetInfoByContact;
tmi->printDateTime = timeapiPrintDateTime;
tmi->printTimeStamp = timeapiPrintTimeStamp;
tmi->prepareList = timeapiPrepareList;
tmi->selectListItem = timeapiSelectListItem;
tmi->storeListResults = timeapiStoreListResult;
tmi->getTimeZoneTime = timeapiGetTimeZoneTime;
tmi->timeStampToTimeZoneTimeStamp = timeapiTimeStampToTimeZoneTimeStamp;
tmi->getTzi = timeapiGetTzi;
tmi->getTzName = timeapiGetTzName;
tmi->getTzDescription = timeapiGetTzDescription;
return TRUE;
}
static INT_PTR TimestampToLocal(WPARAM wParam, LPARAM)
{
return timeapiTimeStampToTimeZoneTimeStamp(NULL, (mir_time)wParam);
}
static INT_PTR TimestampToStringT(WPARAM wParam, LPARAM lParam)
{
DBTIMETOSTRINGT *tts = (DBTIMETOSTRINGT*)lParam;
if (tts == NULL) return 0;
timeapiPrintTimeStamp(NULL, (mir_time)wParam, tts->szFormat, tts->szDest, tts->cbDest, 0);
return 0;
}
static INT_PTR TimestampToStringA(WPARAM wParam, LPARAM lParam)
{
DBTIMETOSTRING *tts = (DBTIMETOSTRING*)lParam;
if (tts == NULL) return 0;
TCHAR *szDest = (TCHAR*)alloca(tts->cbDest);
timeapiPrintTimeStamp(NULL, (mir_time)wParam, StrConvT(tts->szFormat), szDest, tts->cbDest, 0);
WideCharToMultiByte(CP_ACP, 0, szDest, -1, tts->szDest, tts->cbDest, NULL, NULL);
return 0;
}
void GetLocalizedString(HKEY hSubKey, const TCHAR *szName, wchar_t *szBuf, DWORD cbLen)
{
DWORD dwLength = cbLen * sizeof(wchar_t);
RegQueryValueEx(hSubKey, szName, NULL, NULL, (unsigned char *)szBuf, &dwLength);
szBuf[min(dwLength / sizeof(TCHAR), cbLen - 1)] = 0;
}
extern "C" __declspec(dllexport) void RecalculateTime(void)
{
GetTimeZoneInformation(&myInfo.myTZ.tzi);
myInfo.timestamp = time(NULL);
myInfo.myTZ.offset = INT_MIN;
bool found = false;
DYNAMIC_TIME_ZONE_INFORMATION dtzi;
if (pfnGetDynamicTimeZoneInformation && pfnGetDynamicTimeZoneInformation(&dtzi) != TIME_ZONE_ID_INVALID)
{
TCHAR *myTzKey = mir_u2t(dtzi.TimeZoneKeyName);
_tcscpy(myInfo.myTZ.tszName, myTzKey);
mir_free(myTzKey);
found = true;
}
for (int i=0; i < g_timezones.getCount(); i++)
{
MIM_TIMEZONE &tz = g_timezones[i];
if (tz.offset != INT_MIN) tz.offset = INT_MIN;
if ( !found)
{
if ( !wcscmp(tz.tzi.StandardName, myInfo.myTZ.tzi.StandardName) ||
!wcscmp(tz.tzi.DaylightName, myInfo.myTZ.tzi.DaylightName))
{
_tcscpy(myInfo.myTZ.tszName, tz.tszName);
found = true;
}
}
}
}
void InitTimeZones(void)
{
REG_TZI_FORMAT tzi;
HKEY hKey;
const TCHAR *tszKey = _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones");
/*
* use GetDynamicTimeZoneInformation() on Vista+ - this will return a structure with
* the registry key name, so finding our own time zone later will be MUCH easier for
* localized systems or systems with a MUI pack installed
*/
if (IsWinVerVistaPlus())
pfnGetDynamicTimeZoneInformation = (pfnGetDynamicTimeZoneInformation_t)GetProcAddress(GetModuleHandle(_T("kernel32")), "GetDynamicTimeZoneInformation");
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, tszKey, 0, KEY_ENUMERATE_SUB_KEYS, &hKey))
{
DWORD dwIndex = 0;
HKEY hSubKey;
TCHAR tszName[MIM_TZ_NAMELEN];
DWORD dwSize = SIZEOF(tszName);
while (ERROR_NO_MORE_ITEMS != RegEnumKeyEx(hKey, dwIndex++, tszName, &dwSize, NULL, NULL, 0, NULL))
{
if (ERROR_SUCCESS == RegOpenKeyEx(hKey, tszName, 0, KEY_QUERY_VALUE, &hSubKey))
{
dwSize = sizeof(tszName);
DWORD dwLength = sizeof(tzi);
if (ERROR_SUCCESS != RegQueryValueEx(hSubKey, _T("TZI"), NULL, NULL, (unsigned char *)&tzi, &dwLength))
continue;
MIM_TIMEZONE *tz = new MIM_TIMEZONE;
tz->tzi.Bias = tzi.Bias;
tz->tzi.StandardDate = tzi.StandardDate;
tz->tzi.StandardBias = tzi.StandardBias;
tz->tzi.DaylightDate = tzi.DaylightDate;
tz->tzi.DaylightBias = tzi.DaylightBias;
_tcscpy(tz->tszName, tszName);
tz->hash = mir_hashstrT(tszName);
tz->offset = INT_MIN;
GetLocalizedString(hSubKey, _T("Display"), tz->szDisplay, SIZEOF(tz->szDisplay));
GetLocalizedString(hSubKey, _T("Std"), tz->tzi.StandardName, SIZEOF(tz->tzi.StandardName));
GetLocalizedString(hSubKey, _T("Dlt"), tz->tzi.DaylightName, SIZEOF(tz->tzi.DaylightName));
g_timezones.insert(tz);
g_timezonesBias.insert(tz);
RegCloseKey(hSubKey);
}
dwSize = SIZEOF(tszName);
}
RegCloseKey(hKey);
}
RecalculateTime();
CreateServiceFunction(MS_SYSTEM_GET_TMI, GetTimeApi);
CreateServiceFunction(MS_DB_TIME_TIMESTAMPTOLOCAL, TimestampToLocal);
CreateServiceFunction(MS_DB_TIME_TIMESTAMPTOSTRINGT, TimestampToStringT);
CreateServiceFunction(MS_DB_TIME_TIMESTAMPTOSTRING, TimestampToStringA);
tmi.cbSize = sizeof(tmi);
GetTimeApi(0, (LPARAM)&tmi);
}
|