]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/wintz.cpp
ICU-64232.0.1.tar.gz
[apple/icu.git] / icuSources / common / wintz.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
73c04bcf
A
3/*
4********************************************************************************
2ca993e8 5* Copyright (C) 2005-2015, International Business Machines
73c04bcf
A
6* Corporation and others. All Rights Reserved.
7********************************************************************************
8*
9* File WINTZ.CPP
10*
11********************************************************************************
12*/
13
14#include "unicode/utypes.h"
15
3d1f044b 16#if U_PLATFORM_USES_ONLY_WIN32_API
73c04bcf
A
17
18#include "wintz.h"
73c04bcf
A
19#include "cmemory.h"
20#include "cstring.h"
21
729e4ab9 22#include "unicode/ures.h"
b331163b 23#include "unicode/ustring.h"
3d1f044b 24#include "uresimp.h"
73c04bcf 25
f3c0d7a5 26#ifndef WIN32_LEAN_AND_MEAN
73c04bcf 27# define WIN32_LEAN_AND_MEAN
f3c0d7a5 28#endif
73c04bcf
A
29# define VC_EXTRALEAN
30# define NOUSER
31# define NOSERVICE
32# define NOIME
33# define NOMCX
34#include <windows.h>
35
3d1f044b 36U_NAMESPACE_BEGIN
4388f060 37
3d1f044b
A
38// The max size of TimeZoneKeyName is 128, defined in DYNAMIC_TIME_ZONE_INFORMATION
39#define MAX_TIMEZONE_ID_LENGTH 128
73c04bcf
A
40
41/**
3d1f044b
A
42* Main Windows time zone detection function.
43* Returns the Windows time zone converted to an ICU time zone as a heap-allocated buffer, or nullptr upon failure.
44* Note: We use the Win32 API GetDynamicTimeZoneInformation to get the current time zone info.
45* This API returns a non-localized time zone name, which we can then map to an ICU time zone name.
73c04bcf 46*/
3d1f044b
A
47U_INTERNAL const char* U_EXPORT2
48uprv_detectWindowsTimeZone()
f3c0d7a5 49{
729e4ab9 50 UErrorCode status = U_ZERO_ERROR;
3d1f044b
A
51 char* icuid = nullptr;
52 char dynamicTZKeyName[MAX_TIMEZONE_ID_LENGTH];
53 char tmpid[MAX_TIMEZONE_ID_LENGTH];
4388f060 54 int32_t len;
3d1f044b 55 int id = GEOID_NOT_AVAILABLE;
51004dcb 56 int errorCode;
3d1f044b
A
57 wchar_t ISOcodeW[3] = {}; /* 2 letter ISO code in UTF-16 */
58 char ISOcode[3] = {}; /* 2 letter ISO code in UTF-8 */
729e4ab9 59
3d1f044b
A
60 DYNAMIC_TIME_ZONE_INFORMATION dynamicTZI;
61 uprv_memset(&dynamicTZI, 0, sizeof(dynamicTZI));
62 uprv_memset(dynamicTZKeyName, 0, sizeof(dynamicTZKeyName));
63 uprv_memset(tmpid, 0, sizeof(tmpid));
73c04bcf 64
3d1f044b
A
65 /* Obtain TIME_ZONE_INFORMATION from the API and get the non-localized time zone name. */
66 if (TIME_ZONE_ID_INVALID == GetDynamicTimeZoneInformation(&dynamicTZI)) {
67 return nullptr;
68 }
69
70 id = GetUserGeoID(GEOCLASS_NATION);
71 errorCode = GetGeoInfoW(id, GEO_ISO2, ISOcodeW, 3, 0);
b331163b 72
3d1f044b
A
73 // convert from wchar_t* (UTF-16 on Windows) to char* (UTF-8).
74 u_strToUTF8(ISOcode, UPRV_LENGTHOF(ISOcode), nullptr,
75 reinterpret_cast<const UChar*>(ISOcodeW), UPRV_LENGTHOF(ISOcodeW), &status);
73c04bcf 76
3d1f044b
A
77 LocalUResourceBundlePointer bundle(ures_openDirect(nullptr, "windowsZones", &status));
78 ures_getByKey(bundle.getAlias(), "mapTimezones", bundle.getAlias(), &status);
4388f060 79
3d1f044b
A
80 // convert from wchar_t* (UTF-16 on Windows) to char* (UTF-8).
81 u_strToUTF8(dynamicTZKeyName, UPRV_LENGTHOF(dynamicTZKeyName), nullptr,
82 reinterpret_cast<const UChar*>(dynamicTZI.TimeZoneKeyName), -1, &status);
4388f060 83
3d1f044b
A
84 if (U_FAILURE(status)) {
85 return nullptr;
86 }
51004dcb 87
3d1f044b
A
88 if (dynamicTZI.TimeZoneKeyName[0] != 0) {
89 StackUResourceBundle winTZ;
90 ures_getByKey(bundle.getAlias(), dynamicTZKeyName, winTZ.getAlias(), &status);
73c04bcf 91
3d1f044b
A
92 if (U_SUCCESS(status)) {
93 const UChar* icuTZ = nullptr;
94 if (errorCode != 0) {
95 icuTZ = ures_getStringByKey(winTZ.getAlias(), ISOcode, &len, &status);
f3c0d7a5 96 }
3d1f044b 97 if (errorCode == 0 || icuTZ == nullptr) {
f3c0d7a5
A
98 /* fallback to default "001" and reset status */
99 status = U_ZERO_ERROR;
3d1f044b 100 icuTZ = ures_getStringByKey(winTZ.getAlias(), "001", &len, &status);
f3c0d7a5 101 }
51004dcb 102
3d1f044b
A
103 if (U_SUCCESS(status)) {
104 int index = 0;
b331163b 105
3d1f044b
A
106 while (!(*icuTZ == '\0' || *icuTZ == ' ')) {
107 // time zone IDs only contain ASCII invariant characters.
108 tmpid[index++] = (char)(*icuTZ++);
73c04bcf 109 }
3d1f044b 110 tmpid[index] = '\0';
b331163b 111 }
73c04bcf
A
112 }
113 }
114
3d1f044b
A
115 // Copy the timezone ID to icuid to be returned.
116 if (tmpid[0] != 0) {
117 icuid = uprv_strdup(tmpid);
4388f060
A
118 }
119
729e4ab9 120 return icuid;
73c04bcf
A
121}
122
3d1f044b
A
123U_NAMESPACE_END
124#endif /* U_PLATFORM_USES_ONLY_WIN32_API */