]>
Commit | Line | Data |
---|---|---|
73c04bcf A |
1 | /* |
2 | ******************************************************************************** | |
2ca993e8 | 3 | * Copyright (C) 2005-2016, International Business Machines |
73c04bcf A |
4 | * Corporation and others. All Rights Reserved. |
5 | ******************************************************************************** | |
6 | * | |
7 | * File WINDTTST.CPP | |
8 | * | |
9 | ******************************************************************************** | |
10 | */ | |
11 | ||
12 | #include "unicode/utypes.h" | |
13 | ||
4388f060 | 14 | #if U_PLATFORM_HAS_WIN32_API |
73c04bcf A |
15 | |
16 | #if !UCONFIG_NO_FORMATTING | |
17 | ||
18 | #include "unicode/format.h" | |
19 | #include "unicode/numfmt.h" | |
20 | #include "unicode/locid.h" | |
21 | #include "unicode/ustring.h" | |
22 | #include "unicode/testlog.h" | |
23 | #include "unicode/utmscale.h" | |
24 | ||
25 | #include "windtfmt.h" | |
26 | #include "winutil.h" | |
27 | #include "windttst.h" | |
28 | ||
29 | #include "cmemory.h" | |
30 | #include "cstring.h" | |
31 | #include "locmap.h" | |
729e4ab9 | 32 | #include "wintzimpl.h" |
73c04bcf A |
33 | |
34 | # define WIN32_LEAN_AND_MEAN | |
35 | # define VC_EXTRALEAN | |
36 | # define NOUSER | |
37 | # define NOSERVICE | |
38 | # define NOIME | |
39 | # define NOMCX | |
40 | # include <windows.h> | |
41 | ||
73c04bcf A |
42 | static const char *getCalendarType(int32_t type) |
43 | { | |
44 | switch (type) | |
45 | { | |
46 | case 1: | |
47 | case 2: | |
48 | return "@calendar=gregorian"; | |
49 | ||
50 | case 3: | |
51 | return "@calendar=japanese"; | |
52 | ||
53 | case 6: | |
54 | return "@calendar=islamic"; | |
55 | ||
56 | case 7: | |
57 | return "@calendar=buddhist"; | |
58 | ||
59 | case 8: | |
60 | return "@calendar=hebrew"; | |
61 | ||
62 | default: | |
63 | return ""; | |
64 | } | |
65 | } | |
66 | ||
67 | void Win32DateTimeTest::testLocales(TestLog *log) | |
68 | { | |
69 | SYSTEMTIME winNow; | |
70 | UDate icuNow = 0; | |
71 | SYSTEMTIME st; | |
72 | FILETIME ft; | |
73 | UnicodeString zoneID; | |
74 | const TimeZone *tz = TimeZone::createDefault(); | |
75 | TIME_ZONE_INFORMATION tzi; | |
76 | ||
73c04bcf A |
77 | tz->getID(zoneID); |
78 | if (! uprv_getWindowsTimeZoneInfo(&tzi, zoneID.getBuffer(), zoneID.length())) { | |
79 | UBool found = FALSE; | |
80 | int32_t ec = TimeZone::countEquivalentIDs(zoneID); | |
81 | ||
82 | for (int z = 0; z < ec; z += 1) { | |
83 | UnicodeString equiv = TimeZone::getEquivalentID(zoneID, z); | |
84 | ||
85 | if (found = uprv_getWindowsTimeZoneInfo(&tzi, equiv.getBuffer(), equiv.length())) { | |
86 | break; | |
87 | } | |
88 | } | |
89 | ||
90 | if (! found) { | |
91 | GetTimeZoneInformation(&tzi); | |
92 | } | |
93 | } | |
94 | ||
95 | GetSystemTime(&st); | |
96 | SystemTimeToFileTime(&st, &ft); | |
97 | SystemTimeToTzSpecificLocalTime(&tzi, &st, &winNow); | |
98 | ||
99 | int64_t wftNow = ((int64_t) ft.dwHighDateTime << 32) + ft.dwLowDateTime; | |
100 | UErrorCode status = U_ZERO_ERROR; | |
101 | ||
102 | int64_t udtsNow = utmscale_fromInt64(wftNow, UDTS_WINDOWS_FILE_TIME, &status); | |
103 | ||
104 | icuNow = (UDate) utmscale_toInt64(udtsNow, UDTS_ICU4C_TIME, &status); | |
105 | ||
106 | int32_t lcidCount = 0; | |
107 | Win32Utilities::LCIDRecord *lcidRecords = Win32Utilities::getLocales(lcidCount); | |
108 | ||
109 | for(int i = 0; i < lcidCount; i += 1) { | |
110 | UErrorCode status = U_ZERO_ERROR; | |
111 | WCHAR longDateFormat[81], longTimeFormat[81], wdBuffer[256], wtBuffer[256]; | |
112 | int32_t calType = 0; | |
113 | ||
729e4ab9 A |
114 | // NULL localeID means ICU didn't recognize this locale |
115 | if (lcidRecords[i].localeID == NULL) { | |
116 | continue; | |
117 | } | |
73c04bcf A |
118 | |
119 | GetLocaleInfoW(lcidRecords[i].lcid, LOCALE_SLONGDATE, longDateFormat, 81); | |
120 | GetLocaleInfoW(lcidRecords[i].lcid, LOCALE_STIMEFORMAT, longTimeFormat, 81); | |
121 | GetLocaleInfoW(lcidRecords[i].lcid, LOCALE_RETURN_NUMBER|LOCALE_ICALENDARTYPE, (LPWSTR) calType, sizeof(int32_t)); | |
122 | ||
123 | char localeID[64]; | |
124 | ||
125 | uprv_strcpy(localeID, lcidRecords[i].localeID); | |
126 | uprv_strcat(localeID, getCalendarType(calType)); | |
127 | ||
128 | UnicodeString ubBuffer, udBuffer, utBuffer; | |
129 | Locale ulocale(localeID); | |
130 | int32_t wdLength, wtLength; | |
131 | ||
2ca993e8 A |
132 | wdLength = GetDateFormatW(lcidRecords[i].lcid, DATE_LONGDATE, &winNow, NULL, wdBuffer, UPRV_LENGTHOF(wdBuffer)); |
133 | wtLength = GetTimeFormatW(lcidRecords[i].lcid, 0, &winNow, NULL, wtBuffer, UPRV_LENGTHOF(wtBuffer)); | |
73c04bcf A |
134 | |
135 | if (uprv_strchr(localeID, '@') > 0) { | |
136 | uprv_strcat(localeID, ";"); | |
137 | } else { | |
138 | uprv_strcat(localeID, "@"); | |
139 | } | |
140 | ||
141 | uprv_strcat(localeID, "compat=host"); | |
142 | ||
143 | Locale wlocale(localeID); | |
144 | DateFormat *wbf = DateFormat::createDateTimeInstance(DateFormat::kFull, DateFormat::kFull, wlocale); | |
145 | DateFormat *wdf = DateFormat::createDateInstance(DateFormat::kFull, wlocale); | |
146 | DateFormat *wtf = DateFormat::createTimeInstance(DateFormat::kFull, wlocale); | |
147 | ||
148 | wbf->format(icuNow, ubBuffer); | |
149 | wdf->format(icuNow, udBuffer); | |
150 | wtf->format(icuNow, utBuffer); | |
151 | ||
152 | if (ubBuffer.indexOf(wdBuffer, wdLength - 1, 0) < 0) { | |
153 | UnicodeString baseName(wlocale.getBaseName()); | |
154 | UnicodeString expected(wdBuffer); | |
155 | ||
156 | log->errln("DateTime format error for locale " + baseName + ": expected date \"" + expected + | |
157 | "\" got \"" + ubBuffer + "\""); | |
158 | } | |
159 | ||
160 | if (ubBuffer.indexOf(wtBuffer, wtLength - 1, 0) < 0) { | |
161 | UnicodeString baseName(wlocale.getBaseName()); | |
162 | UnicodeString expected(wtBuffer); | |
163 | ||
164 | log->errln("DateTime format error for locale " + baseName + ": expected time \"" + expected + | |
165 | "\" got \"" + ubBuffer + "\""); | |
166 | } | |
167 | ||
168 | if (udBuffer.compare(wdBuffer) != 0) { | |
169 | UnicodeString baseName(wlocale.getBaseName()); | |
170 | UnicodeString expected(wdBuffer); | |
171 | ||
172 | log->errln("Date format error for locale " + baseName + ": expected \"" + expected + | |
173 | "\" got \"" + udBuffer + "\""); | |
174 | } | |
175 | ||
176 | if (utBuffer.compare(wtBuffer) != 0) { | |
177 | UnicodeString baseName(wlocale.getBaseName()); | |
178 | UnicodeString expected(wtBuffer); | |
179 | ||
180 | log->errln("Time format error for locale " + baseName + ": expected \"" + expected + | |
181 | "\" got \"" + utBuffer + "\""); | |
182 | } | |
183 | delete wbf; | |
184 | delete wdf; | |
185 | delete wtf; | |
186 | } | |
187 | ||
188 | Win32Utilities::freeLocales(lcidRecords); | |
189 | delete tz; | |
190 | } | |
191 | ||
192 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
193 | ||
4388f060 | 194 | #endif /* U_PLATFORM_HAS_WIN32_API */ |