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
|
/**
* WinPR: Windows Portable Runtime
* Time Zone
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <winpr/config.h>
#include <winpr/environment.h>
#include <winpr/wtypes.h>
#include <winpr/timezone.h>
#include <winpr/crt.h>
#include <winpr/assert.h>
#include <winpr/file.h>
#include "../log.h"
#define TAG WINPR_TAG("timezone")
#ifndef _WIN32
#include <time.h>
#include <unistd.h>
#include "TimeZones.h"
#include "WindowsZones.h"
static UINT64 winpr_windows_gmtime(void)
{
time_t unix_time = 0;
UINT64 windows_time = 0;
time(&unix_time);
if (unix_time < 0)
return 0;
windows_time = (UINT64)unix_time;
windows_time *= 10000000;
windows_time += 621355968000000000ULL;
return windows_time;
}
static char* winpr_read_unix_timezone_identifier_from_file(FILE* fp)
{
const INT CHUNK_SIZE = 32;
INT64 rc = 0;
INT64 read = 0;
INT64 length = CHUNK_SIZE;
char* tzid = NULL;
tzid = (char*)malloc(length);
if (!tzid)
return NULL;
do
{
rc = fread(tzid + read, 1, length - read - 1, fp);
if (rc > 0)
read += rc;
if (read < (length - 1))
break;
length += CHUNK_SIZE;
char* tmp = (char*)realloc(tzid, length);
if (!tmp)
{
free(tzid);
return NULL;
}
tzid = tmp;
} while (rc > 0);
if (ferror(fp))
{
free(tzid);
return NULL;
}
tzid[read] = '\0';
if (read > 0)
{
if (tzid[read - 1] == '\n')
tzid[read - 1] = '\0';
}
return tzid;
}
static char* winpr_get_timezone_from_link(const char* links[], size_t count)
{
const char* _links[] = { "/etc/localtime", "/etc/TZ" };
if (links == NULL)
{
links = _links;
count = ARRAYSIZE(_links);
}
/*
* On linux distros such as Redhat or Archlinux, a symlink at /etc/localtime
* will point to /usr/share/zoneinfo/region/place where region/place could be
* America/Montreal for example.
* Some distributions do have to symlink at /etc/TZ.
*/
for (size_t x = 0; x < count; x++)
{
char* tzid = NULL;
const char* link = links[x];
char* buf = realpath(link, NULL);
if (buf)
{
size_t sep = 0;
size_t alloc = 0;
size_t pos = 0;
size_t len = pos = strlen(buf);
/* find the position of the 2nd to last "/" */
for (size_t i = 1; i <= len; i++)
{
const size_t curpos = len - i;
const char cur = buf[curpos];
if (cur == '/')
sep++;
if (sep >= 2)
{
alloc = i;
pos = len - i + 1;
break;
}
}
if ((len == 0) || (sep != 2))
goto end;
tzid = (char*)calloc(alloc + 1, sizeof(char));
if (!tzid)
goto end;
strncpy(tzid, &buf[pos], alloc);
WLog_DBG(TAG, "tzid: %s", tzid);
goto end;
}
end:
free(buf);
if (tzid)
return tzid;
}
return NULL;
}
#if defined(ANDROID)
#include "../utils/android.h"
static char* winpr_get_android_timezone_identifier(void)
{
char* tzid = NULL;
JNIEnv* jniEnv;
/* Preferred: Try to get identifier from java TimeZone class */
if (jniVm && ((*jniVm)->GetEnv(jniVm, (void**)&jniEnv, JNI_VERSION_1_6) == JNI_OK))
{
const char* raw;
jclass jObjClass;
jobject jObj;
jmethodID jDefaultTimezone;
jmethodID jTimezoneIdentifier;
jstring tzJId;
jboolean attached = (*jniVm)->AttachCurrentThread(jniVm, &jniEnv, NULL);
jObjClass = (*jniEnv)->FindClass(jniEnv, "java/util/TimeZone");
if (!jObjClass)
goto fail;
jDefaultTimezone =
(*jniEnv)->GetStaticMethodID(jniEnv, jObjClass, "getDefault", "()Ljava/util/TimeZone;");
if (!jDefaultTimezone)
goto fail;
jObj = (*jniEnv)->CallStaticObjectMethod(jniEnv, jObjClass, jDefaultTimezone);
if (!jObj)
goto fail;
jTimezoneIdentifier =
(*jniEnv)->GetMethodID(jniEnv, jObjClass, "getID", "()Ljava/lang/String;");
if (!jTimezoneIdentifier)
goto fail;
tzJId = (*jniEnv)->CallObjectMethod(jniEnv, jObj, jTimezoneIdentifier);
if (!tzJId)
goto fail;
raw = (*jniEnv)->GetStringUTFChars(jniEnv, tzJId, 0);
if (raw)
tzid = _strdup(raw);
(*jniEnv)->ReleaseStringUTFChars(jniEnv, tzJId, raw);
fail:
if (attached)
(*jniVm)->DetachCurrentThread(jniVm);
}
/* Fall back to property, might not be available. */
if (!tzid)
{
FILE* fp = popen("getprop persist.sys.timezone", "r");
if (fp)
{
tzid = winpr_read_unix_timezone_identifier_from_file(fp);
pclose(fp);
}
}
return tzid;
}
#endif
static char* winpr_get_unix_timezone_identifier_from_file(void)
{
#if defined(ANDROID)
return winpr_get_android_timezone_identifier();
#else
FILE* fp = NULL;
char* tzid = NULL;
#if !defined(WINPR_TIMEZONE_FILE)
#error \
"Please define WINPR_TIMEZONE_FILE with the path to your timezone file (e.g. /etc/timezone or similar)"
#else
fp = winpr_fopen(WINPR_TIMEZONE_FILE, "r");
#endif
if (NULL == fp)
return NULL;
tzid = winpr_read_unix_timezone_identifier_from_file(fp);
fclose(fp);
if (tzid != NULL)
WLog_DBG(TAG, "tzid: %s", tzid);
return tzid;
#endif
}
static BOOL winpr_match_unix_timezone_identifier_with_list(const char* tzid, const char* list)
{
char* p = NULL;
char* list_copy = NULL;
char* context = NULL;
list_copy = _strdup(list);
if (!list_copy)
return FALSE;
p = strtok_s(list_copy, " ", &context);
while (p != NULL)
{
if (strcmp(p, tzid) == 0)
{
free(list_copy);
return TRUE;
}
p = strtok_s(NULL, " ", &context);
}
free(list_copy);
return FALSE;
}
static TIME_ZONE_ENTRY* winpr_detect_windows_time_zone(void)
{
char* tzid = NULL;
char* ntzid = NULL;
LPCSTR tz = "TZ";
DWORD nSize = GetEnvironmentVariableA(tz, NULL, 0);
if (nSize)
{
tzid = (char*)malloc(nSize);
if (!GetEnvironmentVariableA(tz, tzid, nSize))
{
free(tzid);
tzid = NULL;
}
else if (tzid[0] == ':')
{
/* Remove leading colon, see tzset(3) */
memmove(tzid, tzid + 1, nSize - sizeof(char));
}
}
if (tzid == NULL)
tzid = winpr_get_unix_timezone_identifier_from_file();
if (tzid == NULL)
{
tzid = winpr_get_timezone_from_link(NULL, 0);
}
else
{
const char* zipath = "/usr/share/zoneinfo/";
char buf[1024] = { 0 };
const char* links[] = { buf };
if (tzid[0] == '/')
{
/* Full path given in TZ */
links[0] = tzid;
}
else
snprintf(buf, ARRAYSIZE(buf), "%s%s", zipath, tzid);
ntzid = winpr_get_timezone_from_link(links, 1);
if (ntzid != NULL)
{
free(tzid);
tzid = ntzid;
}
}
if (tzid == NULL)
return NULL;
WLog_INFO(TAG, "tzid: %s", tzid);
for (size_t i = 0; i < TimeZoneTableNrElements; i++)
{
const TIME_ZONE_ENTRY* tze = &TimeZoneTable[i];
for (size_t j = 0; j < WindowsTimeZoneIdTableNrElements; j++)
{
const WINDOWS_TZID_ENTRY* wzid = &WindowsTimeZoneIdTable[j];
if (strcmp(tze->Id, wzid->windows) != 0)
continue;
if (winpr_match_unix_timezone_identifier_with_list(tzid, wzid->tzid))
{
TIME_ZONE_ENTRY* ctimezone = (TIME_ZONE_ENTRY*)malloc(sizeof(TIME_ZONE_ENTRY));
free(tzid);
if (!ctimezone)
return NULL;
*ctimezone = TimeZoneTable[i];
return ctimezone;
}
}
}
WLog_ERR(TAG, "Unable to find a match for unix timezone: %s", tzid);
free(tzid);
return NULL;
}
static const TIME_ZONE_RULE_ENTRY*
winpr_get_current_time_zone_rule(const TIME_ZONE_RULE_ENTRY* rules, UINT32 count)
{
UINT64 windows_time = 0;
windows_time = winpr_windows_gmtime();
for (UINT32 i = 0; i < count; i++)
{
if ((rules[i].TicksStart >= windows_time) && (windows_time >= rules[i].TicksEnd))
{
/*WLog_ERR(TAG, "Got rule %" PRIu32 " from table at %p with count %"PRIu32"", i,
* (void*) rules, count);*/
return &rules[i];
}
}
WLog_ERR(TAG, "Unable to get current timezone rule");
return NULL;
}
DWORD GetTimeZoneInformation(LPTIME_ZONE_INFORMATION lpTimeZoneInformation)
{
time_t t = 0;
struct tm tres = { 0 };
TIME_ZONE_ENTRY* dtz = NULL;
const TIME_ZONE_INFORMATION empty = { 0 };
LPTIME_ZONE_INFORMATION tz = lpTimeZoneInformation;
WINPR_ASSERT(tz);
*tz = empty;
t = time(NULL);
struct tm* local_time = localtime_r(&t, &tres);
if (!local_time)
goto out_error;
#ifdef WINPR_HAVE_TM_GMTOFF
{
long bias = -(local_time->tm_gmtoff / 60L);
if (bias > INT32_MAX)
bias = INT32_MAX;
tz->Bias = (LONG)bias;
}
#endif
dtz = winpr_detect_windows_time_zone();
if (dtz != NULL)
{
WLog_DBG(TAG, "tz: Bias=%" PRId32 " sn='%s' dln='%s'", dtz->Bias, dtz->StandardName,
dtz->DaylightName);
tz->Bias = dtz->Bias;
if (ConvertUtf8ToWChar(dtz->StandardName, tz->StandardName, ARRAYSIZE(tz->StandardName)) <
0)
{
WLog_ERR(TAG, "StandardName conversion failed - using default");
goto out_error;
}
if (ConvertUtf8ToWChar(dtz->DaylightName, tz->DaylightName, ARRAYSIZE(tz->DaylightName)) <
0)
{
WLog_ERR(TAG, "DaylightName conversion failed - using default");
goto out_error;
}
if ((dtz->SupportsDST) && (dtz->RuleTableCount > 0))
{
const TIME_ZONE_RULE_ENTRY* rule =
winpr_get_current_time_zone_rule(dtz->RuleTable, dtz->RuleTableCount);
if (rule != NULL)
{
tz->DaylightBias = -rule->DaylightDelta;
tz->StandardDate = rule->StandardDate;
tz->DaylightDate = rule->DaylightDate;
}
}
free(dtz);
/* 1 ... TIME_ZONE_ID_STANDARD
* 2 ... TIME_ZONE_ID_DAYLIGHT */
return local_time->tm_isdst ? 2 : 1;
}
/* could not detect timezone, use computed bias from tm_gmtoff */
WLog_DBG(TAG, "tz not found, using computed bias %" PRId32 ".", tz->Bias);
out_error:
free(dtz);
if (ConvertUtf8ToWChar("Client Local Time", tz->StandardName, ARRAYSIZE(tz->StandardName)) <= 0)
WLog_WARN(TAG, "Failed to set default timezone name");
memcpy(tz->DaylightName, tz->StandardName, sizeof(tz->DaylightName));
return 0; /* TIME_ZONE_ID_UNKNOWN */
}
BOOL SetTimeZoneInformation(const TIME_ZONE_INFORMATION* lpTimeZoneInformation)
{
WINPR_UNUSED(lpTimeZoneInformation);
return FALSE;
}
BOOL SystemTimeToFileTime(const SYSTEMTIME* lpSystemTime, LPFILETIME lpFileTime)
{
WINPR_UNUSED(lpSystemTime);
WINPR_UNUSED(lpFileTime);
return FALSE;
}
BOOL FileTimeToSystemTime(const FILETIME* lpFileTime, LPSYSTEMTIME lpSystemTime)
{
WINPR_UNUSED(lpFileTime);
WINPR_UNUSED(lpSystemTime);
return FALSE;
}
BOOL SystemTimeToTzSpecificLocalTime(LPTIME_ZONE_INFORMATION lpTimeZone,
LPSYSTEMTIME lpUniversalTime, LPSYSTEMTIME lpLocalTime)
{
WINPR_UNUSED(lpTimeZone);
WINPR_UNUSED(lpUniversalTime);
WINPR_UNUSED(lpLocalTime);
return FALSE;
}
BOOL TzSpecificLocalTimeToSystemTime(LPTIME_ZONE_INFORMATION lpTimeZoneInformation,
LPSYSTEMTIME lpLocalTime, LPSYSTEMTIME lpUniversalTime)
{
WINPR_UNUSED(lpTimeZoneInformation);
WINPR_UNUSED(lpLocalTime);
WINPR_UNUSED(lpUniversalTime);
return FALSE;
}
#endif
/*
* GetDynamicTimeZoneInformation is provided by the SDK if _WIN32_WINNT >= 0x0600 in SDKs above 7.1A
* and incorrectly if _WIN32_WINNT >= 0x0501 in older SDKs
*/
#if !defined(_WIN32) || \
(defined(_WIN32) && (defined(NTDDI_WIN8) && _WIN32_WINNT < 0x0600 || \
!defined(NTDDI_WIN8) && _WIN32_WINNT < 0x0501)) /* Windows Vista */
DWORD GetDynamicTimeZoneInformation(PDYNAMIC_TIME_ZONE_INFORMATION pTimeZoneInformation)
{
WINPR_UNUSED(pTimeZoneInformation);
return 0;
}
BOOL SetDynamicTimeZoneInformation(const DYNAMIC_TIME_ZONE_INFORMATION* lpTimeZoneInformation)
{
WINPR_UNUSED(lpTimeZoneInformation);
return FALSE;
}
BOOL GetTimeZoneInformationForYear(USHORT wYear, PDYNAMIC_TIME_ZONE_INFORMATION pdtzi,
LPTIME_ZONE_INFORMATION ptzi)
{
WINPR_UNUSED(wYear);
WINPR_UNUSED(pdtzi);
WINPR_UNUSED(ptzi);
return FALSE;
}
#endif
#if !defined(_WIN32) || (defined(_WIN32) && (_WIN32_WINNT < 0x0601)) /* Windows 7 */
BOOL SystemTimeToTzSpecificLocalTimeEx(const DYNAMIC_TIME_ZONE_INFORMATION* lpTimeZoneInformation,
const SYSTEMTIME* lpUniversalTime, LPSYSTEMTIME lpLocalTime)
{
WINPR_UNUSED(lpTimeZoneInformation);
WINPR_UNUSED(lpUniversalTime);
WINPR_UNUSED(lpLocalTime);
return FALSE;
}
BOOL TzSpecificLocalTimeToSystemTimeEx(const DYNAMIC_TIME_ZONE_INFORMATION* lpTimeZoneInformation,
const SYSTEMTIME* lpLocalTime, LPSYSTEMTIME lpUniversalTime)
{
WINPR_UNUSED(lpTimeZoneInformation);
WINPR_UNUSED(lpLocalTime);
WINPR_UNUSED(lpUniversalTime);
return FALSE;
}
#endif
#if !defined(_WIN32) || (defined(_WIN32) && (_WIN32_WINNT < 0x0602)) /* Windows 8 */
DWORD EnumDynamicTimeZoneInformation(const DWORD dwIndex,
PDYNAMIC_TIME_ZONE_INFORMATION lpTimeZoneInformation)
{
WINPR_UNUSED(dwIndex);
WINPR_UNUSED(lpTimeZoneInformation);
return 0;
}
DWORD GetDynamicTimeZoneInformationEffectiveYears(
const PDYNAMIC_TIME_ZONE_INFORMATION lpTimeZoneInformation, LPDWORD FirstYear, LPDWORD LastYear)
{
WINPR_UNUSED(lpTimeZoneInformation);
WINPR_UNUSED(FirstYear);
WINPR_UNUSED(LastYear);
return 0;
}
#endif
|