2 ********************************************************************************
3 * Copyright (C) 2005-2011, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
9 ********************************************************************************
12 #include "unicode/utypes.h"
14 #if U_PLATFORM_HAS_WIN32_API
16 #if !UCONFIG_NO_FORMATTING
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"
32 #include "wintzimpl.h"
34 # define WIN32_LEAN_AND_MEAN
42 #define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
44 static const char *getCalendarType(int32_t type)
50 return "@calendar=gregorian";
53 return "@calendar=japanese";
56 return "@calendar=islamic";
59 return "@calendar=buddhist";
62 return "@calendar=hebrew";
69 void Win32DateTimeTest::testLocales(TestLog *log)
76 const TimeZone *tz = TimeZone::createDefault();
77 TIME_ZONE_INFORMATION tzi;
80 if (! uprv_getWindowsTimeZoneInfo(&tzi, zoneID.getBuffer(), zoneID.length())) {
82 int32_t ec = TimeZone::countEquivalentIDs(zoneID);
84 for (int z = 0; z < ec; z += 1) {
85 UnicodeString equiv = TimeZone::getEquivalentID(zoneID, z);
87 if (found = uprv_getWindowsTimeZoneInfo(&tzi, equiv.getBuffer(), equiv.length())) {
93 GetTimeZoneInformation(&tzi);
98 SystemTimeToFileTime(&st, &ft);
99 SystemTimeToTzSpecificLocalTime(&tzi, &st, &winNow);
101 int64_t wftNow = ((int64_t) ft.dwHighDateTime << 32) + ft.dwLowDateTime;
102 UErrorCode status = U_ZERO_ERROR;
104 int64_t udtsNow = utmscale_fromInt64(wftNow, UDTS_WINDOWS_FILE_TIME, &status);
106 icuNow = (UDate) utmscale_toInt64(udtsNow, UDTS_ICU4C_TIME, &status);
108 int32_t lcidCount = 0;
109 Win32Utilities::LCIDRecord *lcidRecords = Win32Utilities::getLocales(lcidCount);
111 for(int i = 0; i < lcidCount; i += 1) {
112 UErrorCode status = U_ZERO_ERROR;
113 WCHAR longDateFormat[81], longTimeFormat[81], wdBuffer[256], wtBuffer[256];
116 // NULL localeID means ICU didn't recognize this locale
117 if (lcidRecords[i].localeID == NULL) {
121 GetLocaleInfoW(lcidRecords[i].lcid, LOCALE_SLONGDATE, longDateFormat, 81);
122 GetLocaleInfoW(lcidRecords[i].lcid, LOCALE_STIMEFORMAT, longTimeFormat, 81);
123 GetLocaleInfoW(lcidRecords[i].lcid, LOCALE_RETURN_NUMBER|LOCALE_ICALENDARTYPE, (LPWSTR) calType, sizeof(int32_t));
127 uprv_strcpy(localeID, lcidRecords[i].localeID);
128 uprv_strcat(localeID, getCalendarType(calType));
130 UnicodeString ubBuffer, udBuffer, utBuffer;
131 Locale ulocale(localeID);
132 int32_t wdLength, wtLength;
134 wdLength = GetDateFormatW(lcidRecords[i].lcid, DATE_LONGDATE, &winNow, NULL, wdBuffer, ARRAY_SIZE(wdBuffer));
135 wtLength = GetTimeFormatW(lcidRecords[i].lcid, 0, &winNow, NULL, wtBuffer, ARRAY_SIZE(wtBuffer));
137 if (uprv_strchr(localeID, '@') > 0) {
138 uprv_strcat(localeID, ";");
140 uprv_strcat(localeID, "@");
143 uprv_strcat(localeID, "compat=host");
145 Locale wlocale(localeID);
146 DateFormat *wbf = DateFormat::createDateTimeInstance(DateFormat::kFull, DateFormat::kFull, wlocale);
147 DateFormat *wdf = DateFormat::createDateInstance(DateFormat::kFull, wlocale);
148 DateFormat *wtf = DateFormat::createTimeInstance(DateFormat::kFull, wlocale);
150 wbf->format(icuNow, ubBuffer);
151 wdf->format(icuNow, udBuffer);
152 wtf->format(icuNow, utBuffer);
154 if (ubBuffer.indexOf(wdBuffer, wdLength - 1, 0) < 0) {
155 UnicodeString baseName(wlocale.getBaseName());
156 UnicodeString expected(wdBuffer);
158 log->errln("DateTime format error for locale " + baseName + ": expected date \"" + expected +
159 "\" got \"" + ubBuffer + "\"");
162 if (ubBuffer.indexOf(wtBuffer, wtLength - 1, 0) < 0) {
163 UnicodeString baseName(wlocale.getBaseName());
164 UnicodeString expected(wtBuffer);
166 log->errln("DateTime format error for locale " + baseName + ": expected time \"" + expected +
167 "\" got \"" + ubBuffer + "\"");
170 if (udBuffer.compare(wdBuffer) != 0) {
171 UnicodeString baseName(wlocale.getBaseName());
172 UnicodeString expected(wdBuffer);
174 log->errln("Date format error for locale " + baseName + ": expected \"" + expected +
175 "\" got \"" + udBuffer + "\"");
178 if (utBuffer.compare(wtBuffer) != 0) {
179 UnicodeString baseName(wlocale.getBaseName());
180 UnicodeString expected(wtBuffer);
182 log->errln("Time format error for locale " + baseName + ": expected \"" + expected +
183 "\" got \"" + utBuffer + "\"");
190 Win32Utilities::freeLocales(lcidRecords);
194 #endif /* #if !UCONFIG_NO_FORMATTING */
196 #endif /* U_PLATFORM_HAS_WIN32_API */