20#include <winpr/config.h>
22#include <winpr/atexit.h>
23#include <winpr/assert.h>
24#include <winpr/string.h>
25#include <winpr/synch.h>
26#include <winpr/json.h>
27#include <winpr/path.h>
28#include <winpr/file.h>
34#define TAG WINPR_TAG("timezone.utils")
36#if defined(WITH_TIMEZONE_ICU)
37#include <unicode/ucal.h>
39#include "WindowsZones.h"
42#include "TimeZoneNameMap.h"
43#include "TimeZoneIanaAliases.h"
45#if defined(WITH_TIMEZONE_COMPILED)
46#include "TimeZoneNameMap_static.h"
53} TimeZoneNameMapContext;
55static TimeZoneNameMapContext tz_context = WINPR_C_ARRAY_INIT;
61 free(entry->DaylightName);
62 free(entry->DisplayName);
65 free(entry->StandardName);
77 if (entry->DaylightName)
78 clone.DaylightName = _strdup(entry->DaylightName);
79 if (entry->DisplayName)
80 clone.DisplayName = _strdup(entry->DisplayName);
82 clone.Iana = _strdup(entry->Iana);
84 clone.Id = _strdup(entry->Id);
85 if (entry->StandardName)
86 clone.StandardName = _strdup(entry->StandardName);
90static void tz_context_free(
void)
92 for (
size_t x = 0; x < tz_context.count; x++)
93 tz_entry_free(&tz_context.entries[x]);
94 free(tz_context.entries);
96 tz_context.entries =
nullptr;
99#if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON)
100char* TimeZoneJsonGetString(WINPR_JSON* json,
size_t pos,
const char* name)
105 WLog_WARN(TAG,
"Invalid JSON entry at entry %" PRIuz
", missing an Object named '%s'", pos,
114 "Invalid JSON entry at entry %" PRIuz
", Object named '%s': Not of type string",
122 WLog_WARN(TAG,
"Invalid JSON entry at entry %" PRIuz
", Object named '%s': nullptr string",
135 WLog_WARN(TAG,
"Invalid JSON entry at entry %" PRIuz
", expected an array", pos);
139 entry->Id = TimeZoneJsonGetString(json, pos,
"Id");
140 entry->StandardName = TimeZoneJsonGetString(json, pos,
"StandardName");
141 entry->DisplayName = TimeZoneJsonGetString(json, pos,
"DisplayName");
142 entry->DaylightName = TimeZoneJsonGetString(json, pos,
"DaylightName");
143 entry->Iana = TimeZoneJsonGetString(json, pos,
"Iana");
144 if (!entry->Id || !entry->StandardName || !entry->DisplayName || !entry->DaylightName ||
147 tz_entry_free(entry);
153WINPR_JSON* TimeZoneJsonParseFile(
const char* filename)
157 WLog_WARN(TAG,
"Timezone resource file '%s' is not a valid JSON file", filename);
162static BOOL reallocate_context(TimeZoneNameMapContext* context,
size_t size_to_add)
170 "Failed to reallocate TimeZoneNameMapEntry::entries to %" PRIuz
" elements",
171 context->count + size_to_add);
174 const size_t offset = context->count;
175 context->entries = tmp;
176 context->count += size_to_add;
183static BOOL CALLBACK load_timezones(
PINIT_ONCE once, PVOID param, PVOID* pvcontext)
185 TimeZoneNameMapContext* context = param;
186 WINPR_ASSERT(context);
187 WINPR_UNUSED(pvcontext);
190 const TimeZoneNameMapContext empty = WINPR_C_ARRAY_INIT;
193#if defined(WITH_TIMEZONE_FROM_FILE) && defined(WITH_WINPR_JSON)
195 WINPR_JSON* json =
nullptr;
196 char* filename = GetCombinedPath(WINPR_RESOURCE_ROOT,
"TimeZoneNameMap.json");
199 WLog_WARN(TAG,
"Could not create WinPR timezone resource filename");
203 json = TimeZoneJsonParseFile(filename);
209 WLog_WARN(TAG,
"Invalid top level JSON type in file %s, expected an array", filename);
216 WLog_WARN(TAG,
"Invalid top level JSON type in file %s, expected an array", filename);
220 const size_t offset = context->count;
221 if (!reallocate_context(context, count))
223 for (
size_t x = 0; x < count; x++)
226 if (!tz_parse_json_entry(entry, x, &context->entries[offset + x]))
236#if defined(WITH_TIMEZONE_COMPILED)
238 const size_t offset = context->count;
239 if (!reallocate_context(context, TimeZoneNameMapSize))
241 for (
size_t x = 0; x < TimeZoneNameMapSize; x++)
242 context->entries[offset + x] = tz_entry_clone(&TimeZoneNameMap[x]);
246 (void)winpr_atexit(tz_context_free);
252 static INIT_ONCE init_guard = INIT_ONCE_STATIC_INIT;
254 if (!InitOnceExecuteOnce(&init_guard, load_timezones, &tz_context,
nullptr))
256 if (index >= tz_context.count)
258 return &tz_context.entries[index];
266 case TIME_ZONE_NAME_IANA:
268 case TIME_ZONE_NAME_ID:
270 case TIME_ZONE_NAME_STANDARD:
271 return entry->StandardName;
272 case TIME_ZONE_NAME_DISPLAY:
273 return entry->DisplayName;
274 case TIME_ZONE_NAME_DAYLIGHT:
275 return entry->DaylightName;
283 if (!entry || !iana || !entry->Iana)
285 return strcmp(iana, entry->Iana) == 0;
290 if (!entry || !
id || !entry->Id)
292 return strcmp(
id, entry->Id) == 0;
295static const char* get_for_type(
const char* val, TimeZoneNameType type,
308 return return_type(entry, type);
312#if defined(WITH_TIMEZONE_ICU)
313static char* get_wzid_icu(
const UChar* utzid,
size_t utzid_len)
316 UErrorCode error = U_ZERO_ERROR;
318 int32_t rc = ucal_getWindowsTimeZoneID(utzid, WINPR_ASSERTING_INT_CAST(int32_t, utzid_len),
320 if ((error == U_BUFFER_OVERFLOW_ERROR) && (rc > 0))
323 UChar* wzid = calloc((
size_t)rc + 1,
sizeof(UChar));
326 UErrorCode error2 = U_ZERO_ERROR;
327 int32_t rc2 = ucal_getWindowsTimeZoneID(
328 utzid, WINPR_ASSERTING_INT_CAST(int32_t, utzid_len), wzid, rc, &error2);
329 if (U_SUCCESS(error2) && (rc2 > 0))
330 res = ConvertWCharNToUtf8Alloc(wzid, (
size_t)rc,
nullptr);
337static char* get(
const char* iana)
339 size_t utzid_len = 0;
340 UChar* utzid = ConvertUtf8ToWCharAlloc(iana, &utzid_len);
344 char* wzid = get_wzid_icu(utzid, utzid_len);
349static const char* map_fallback(
const char* iana, TimeZoneNameType type)
351 char* wzid = get(iana);
355 const char* res = get_for_type(wzid, type, id_cmp);
360static const char* map_fallback(
const char* iana, WINPR_ATTR_UNUSED TimeZoneNameType type)
365 for (
size_t x = 0; x < WindowsZonesNrElements; x++)
368 if (strchr(entry->tzid,
' '))
370 const char* res =
nullptr;
371 char* tzid = _strdup(entry->tzid);
375 char* space = strchr(tzid,
' ');
378 if (strcmp(tzid, iana) == 0)
380 res = entry->windows;
389 else if (strcmp(entry->tzid, iana) == 0)
390 return entry->windows;
398static const char* iana_to_windows(
const char* iana, TimeZoneNameType type)
400 const char* val = get_for_type(iana, type, iana_cmp);
404 const char* wzid = map_fallback(iana, type);
408 return get_for_type(wzid, type, id_cmp);
411const char* TimeZoneIanaToWindows(
const char* iana, TimeZoneNameType type)
416 const char* val = iana_to_windows(iana, type);
422 const char* alias = TimeZoneIanaAlias(iana);
426 return iana_to_windows(alias, type);
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON *object, const char *string)
Check if JSON has an object matching the name.
WINPR_ATTR_NODISCARD WINPR_API size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
WINPR_API WINPR_JSON * WINPR_JSON_ParseFromFile(const char *filename)
Parse a JSON string read from a file filename.
WINPR_ATTR_NODISCARD WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON *object, const char *string)
Same as WINPR_JSON_GetObjectItem but with case sensitive matching.
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
WINPR_ATTR_NODISCARD WINPR_API WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
WINPR_ATTR_NODISCARD WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.