22#include <winpr/config.h>
24#include <winpr/environment.h>
25#include <winpr/wtypes.h>
26#include <winpr/timezone.h>
28#include <winpr/assert.h>
29#include <winpr/file.h>
33#define TAG WINPR_TAG("timezone")
35#include "TimeZoneNameMap.h"
36#include "TimeZoneIanaAbbrevMap.h"
46static char* winpr_read_unix_timezone_identifier_from_file(FILE* fp)
48 const INT CHUNK_SIZE = 32;
51 size_t length = CHUNK_SIZE;
53 char* tzid = malloc(length);
59 rc = fread(tzid + read, 1, length - read - 1UL, fp);
63 if (read < (length - 1UL))
66 if (read > length - 1UL)
73 char* tmp = (
char*)realloc(tzid, length);
92 if (tzid[read - 1] ==
'\n')
93 tzid[read - 1] =
'\0';
99static char* winpr_get_timezone_from_link(
const char* links[],
size_t count)
101 const char* _links[] = {
"/etc/localtime",
"/etc/TZ" };
106 count = ARRAYSIZE(_links);
116 for (
size_t x = 0; x < count; x++)
119 const char* link = links[x];
120 char* buf = realpath(link, NULL);
127 size_t len = pos = strlen(buf);
130 for (
size_t i = 1; i <= len; i++)
132 const size_t curpos = len - i;
133 const char cur = buf[curpos];
145 if ((len == 0) || (sep != 2))
148 tzid = (
char*)calloc(alloc + 1,
sizeof(
char));
153 strncpy(tzid, &buf[pos], alloc);
154 WLog_DBG(TAG,
"tzid: %s", tzid);
168#include "../utils/android.h"
170static char* winpr_get_android_timezone_identifier(
void)
176 if (jniVm && ((*jniVm)->GetEnv(jniVm, (
void**)&jniEnv, JNI_VERSION_1_6) == JNI_OK))
181 jmethodID jDefaultTimezone;
182 jmethodID jTimezoneIdentifier;
184 jboolean attached = (*jniVm)->AttachCurrentThread(jniVm, &jniEnv, NULL);
185 jObjClass = (*jniEnv)->FindClass(jniEnv,
"java/util/TimeZone");
191 (*jniEnv)->GetStaticMethodID(jniEnv, jObjClass,
"getDefault",
"()Ljava/util/TimeZone;");
193 if (!jDefaultTimezone)
196 jObj = (*jniEnv)->CallStaticObjectMethod(jniEnv, jObjClass, jDefaultTimezone);
201 jTimezoneIdentifier =
202 (*jniEnv)->GetMethodID(jniEnv, jObjClass,
"getID",
"()Ljava/lang/String;");
204 if (!jTimezoneIdentifier)
207 tzJId = (*jniEnv)->CallObjectMethod(jniEnv, jObj, jTimezoneIdentifier);
212 raw = (*jniEnv)->GetStringUTFChars(jniEnv, tzJId, 0);
217 (*jniEnv)->ReleaseStringUTFChars(jniEnv, tzJId, raw);
221 (*jniVm)->DetachCurrentThread(jniVm);
227 FILE* fp = popen(
"getprop persist.sys.timezone",
"r");
231 tzid = winpr_read_unix_timezone_identifier_from_file(fp);
240static char* winpr_get_unix_timezone_identifier_from_file(
void)
243 return winpr_get_android_timezone_identifier();
247#if !defined(WINPR_TIMEZONE_FILE)
249 "Please define WINPR_TIMEZONE_FILE with the path to your timezone file (e.g. /etc/timezone or similar)"
251 fp = winpr_fopen(WINPR_TIMEZONE_FILE,
"r");
257 tzid = winpr_read_unix_timezone_identifier_from_file(fp);
260 WLog_DBG(TAG,
"tzid: %s", tzid);
265static char* winpr_time_zone_from_env(
void)
270 DWORD nSize = GetEnvironmentVariableA(tz, NULL, 0);
273 tzid = (
char*)calloc(nSize,
sizeof(
char));
276 if (!GetEnvironmentVariableA(tz, tzid, nSize))
278 else if (tzid[0] ==
':')
281 memmove(tzid, tzid + 1, nSize -
sizeof(
char));
292static char* winpr_translate_time_zone(
const char* tzid)
294 const char* zipath =
"/usr/share/zoneinfo/";
296 const char* links[] = { buf };
309 winpr_asprintf(&buf, &bsize,
"%s%s", zipath, tzid);
313 char* ntzid = winpr_get_timezone_from_link(links, 1);
318static char* winpr_guess_time_zone(
void)
320 char* tzid = winpr_time_zone_from_env();
323 tzid = winpr_get_unix_timezone_identifier_from_file();
326 tzid = winpr_get_timezone_from_link(NULL, 0);
332 char* ntzid = winpr_translate_time_zone(tzid);
342static SYSTEMTIME tm2systemtime(
const struct tm* t)
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;
360static struct tm systemtime2tm(const
SYSTEMTIME* st)
365 if (st->wYear >= 1900)
366 t.tm_year = st->wYear - 1900;
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;
378static LONG get_gmtoff_min(
const struct tm* t)
381 return -(LONG)(t->tm_gmtoff / 60l);
384static struct tm next_day(const struct tm* start)
386 struct tm cur = *start;
392 const time_t t = mktime(&cur);
393 (void)localtime_r(&t, &cur);
397static struct tm adjust_time(const struct tm* start,
int hour,
int minute)
399 struct tm cur = *start;
404 const time_t t = mktime(&cur);
405 (void)localtime_r(&t, &cur);
410static WORD get_transition_weekday_occurrence(
const SYSTEMTIME* st)
413 struct tm start = systemtime2tm(st);
416 struct tm next = start;
422 const time_t t = mktime(&next);
425 struct tm cur = { 0 };
426 (void)localtime_r(&t, &cur);
428 if (cur.tm_mon + 1 != st->wMonth)
431 if (cur.tm_wday == st->wDayOfWeek)
433 if (cur.tm_mday <= st->wDay)
446static SYSTEMTIME tm2transitiontime(
const struct tm* cur)
451 t.wDay = get_transition_weekday_occurrence(&t);
458static SYSTEMTIME get_transition_time(
const struct tm* start, BOOL toDst)
460 BOOL toggled = FALSE;
461 struct tm first = adjust_time(start, 0, 0);
462 for (
int hour = 0; hour < 24; hour++)
464 struct tm cur = adjust_time(start, hour, 0);
465 if (cur.tm_isdst != first.tm_isdst)
470 if (toDst && (cur.tm_isdst > 0))
471 return tm2transitiontime(&cur);
472 else if (!toDst && (cur.tm_isdst == 0))
473 return tm2transitiontime(&cur);
476 return tm2transitiontime(start);
479static BOOL get_transition_date(
const struct tm* start, BOOL toDst,
SYSTEMTIME* pdate)
484 *pdate = tm2transitiontime(NULL);
486 if (start->tm_isdst < 0)
489 BOOL val = start->tm_isdst > 0;
490 BOOL toggled = FALSE;
491 struct tm cur = *start;
492 struct tm last = cur;
493 for (
int day = 1; day <= 365; day++)
496 cur = next_day(&cur);
497 const BOOL curDst = (cur.tm_isdst > 0);
498 if ((val != curDst) && !toggled)
505 *pdate = get_transition_time(&last, toDst);
508 if (!toDst && !curDst)
510 *pdate = get_transition_time(&last, toDst);
518static LONG get_bias(
const struct tm* start, BOOL dstBias)
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);
527 struct tm cur = *start;
528 for (
int day = 1; day <= 365; day++)
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);
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);
546 (void)ConvertUtf8ToWChar(winStd, tz->StandardName, ARRAYSIZE(tz->StandardName));
548 (void)ConvertUtf8ToWChar(winDst, tz->DaylightName, ARRAYSIZE(tz->DaylightName));
550 (void)ConvertUtf8ToWChar(winId, tz->TimeZoneKeyName, ARRAYSIZE(tz->TimeZoneKeyName));
552 return winId != NULL;
555static const char* weekday2str(WORD wDayOfWeek)
574 return "DAY-OF-MAGIC";
578static char* systemtime2str(
const SYSTEMTIME* t,
char* buffer,
size_t len)
582 if (memcmp(t, &empty,
sizeof(
SYSTEMTIME)) == 0)
583 (void)_snprintf(buffer, len,
"{ not set }");
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);
595static void log_print(wLog* log, DWORD level,
const char* file,
const char* fkt,
size_t line, ...)
597 if (!WLog_IsLevelActive(log, level))
602 WLog_PrintMessageVA(log, WLOG_MESSAGE_TEXT, level, line, file, fkt, ap);
606#define log_timezone(tzif, result) log_timezone_((tzif), (result), __FILE__, __func__, __LINE__)
608 const char* fkt,
size_t line)
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 {");
617 log_print(log, level, file, fkt, line,
" Bias=%" PRIu32, tzif->Bias);
618 (void)ConvertWCharNToUtf8(tzif->StandardName, ARRAYSIZE(tzif->StandardName), 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);
625 (void)ConvertWCharNToUtf8(tzif->DaylightName, ARRAYSIZE(tzif->DaylightName), 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,
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");
638 case TIME_ZONE_ID_DAYLIGHT:
639 log_print(log, level, file, fkt, line,
" DaylightDate in use");
641 case TIME_ZONE_ID_STANDARD:
642 log_print(log, level, file, fkt, line,
" StandardDate in use");
645 log_print(log, level, file, fkt, line,
" UnknownDate in use");
648 log_print(log, level, file, fkt, line,
"}");
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));
669 WINPR_UNUSED(lpTimeZoneInformation);
673BOOL SystemTimeToFileTime(
const SYSTEMTIME* lpSystemTime, LPFILETIME lpFileTime)
675 WINPR_UNUSED(lpSystemTime);
676 WINPR_UNUSED(lpFileTime);
680BOOL FileTimeToSystemTime(
const FILETIME* lpFileTime, LPSYSTEMTIME lpSystemTime)
682 WINPR_UNUSED(lpFileTime);
683 WINPR_UNUSED(lpSystemTime);
688 LPSYSTEMTIME lpUniversalTime, LPSYSTEMTIME lpLocalTime)
690 WINPR_UNUSED(lpTimeZone);
691 WINPR_UNUSED(lpUniversalTime);
692 WINPR_UNUSED(lpLocalTime);
697 LPSYSTEMTIME lpLocalTime, LPSYSTEMTIME lpUniversalTime)
699 WINPR_UNUSED(lpTimeZoneInformation);
700 WINPR_UNUSED(lpLocalTime);
701 WINPR_UNUSED(lpUniversalTime);
711#if !defined(_WIN32) || \
712 (defined(_WIN32) && (defined(NTDDI_WIN8) && _WIN32_WINNT < 0x0600 || \
713 !defined(NTDDI_WIN8) && _WIN32_WINNT < 0x0501))
717 HAVE_TRANSITION_DATES = 0,
718 HAVE_NO_STANDARD_TRANSITION_DATE = 1,
719 HAVE_NO_DAYLIGHT_TRANSITION_DATE = 2
720} dyn_transition_result;
722static int dynamic_time_zone_from_localtime(
const struct tm* local_time,
725 WINPR_ASSERT(local_time);
727 int rc = HAVE_TRANSITION_DATES;
729 tz->Bias = get_bias(local_time, FALSE);
732 if (local_time->tm_isdst >= 0)
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))
739 rc |= HAVE_NO_STANDARD_TRANSITION_DATE;
740 tz->StandardBias = 0;
742 if (!get_transition_date(local_time, TRUE, &tz->DaylightDate))
744 rc |= HAVE_NO_DAYLIGHT_TRANSITION_DATE;
745 tz->DaylightBias = 0;
753 const char** list = NULL;
755 const char* defaultName =
"Client Local Time";
756 DWORD res = TIME_ZONE_ID_UNKNOWN;
759 WINPR_ASSERT(pTimeZoneInformation);
761 *pTimeZoneInformation = empty;
762 (void)ConvertUtf8ToWChar(defaultName, pTimeZoneInformation->StandardName,
763 ARRAYSIZE(pTimeZoneInformation->StandardName));
765 const time_t t = time(NULL);
766 struct tm tres = { 0 };
767 struct tm* local_time = localtime_r(&t, &tres);
771 pTimeZoneInformation->Bias = get_bias(local_time, FALSE);
772 if (local_time->tm_isdst >= 0)
773 dynamic_time_zone_from_localtime(local_time, pTimeZoneInformation);
775 tzid = winpr_guess_time_zone();
776 if (!map_iana_id(tzid, pTimeZoneInformation))
778 const size_t len = TimeZoneIanaAbbrevGet(local_time->tm_zone, NULL, 0);
779 list = (
const char**)calloc(len,
sizeof(
const char*));
782 const size_t size = TimeZoneIanaAbbrevGet(local_time->tm_zone, list, len);
783 for (
size_t x = 0; x < size; x++)
785 const char*
id = list[x];
786 if (map_iana_id(
id, pTimeZoneInformation))
788 res = (local_time->tm_isdst) ? TIME_ZONE_ID_DAYLIGHT : TIME_ZONE_ID_STANDARD;
794 res = (local_time->tm_isdst) ? TIME_ZONE_ID_DAYLIGHT : TIME_ZONE_ID_STANDARD;
800 log_timezone(pTimeZoneInformation, res);
806 WINPR_UNUSED(lpTimeZoneInformation);
821#if !defined(_WIN32) || (defined(_WIN32) && (_WIN32_WINNT < 0x0601))
824 const SYSTEMTIME* lpUniversalTime, LPSYSTEMTIME lpLocalTime)
826 WINPR_UNUSED(lpTimeZoneInformation);
827 WINPR_UNUSED(lpUniversalTime);
828 WINPR_UNUSED(lpLocalTime);
833 const SYSTEMTIME* lpLocalTime, LPSYSTEMTIME lpUniversalTime)
835 WINPR_UNUSED(lpTimeZoneInformation);
836 WINPR_UNUSED(lpLocalTime);
837 WINPR_UNUSED(lpUniversalTime);
845DWORD EnumDynamicTimeZoneInformation(DWORD dwIndex,
848 if (!lpTimeZoneInformation)
849 return ERROR_INVALID_PARAMETER;
851 *lpTimeZoneInformation = empty;
855 return ERROR_NO_MORE_ITEMS;
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));
864 (void)ConvertUtf8ToWChar(entry->Id, lpTimeZoneInformation->TimeZoneKeyName,
865 ARRAYSIZE(lpTimeZoneInformation->TimeZoneKeyName));
867 const time_t t = time(NULL);
868 struct tm tres = { 0 };
870 char* tzcopy = entry->Iana ? setNewAndSaveOldTZ(entry->Iana) : NULL;
872 struct tm* local_time = localtime_r(&t, &tres);
875 dynamic_time_zone_from_localtime(local_time, lpTimeZoneInformation);
878 restoreSavedTZ(tzcopy);
880 return ERROR_SUCCESS;
884DWORD GetDynamicTimeZoneInformationEffectiveYears(
888 WINPR_UNUSED(lpTimeZoneInformation);
889 WINPR_UNUSED(FirstYear);
890 WINPR_UNUSED(LastYear);
891 return ERROR_FILE_NOT_FOUND;
894#elif _WIN32_WINNT < 0x0602
895DWORD EnumDynamicTimeZoneInformation(DWORD dwIndex,
898 WINPR_UNUSED(dwIndex);
899 WINPR_UNUSED(lpTimeZoneInformation);
900 return ERROR_NO_MORE_ITEMS;
903DWORD GetDynamicTimeZoneInformationEffectiveYears(
906 WINPR_UNUSED(lpTimeZoneInformation);
907 WINPR_UNUSED(FirstYear);
908 WINPR_UNUSED(LastYear);
909 return ERROR_FILE_NOT_FOUND;
914char* setNewAndSaveOldTZ(
const char* val)
917 const char* otz = getenv(
"TZ");
920 oldtz = _strdup(otz);
921 setenv(
"TZ", val, 1);
927void restoreSavedTZ(
char* saved)
932 setenv(
"TZ", saved, 1);