FreeRDP
Loading...
Searching...
No Matches
winpr/libwinpr/timezone/timezone.c
1
22#include <winpr/config.h>
23
24#include <winpr/environment.h>
25#include <winpr/wtypes.h>
26#include <winpr/timezone.h>
27#include <winpr/crt.h>
28#include <winpr/assert.h>
29#include <winpr/file.h>
30#include "../log.h"
31#include "timezone.h"
32
33#define TAG WINPR_TAG("timezone")
34
35#include "TimeZoneNameMap.h"
36#include "TimeZoneIanaAbbrevMap.h"
37
38#ifndef _WIN32
39
40#include <time.h>
41#include <unistd.h>
42
43#endif
44
45#if !defined(_WIN32)
46static char* winpr_read_unix_timezone_identifier_from_file(FILE* fp)
47{
48 const INT CHUNK_SIZE = 32;
49 size_t rc = 0;
50 size_t read = 0;
51 size_t length = CHUNK_SIZE;
52
53 char* tzid = malloc(length);
54 if (!tzid)
55 return NULL;
56
57 do
58 {
59 rc = fread(tzid + read, 1, length - read - 1UL, fp);
60 if (rc > 0)
61 read += rc;
62
63 if (read < (length - 1UL))
64 break;
65
66 if (read > length - 1UL)
67 {
68 free(tzid);
69 return NULL;
70 }
71
72 length += CHUNK_SIZE;
73 char* tmp = (char*)realloc(tzid, length);
74 if (!tmp)
75 {
76 free(tzid);
77 return NULL;
78 }
79
80 tzid = tmp;
81 } while (rc > 0);
82
83 if (ferror(fp))
84 {
85 free(tzid);
86 return NULL;
87 }
88
89 tzid[read] = '\0';
90 if (read > 0)
91 {
92 if (tzid[read - 1] == '\n')
93 tzid[read - 1] = '\0';
94 }
95
96 return tzid;
97}
98
99static char* winpr_get_timezone_from_link(const char* links[], size_t count)
100{
101 const char* _links[] = { "/etc/localtime", "/etc/TZ" };
102
103 if (links == NULL)
104 {
105 links = _links;
106 count = ARRAYSIZE(_links);
107 }
108
109 /*
110 * On linux distros such as Redhat or Archlinux, a symlink at /etc/localtime
111 * will point to /usr/share/zoneinfo/region/place where region/place could be
112 * America/Montreal for example.
113 * Some distributions do have to symlink at /etc/TZ.
114 */
115
116 for (size_t x = 0; x < count; x++)
117 {
118 char* tzid = NULL;
119 const char* link = links[x];
120 char* buf = realpath(link, NULL);
121
122 if (buf)
123 {
124 size_t sep = 0;
125 size_t alloc = 0;
126 size_t pos = 0;
127 size_t len = pos = strlen(buf);
128
129 /* find the position of the 2nd to last "/" */
130 for (size_t i = 1; i <= len; i++)
131 {
132 const size_t curpos = len - i;
133 const char cur = buf[curpos];
134
135 if (cur == '/')
136 sep++;
137 if (sep >= 2)
138 {
139 alloc = i;
140 pos = len - i + 1;
141 break;
142 }
143 }
144
145 if ((len == 0) || (sep != 2))
146 goto end;
147
148 tzid = (char*)calloc(alloc + 1, sizeof(char));
149
150 if (!tzid)
151 goto end;
152
153 strncpy(tzid, &buf[pos], alloc);
154 WLog_DBG(TAG, "tzid: %s", tzid);
155 goto end;
156 }
157
158 end:
159 free(buf);
160 if (tzid)
161 return tzid;
162 }
163
164 return NULL;
165}
166
167#if defined(ANDROID)
168#include "../utils/android.h"
169
170static char* winpr_get_android_timezone_identifier(void)
171{
172 char* tzid = NULL;
173 JNIEnv* jniEnv;
174
175 /* Preferred: Try to get identifier from java TimeZone class */
176 if (jniVm && ((*jniVm)->GetEnv(jniVm, (void**)&jniEnv, JNI_VERSION_1_6) == JNI_OK))
177 {
178 const char* raw;
179 jclass jObjClass;
180 jobject jObj;
181 jmethodID jDefaultTimezone;
182 jmethodID jTimezoneIdentifier;
183 jstring tzJId;
184 jboolean attached = (*jniVm)->AttachCurrentThread(jniVm, &jniEnv, NULL);
185 jObjClass = (*jniEnv)->FindClass(jniEnv, "java/util/TimeZone");
186
187 if (!jObjClass)
188 goto fail;
189
190 jDefaultTimezone =
191 (*jniEnv)->GetStaticMethodID(jniEnv, jObjClass, "getDefault", "()Ljava/util/TimeZone;");
192
193 if (!jDefaultTimezone)
194 goto fail;
195
196 jObj = (*jniEnv)->CallStaticObjectMethod(jniEnv, jObjClass, jDefaultTimezone);
197
198 if (!jObj)
199 goto fail;
200
201 jTimezoneIdentifier =
202 (*jniEnv)->GetMethodID(jniEnv, jObjClass, "getID", "()Ljava/lang/String;");
203
204 if (!jTimezoneIdentifier)
205 goto fail;
206
207 tzJId = (*jniEnv)->CallObjectMethod(jniEnv, jObj, jTimezoneIdentifier);
208
209 if (!tzJId)
210 goto fail;
211
212 raw = (*jniEnv)->GetStringUTFChars(jniEnv, tzJId, 0);
213
214 if (raw)
215 tzid = _strdup(raw);
216
217 (*jniEnv)->ReleaseStringUTFChars(jniEnv, tzJId, raw);
218 fail:
219
220 if (attached)
221 (*jniVm)->DetachCurrentThread(jniVm);
222 }
223
224 /* Fall back to property, might not be available. */
225 if (!tzid)
226 {
227 FILE* fp = popen("getprop persist.sys.timezone", "r");
228
229 if (fp)
230 {
231 tzid = winpr_read_unix_timezone_identifier_from_file(fp);
232 pclose(fp);
233 }
234 }
235
236 return tzid;
237}
238#endif
239
240static char* winpr_get_unix_timezone_identifier_from_file(void)
241{
242#if defined(ANDROID)
243 return winpr_get_android_timezone_identifier();
244#else
245 FILE* fp = NULL;
246 char* tzid = NULL;
247#if !defined(WINPR_TIMEZONE_FILE)
248#error \
249 "Please define WINPR_TIMEZONE_FILE with the path to your timezone file (e.g. /etc/timezone or similar)"
250#else
251 fp = winpr_fopen(WINPR_TIMEZONE_FILE, "r");
252#endif
253
254 if (NULL == fp)
255 return NULL;
256
257 tzid = winpr_read_unix_timezone_identifier_from_file(fp);
258 (void)fclose(fp);
259 if (tzid != NULL)
260 WLog_DBG(TAG, "tzid: %s", tzid);
261 return tzid;
262#endif
263}
264
265static char* winpr_time_zone_from_env(void)
266{
267 LPCSTR tz = "TZ";
268 char* tzid = NULL;
269
270 DWORD nSize = GetEnvironmentVariableA(tz, NULL, 0);
271 if (nSize > 0)
272 {
273 tzid = (char*)calloc(nSize, sizeof(char));
274 if (!tzid)
275 goto fail;
276 if (!GetEnvironmentVariableA(tz, tzid, nSize))
277 goto fail;
278 else if (tzid[0] == ':')
279 {
280 /* Remove leading colon, see tzset(3) */
281 memmove(tzid, tzid + 1, nSize - sizeof(char));
282 }
283 }
284
285 return tzid;
286
287fail:
288 free(tzid);
289 return NULL;
290}
291
292static char* winpr_translate_time_zone(const char* tzid)
293{
294 const char* zipath = "/usr/share/zoneinfo/";
295 char* buf = NULL;
296 const char* links[] = { buf };
297
298 if (!tzid)
299 return NULL;
300
301 if (tzid[0] == '/')
302 {
303 /* Full path given in TZ */
304 links[0] = tzid;
305 }
306 else
307 {
308 size_t bsize = 0;
309 winpr_asprintf(&buf, &bsize, "%s%s", zipath, tzid);
310 links[0] = buf;
311 }
312
313 char* ntzid = winpr_get_timezone_from_link(links, 1);
314 free(buf);
315 return ntzid;
316}
317
318static char* winpr_guess_time_zone(void)
319{
320 char* tzid = winpr_time_zone_from_env();
321 if (tzid)
322 goto end;
323 tzid = winpr_get_unix_timezone_identifier_from_file();
324 if (tzid)
325 goto end;
326 tzid = winpr_get_timezone_from_link(NULL, 0);
327 if (tzid)
328 goto end;
329
330end:
331{
332 char* ntzid = winpr_translate_time_zone(tzid);
333 if (ntzid)
334 {
335 free(tzid);
336 return ntzid;
337 }
338 return tzid;
339}
340}
341
342static SYSTEMTIME tm2systemtime(const struct tm* t)
343{
344 SYSTEMTIME st = { 0 };
345
346 if (t)
347 {
348 st.wYear = (WORD)(1900 + t->tm_year);
349 st.wMonth = (WORD)t->tm_mon + 1;
350 st.wDay = (WORD)t->tm_mday;
351 st.wDayOfWeek = (WORD)t->tm_wday;
352 st.wHour = (WORD)t->tm_hour;
353 st.wMinute = (WORD)t->tm_min;
354 st.wSecond = (WORD)t->tm_sec;
355 st.wMilliseconds = 0;
356 }
357 return st;
358}
359
360static struct tm systemtime2tm(const SYSTEMTIME* st)
361{
362 struct tm t = { 0 };
363 if (st)
364 {
365 if (st->wYear >= 1900)
366 t.tm_year = st->wYear - 1900;
367 if (st->wMonth > 0)
368 t.tm_mon = st->wMonth - 1;
369 t.tm_mday = st->wDay;
370 t.tm_wday = st->wDayOfWeek;
371 t.tm_hour = st->wHour;
372 t.tm_min = st->wMinute;
373 t.tm_sec = st->wSecond;
374 }
375 return t;
376}
377
378static LONG get_gmtoff_min(const struct tm* t)
379{
380 WINPR_ASSERT(t);
381 return -(LONG)(t->tm_gmtoff / 60l);
382}
383
384static struct tm next_day(const struct tm* start)
385{
386 struct tm cur = *start;
387 cur.tm_hour = 0;
388 cur.tm_min = 0;
389 cur.tm_sec = 0;
390 cur.tm_isdst = -1;
391 cur.tm_mday++;
392 const time_t t = mktime(&cur);
393 (void)localtime_r(&t, &cur);
394 return cur;
395}
396
397static struct tm adjust_time(const struct tm* start, int hour, int minute)
398{
399 struct tm cur = *start;
400 cur.tm_hour = hour;
401 cur.tm_min = minute;
402 cur.tm_sec = 0;
403 cur.tm_isdst = -1;
404 const time_t t = mktime(&cur);
405 (void)localtime_r(&t, &cur);
406 return cur;
407}
408
409/* [MS-RDPBCGR] 2.2.1.11.1.1.1.1.1 System Time (TS_SYSTEMTIME) */
410static WORD get_transition_weekday_occurrence(const SYSTEMTIME* st)
411{
412 WORD count = 0;
413 struct tm start = systemtime2tm(st);
414
415 WORD last = 0;
416 struct tm next = start;
417 next.tm_mday = 1;
418 next.tm_isdst = -1;
419 do
420 {
421
422 const time_t t = mktime(&next);
423 next.tm_mday++;
424
425 struct tm cur = { 0 };
426 (void)localtime_r(&t, &cur);
427
428 if (cur.tm_mon + 1 != st->wMonth)
429 break;
430
431 if (cur.tm_wday == st->wDayOfWeek)
432 {
433 if (cur.tm_mday <= st->wDay)
434 count++;
435 last++;
436 }
437
438 } while (TRUE);
439
440 if (count == last)
441 count = 5;
442
443 return count;
444}
445
446static SYSTEMTIME tm2transitiontime(const struct tm* cur)
447{
448 SYSTEMTIME t = tm2systemtime(cur);
449 if (cur)
450 {
451 t.wDay = get_transition_weekday_occurrence(&t);
452 t.wYear = 0;
453 }
454
455 return t;
456}
457
458static SYSTEMTIME get_transition_time(const struct tm* start, BOOL toDst)
459{
460 BOOL toggled = FALSE;
461 struct tm first = adjust_time(start, 0, 0);
462 for (int hour = 0; hour < 24; hour++)
463 {
464 struct tm cur = adjust_time(start, hour, 0);
465 if (cur.tm_isdst != first.tm_isdst)
466 toggled = TRUE;
467
468 if (toggled)
469 {
470 if (toDst && (cur.tm_isdst > 0))
471 return tm2transitiontime(&cur);
472 else if (!toDst && (cur.tm_isdst == 0))
473 return tm2transitiontime(&cur);
474 }
475 }
476 return tm2transitiontime(start);
477}
478
479static BOOL get_transition_date(const struct tm* start, BOOL toDst, SYSTEMTIME* pdate)
480{
481 WINPR_ASSERT(start);
482 WINPR_ASSERT(pdate);
483
484 *pdate = tm2transitiontime(NULL);
485
486 if (start->tm_isdst < 0)
487 return FALSE;
488
489 BOOL val = start->tm_isdst > 0; // the year starts with DST or not
490 BOOL toggled = FALSE;
491 struct tm cur = *start;
492 struct tm last = cur;
493 for (int day = 1; day <= 365; day++)
494 {
495 last = cur;
496 cur = next_day(&cur);
497 const BOOL curDst = (cur.tm_isdst > 0);
498 if ((val != curDst) && !toggled)
499 toggled = TRUE;
500
501 if (toggled)
502 {
503 if (toDst && curDst)
504 {
505 *pdate = get_transition_time(&last, toDst);
506 return TRUE;
507 }
508 if (!toDst && !curDst)
509 {
510 *pdate = get_transition_time(&last, toDst);
511 return TRUE;
512 }
513 }
514 }
515 return FALSE;
516}
517
518static LONG get_bias(const struct tm* start, BOOL dstBias)
519{
520 if ((start->tm_isdst > 0) && dstBias)
521 return get_gmtoff_min(start);
522 if ((start->tm_isdst == 0) && !dstBias)
523 return get_gmtoff_min(start);
524 if (start->tm_isdst < 0)
525 return get_gmtoff_min(start);
526
527 struct tm cur = *start;
528 for (int day = 1; day <= 365; day++)
529 {
530 cur = next_day(&cur);
531 if ((cur.tm_isdst > 0) && dstBias)
532 return get_gmtoff_min(&cur);
533 else if ((cur.tm_isdst == 0) && !dstBias)
534 return get_gmtoff_min(&cur);
535 }
536 return 0;
537}
538
539static BOOL map_iana_id(const char* iana, LPDYNAMIC_TIME_ZONE_INFORMATION tz)
540{
541 const char* winId = TimeZoneIanaToWindows(iana, TIME_ZONE_NAME_ID);
542 const char* winStd = TimeZoneIanaToWindows(iana, TIME_ZONE_NAME_STANDARD);
543 const char* winDst = TimeZoneIanaToWindows(iana, TIME_ZONE_NAME_DAYLIGHT);
544
545 if (winStd)
546 (void)ConvertUtf8ToWChar(winStd, tz->StandardName, ARRAYSIZE(tz->StandardName));
547 if (winDst)
548 (void)ConvertUtf8ToWChar(winDst, tz->DaylightName, ARRAYSIZE(tz->DaylightName));
549 if (winId)
550 (void)ConvertUtf8ToWChar(winId, tz->TimeZoneKeyName, ARRAYSIZE(tz->TimeZoneKeyName));
551
552 return winId != NULL;
553}
554
555static const char* weekday2str(WORD wDayOfWeek)
556{
557 switch (wDayOfWeek)
558 {
559 case 0:
560 return "SUNDAY";
561 case 1:
562 return "MONDAY";
563 case 2:
564 return "TUESDAY";
565 case 3:
566 return "WEDNESDAY";
567 case 4:
568 return "THURSDAY";
569 case 5:
570 return "FRIDAY";
571 case 6:
572 return "SATURDAY";
573 default:
574 return "DAY-OF-MAGIC";
575 }
576}
577
578static char* systemtime2str(const SYSTEMTIME* t, char* buffer, size_t len)
579{
580 const SYSTEMTIME empty = { 0 };
581
582 if (memcmp(t, &empty, sizeof(SYSTEMTIME)) == 0)
583 (void)_snprintf(buffer, len, "{ not set }");
584 else
585 {
586 (void)_snprintf(buffer, len,
587 "{ %" PRIu16 "-%" PRIu16 "-%" PRIu16 " [%s] %" PRIu16 ":%" PRIu16
588 ":%" PRIu16 ".%" PRIu16 "}",
589 t->wYear, t->wMonth, t->wDay, weekday2str(t->wDayOfWeek), t->wHour,
590 t->wMinute, t->wSecond, t->wMilliseconds);
591 }
592 return buffer;
593}
594
595static void log_print(wLog* log, DWORD level, const char* file, const char* fkt, size_t line, ...)
596{
597 if (!WLog_IsLevelActive(log, level))
598 return;
599
600 va_list ap = { 0 };
601 va_start(ap, line);
602 WLog_PrintMessageVA(log, WLOG_MESSAGE_TEXT, level, line, file, fkt, ap);
603 va_end(ap);
604}
605
606#define log_timezone(tzif, result) log_timezone_((tzif), (result), __FILE__, __func__, __LINE__)
607static void log_timezone_(const DYNAMIC_TIME_ZONE_INFORMATION* tzif, DWORD result, const char* file,
608 const char* fkt, size_t line)
609{
610 WINPR_ASSERT(tzif);
611
612 char buffer[130] = { 0 };
613 DWORD level = WLOG_TRACE;
614 wLog* log = WLog_Get(TAG);
615 log_print(log, level, file, fkt, line, "DYNAMIC_TIME_ZONE_INFORMATION {");
616
617 log_print(log, level, file, fkt, line, " Bias=%" PRIu32, tzif->Bias);
618 (void)ConvertWCharNToUtf8(tzif->StandardName, ARRAYSIZE(tzif->StandardName), buffer,
619 ARRAYSIZE(buffer));
620 log_print(log, level, file, fkt, line, " StandardName=%s", buffer);
621 log_print(log, level, file, fkt, line, " StandardDate=%s",
622 systemtime2str(&tzif->StandardDate, buffer, sizeof(buffer)));
623 log_print(log, level, file, fkt, line, " StandardBias=%" PRIu32, tzif->StandardBias);
624
625 (void)ConvertWCharNToUtf8(tzif->DaylightName, ARRAYSIZE(tzif->DaylightName), buffer,
626 ARRAYSIZE(buffer));
627 log_print(log, level, file, fkt, line, " DaylightName=%s", buffer);
628 log_print(log, level, file, fkt, line, " DaylightDate=%s",
629 systemtime2str(&tzif->DaylightDate, buffer, sizeof(buffer)));
630 log_print(log, level, file, fkt, line, " DaylightBias=%" PRIu32, tzif->DaylightBias);
631 (void)ConvertWCharNToUtf8(tzif->TimeZoneKeyName, ARRAYSIZE(tzif->TimeZoneKeyName), buffer,
632 ARRAYSIZE(buffer));
633 log_print(log, level, file, fkt, line, " TimeZoneKeyName=%s", buffer);
634 log_print(log, level, file, fkt, line, " DynamicDaylightTimeDisabled=DST-%s",
635 tzif->DynamicDaylightTimeDisabled ? "disabled" : "enabled");
636 switch (result)
637 {
638 case TIME_ZONE_ID_DAYLIGHT:
639 log_print(log, level, file, fkt, line, " DaylightDate in use");
640 break;
641 case TIME_ZONE_ID_STANDARD:
642 log_print(log, level, file, fkt, line, " StandardDate in use");
643 break;
644 default:
645 log_print(log, level, file, fkt, line, " UnknownDate in use");
646 break;
647 }
648 log_print(log, level, file, fkt, line, "}");
649}
650
651DWORD GetTimeZoneInformation(LPTIME_ZONE_INFORMATION lpTimeZoneInformation)
652{
654 DWORD rc = GetDynamicTimeZoneInformation(&dyn);
655 lpTimeZoneInformation->Bias = dyn.Bias;
656 lpTimeZoneInformation->DaylightBias = dyn.DaylightBias;
657 lpTimeZoneInformation->DaylightDate = dyn.DaylightDate;
658 lpTimeZoneInformation->StandardBias = dyn.StandardBias;
659 lpTimeZoneInformation->StandardDate = dyn.StandardDate;
660 memcpy(lpTimeZoneInformation->StandardName, dyn.StandardName,
661 sizeof(lpTimeZoneInformation->StandardName));
662 memcpy(lpTimeZoneInformation->DaylightName, dyn.DaylightName,
663 sizeof(lpTimeZoneInformation->DaylightName));
664 return rc;
665}
666
667BOOL SetTimeZoneInformation(const TIME_ZONE_INFORMATION* lpTimeZoneInformation)
668{
669 WINPR_UNUSED(lpTimeZoneInformation);
670 return FALSE;
671}
672
673BOOL SystemTimeToFileTime(const SYSTEMTIME* lpSystemTime, LPFILETIME lpFileTime)
674{
675 WINPR_UNUSED(lpSystemTime);
676 WINPR_UNUSED(lpFileTime);
677 return FALSE;
678}
679
680BOOL FileTimeToSystemTime(const FILETIME* lpFileTime, LPSYSTEMTIME lpSystemTime)
681{
682 WINPR_UNUSED(lpFileTime);
683 WINPR_UNUSED(lpSystemTime);
684 return FALSE;
685}
686
687BOOL SystemTimeToTzSpecificLocalTime(LPTIME_ZONE_INFORMATION lpTimeZone,
688 LPSYSTEMTIME lpUniversalTime, LPSYSTEMTIME lpLocalTime)
689{
690 WINPR_UNUSED(lpTimeZone);
691 WINPR_UNUSED(lpUniversalTime);
692 WINPR_UNUSED(lpLocalTime);
693 return FALSE;
694}
695
696BOOL TzSpecificLocalTimeToSystemTime(LPTIME_ZONE_INFORMATION lpTimeZoneInformation,
697 LPSYSTEMTIME lpLocalTime, LPSYSTEMTIME lpUniversalTime)
698{
699 WINPR_UNUSED(lpTimeZoneInformation);
700 WINPR_UNUSED(lpLocalTime);
701 WINPR_UNUSED(lpUniversalTime);
702 return FALSE;
703}
704
705#endif
706
707/*
708 * GetDynamicTimeZoneInformation is provided by the SDK if _WIN32_WINNT >= 0x0600 in SDKs above 7.1A
709 * and incorrectly if _WIN32_WINNT >= 0x0501 in older SDKs
710 */
711#if !defined(_WIN32) || \
712 (defined(_WIN32) && (defined(NTDDI_WIN8) && _WIN32_WINNT < 0x0600 || \
713 !defined(NTDDI_WIN8) && _WIN32_WINNT < 0x0501)) /* Windows Vista */
714
715typedef enum
716{
717 HAVE_TRANSITION_DATES = 0,
718 HAVE_NO_STANDARD_TRANSITION_DATE = 1,
719 HAVE_NO_DAYLIGHT_TRANSITION_DATE = 2
720} dyn_transition_result;
721
722static int dynamic_time_zone_from_localtime(const struct tm* local_time,
724{
725 WINPR_ASSERT(local_time);
726 WINPR_ASSERT(tz);
727 int rc = HAVE_TRANSITION_DATES;
728
729 tz->Bias = get_bias(local_time, FALSE);
730
731 /* If the current time has (or had) DST */
732 if (local_time->tm_isdst >= 0)
733 {
734 /* DST bias is the difference between standard time and DST in minutes */
735 const LONG d = get_bias(local_time, TRUE);
736 tz->DaylightBias = -1 * (tz->Bias - d);
737 if (!get_transition_date(local_time, FALSE, &tz->StandardDate))
738 {
739 rc |= HAVE_NO_STANDARD_TRANSITION_DATE;
740 tz->StandardBias = 0;
741 }
742 if (!get_transition_date(local_time, TRUE, &tz->DaylightDate))
743 {
744 rc |= HAVE_NO_DAYLIGHT_TRANSITION_DATE;
745 tz->DaylightBias = 0;
746 }
747 }
748 return rc;
749}
750
751DWORD GetDynamicTimeZoneInformation(PDYNAMIC_TIME_ZONE_INFORMATION pTimeZoneInformation)
752{
753 const char** list = NULL;
754 char* tzid = NULL;
755 const char* defaultName = "Client Local Time";
756 DWORD res = TIME_ZONE_ID_UNKNOWN;
757 const DYNAMIC_TIME_ZONE_INFORMATION empty = { 0 };
758
759 WINPR_ASSERT(pTimeZoneInformation);
760
761 *pTimeZoneInformation = empty;
762 (void)ConvertUtf8ToWChar(defaultName, pTimeZoneInformation->StandardName,
763 ARRAYSIZE(pTimeZoneInformation->StandardName));
764
765 const time_t t = time(NULL);
766 struct tm tres = { 0 };
767 struct tm* local_time = localtime_r(&t, &tres);
768 if (!local_time)
769 goto out_error;
770
771 pTimeZoneInformation->Bias = get_bias(local_time, FALSE);
772 if (local_time->tm_isdst >= 0)
773 dynamic_time_zone_from_localtime(local_time, pTimeZoneInformation);
774
775 tzid = winpr_guess_time_zone();
776 if (!map_iana_id(tzid, pTimeZoneInformation))
777 {
778 const size_t len = TimeZoneIanaAbbrevGet(local_time->tm_zone, NULL, 0);
779 list = (const char**)calloc(len, sizeof(const char*));
780 if (!list)
781 goto out_error;
782 const size_t size = TimeZoneIanaAbbrevGet(local_time->tm_zone, list, len);
783 for (size_t x = 0; x < size; x++)
784 {
785 const char* id = list[x];
786 if (map_iana_id(id, pTimeZoneInformation))
787 {
788 res = (local_time->tm_isdst) ? TIME_ZONE_ID_DAYLIGHT : TIME_ZONE_ID_STANDARD;
789 break;
790 }
791 }
792 }
793 else
794 res = (local_time->tm_isdst) ? TIME_ZONE_ID_DAYLIGHT : TIME_ZONE_ID_STANDARD;
795
796out_error:
797 free(tzid);
798 free((void*)list);
799
800 log_timezone(pTimeZoneInformation, res);
801 return res;
802}
803
804BOOL SetDynamicTimeZoneInformation(const DYNAMIC_TIME_ZONE_INFORMATION* lpTimeZoneInformation)
805{
806 WINPR_UNUSED(lpTimeZoneInformation);
807 return FALSE;
808}
809
810BOOL GetTimeZoneInformationForYear(USHORT wYear, PDYNAMIC_TIME_ZONE_INFORMATION pdtzi,
812{
813 WINPR_UNUSED(wYear);
814 WINPR_UNUSED(pdtzi);
815 WINPR_UNUSED(ptzi);
816 return FALSE;
817}
818
819#endif
820
821#if !defined(_WIN32) || (defined(_WIN32) && (_WIN32_WINNT < 0x0601)) /* Windows 7 */
822
823BOOL SystemTimeToTzSpecificLocalTimeEx(const DYNAMIC_TIME_ZONE_INFORMATION* lpTimeZoneInformation,
824 const SYSTEMTIME* lpUniversalTime, LPSYSTEMTIME lpLocalTime)
825{
826 WINPR_UNUSED(lpTimeZoneInformation);
827 WINPR_UNUSED(lpUniversalTime);
828 WINPR_UNUSED(lpLocalTime);
829 return FALSE;
830}
831
832BOOL TzSpecificLocalTimeToSystemTimeEx(const DYNAMIC_TIME_ZONE_INFORMATION* lpTimeZoneInformation,
833 const SYSTEMTIME* lpLocalTime, LPSYSTEMTIME lpUniversalTime)
834{
835 WINPR_UNUSED(lpTimeZoneInformation);
836 WINPR_UNUSED(lpLocalTime);
837 WINPR_UNUSED(lpUniversalTime);
838 return FALSE;
839}
840
841#endif
842
843#if !defined(_WIN32)
844
845DWORD EnumDynamicTimeZoneInformation(DWORD dwIndex,
846 PDYNAMIC_TIME_ZONE_INFORMATION lpTimeZoneInformation)
847{
848 if (!lpTimeZoneInformation)
849 return ERROR_INVALID_PARAMETER;
850 const DYNAMIC_TIME_ZONE_INFORMATION empty = { 0 };
851 *lpTimeZoneInformation = empty;
852
853 const TimeZoneNameMapEntry* entry = TimeZoneGetAt(dwIndex);
854 if (!entry)
855 return ERROR_NO_MORE_ITEMS;
856
857 if (entry->DaylightName)
858 (void)ConvertUtf8ToWChar(entry->DaylightName, lpTimeZoneInformation->DaylightName,
859 ARRAYSIZE(lpTimeZoneInformation->DaylightName));
860 if (entry->StandardName)
861 (void)ConvertUtf8ToWChar(entry->StandardName, lpTimeZoneInformation->StandardName,
862 ARRAYSIZE(lpTimeZoneInformation->StandardName));
863 if (entry->Id)
864 (void)ConvertUtf8ToWChar(entry->Id, lpTimeZoneInformation->TimeZoneKeyName,
865 ARRAYSIZE(lpTimeZoneInformation->TimeZoneKeyName));
866
867 const time_t t = time(NULL);
868 struct tm tres = { 0 };
869
870 char* tzcopy = entry->Iana ? setNewAndSaveOldTZ(entry->Iana) : NULL;
871
872 struct tm* local_time = localtime_r(&t, &tres);
873
874 if (local_time)
875 dynamic_time_zone_from_localtime(local_time, lpTimeZoneInformation);
876
877 if (entry->Iana)
878 restoreSavedTZ(tzcopy);
879
880 return ERROR_SUCCESS;
881}
882
883// NOLINTBEGIN(readability-non-const-parameter)
884DWORD GetDynamicTimeZoneInformationEffectiveYears(
885 const DYNAMIC_TIME_ZONE_INFORMATION* lpTimeZoneInformation, LPDWORD FirstYear, LPDWORD LastYear)
886// NOLINTEND(readability-non-const-parameter)
887{
888 WINPR_UNUSED(lpTimeZoneInformation);
889 WINPR_UNUSED(FirstYear);
890 WINPR_UNUSED(LastYear);
891 return ERROR_FILE_NOT_FOUND;
892}
893
894#elif _WIN32_WINNT < 0x0602 /* Windows 8 */
895DWORD EnumDynamicTimeZoneInformation(DWORD dwIndex,
896 PDYNAMIC_TIME_ZONE_INFORMATION lpTimeZoneInformation)
897{
898 WINPR_UNUSED(dwIndex);
899 WINPR_UNUSED(lpTimeZoneInformation);
900 return ERROR_NO_MORE_ITEMS;
901}
902
903DWORD GetDynamicTimeZoneInformationEffectiveYears(
904 const DYNAMIC_TIME_ZONE_INFORMATION* lpTimeZoneInformation, LPDWORD FirstYear, LPDWORD LastYear)
905{
906 WINPR_UNUSED(lpTimeZoneInformation);
907 WINPR_UNUSED(FirstYear);
908 WINPR_UNUSED(LastYear);
909 return ERROR_FILE_NOT_FOUND;
910}
911#endif
912
913#if !defined(_WIN32)
914char* setNewAndSaveOldTZ(const char* val)
915{
916 // NOLINTBEGIN(concurrency-mt-unsafe)
917 const char* otz = getenv("TZ");
918 char* oldtz = NULL;
919 if (otz)
920 oldtz = _strdup(otz);
921 setenv("TZ", val, 1);
922 tzset();
923 // NOLINTEND(concurrency-mt-unsafe)
924 return oldtz;
925}
926
927void restoreSavedTZ(char* saved)
928{
929 // NOLINTBEGIN(concurrency-mt-unsafe)
930 if (saved)
931 {
932 setenv("TZ", saved, 1);
933 free(saved);
934 }
935 else
936 unsetenv("TZ");
937 tzset();
938 // NOLINTEND(concurrency-mt-unsafe)
939}
940#endif