#include "unicode/utypes.h"
#if U_PLATFORM_USES_ONLY_WIN32_API
#include "wintz.h"
#include "cmemory.h"
#include "cstring.h"
#include "unicode/ures.h"
#include "unicode/ustring.h"
#include "uresimp.h"
#ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif
# define VC_EXTRALEAN
# define NOUSER
# define NOSERVICE
# define NOIME
# define NOMCX
#include <windows.h>
U_NAMESPACE_BEGIN
#define MAX_TIMEZONE_ID_LENGTH 128
U_INTERNAL const char* U_EXPORT2
uprv_detectWindowsTimeZone()
{
UErrorCode status = U_ZERO_ERROR;
char* icuid = nullptr;
char dynamicTZKeyName[MAX_TIMEZONE_ID_LENGTH];
char tmpid[MAX_TIMEZONE_ID_LENGTH];
int32_t len;
int id = GEOID_NOT_AVAILABLE;
int errorCode;
wchar_t ISOcodeW[3] = {};
char ISOcode[3] = {};
DYNAMIC_TIME_ZONE_INFORMATION dynamicTZI;
uprv_memset(&dynamicTZI, 0, sizeof(dynamicTZI));
uprv_memset(dynamicTZKeyName, 0, sizeof(dynamicTZKeyName));
uprv_memset(tmpid, 0, sizeof(tmpid));
if (TIME_ZONE_ID_INVALID == GetDynamicTimeZoneInformation(&dynamicTZI)) {
return nullptr;
}
id = GetUserGeoID(GEOCLASS_NATION);
errorCode = GetGeoInfoW(id, GEO_ISO2, ISOcodeW, 3, 0);
u_strToUTF8(ISOcode, UPRV_LENGTHOF(ISOcode), nullptr,
reinterpret_cast<const UChar*>(ISOcodeW), UPRV_LENGTHOF(ISOcodeW), &status);
LocalUResourceBundlePointer bundle(ures_openDirect(nullptr, "windowsZones", &status));
ures_getByKey(bundle.getAlias(), "mapTimezones", bundle.getAlias(), &status);
u_strToUTF8(dynamicTZKeyName, UPRV_LENGTHOF(dynamicTZKeyName), nullptr,
reinterpret_cast<const UChar*>(dynamicTZI.TimeZoneKeyName), -1, &status);
if (U_FAILURE(status)) {
return nullptr;
}
if (dynamicTZI.TimeZoneKeyName[0] != 0) {
StackUResourceBundle winTZ;
ures_getByKey(bundle.getAlias(), dynamicTZKeyName, winTZ.getAlias(), &status);
if (U_SUCCESS(status)) {
const UChar* icuTZ = nullptr;
if (errorCode != 0) {
icuTZ = ures_getStringByKey(winTZ.getAlias(), ISOcode, &len, &status);
}
if (errorCode == 0 || icuTZ == nullptr) {
status = U_ZERO_ERROR;
icuTZ = ures_getStringByKey(winTZ.getAlias(), "001", &len, &status);
}
if (U_SUCCESS(status)) {
int index = 0;
while (!(*icuTZ == '\0' || *icuTZ == ' ')) {
tmpid[index++] = (char)(*icuTZ++);
}
tmpid[index] = '\0';
}
}
}
if (tmpid[0] != 0) {
icuid = uprv_strdup(tmpid);
}
return icuid;
}
U_NAMESPACE_END
#endif