2 ********************************************************************************
3 * Copyright (C) 2005-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
9 ********************************************************************************
12 #include "unicode/utypes.h"
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"
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
;
79 uprv_memset(&tzi
, 0, sizeof(tzi
));
81 if (! uprv_getWindowsTimeZoneInfo(&tzi
, zoneID
.getBuffer(), zoneID
.length())) {
83 int32_t ec
= TimeZone::countEquivalentIDs(zoneID
);
85 for (int z
= 0; z
< ec
; z
+= 1) {
86 UnicodeString equiv
= TimeZone::getEquivalentID(zoneID
, z
);
88 if (found
= uprv_getWindowsTimeZoneInfo(&tzi
, equiv
.getBuffer(), equiv
.length())) {
94 GetTimeZoneInformation(&tzi
);
99 SystemTimeToFileTime(&st
, &ft
);
100 SystemTimeToTzSpecificLocalTime(&tzi
, &st
, &winNow
);
102 int64_t wftNow
= ((int64_t) ft
.dwHighDateTime
<< 32) + ft
.dwLowDateTime
;
103 UErrorCode status
= U_ZERO_ERROR
;
105 int64_t udtsNow
= utmscale_fromInt64(wftNow
, UDTS_WINDOWS_FILE_TIME
, &status
);
107 icuNow
= (UDate
) utmscale_toInt64(udtsNow
, UDTS_ICU4C_TIME
, &status
);
109 int32_t lcidCount
= 0;
110 Win32Utilities::LCIDRecord
*lcidRecords
= Win32Utilities::getLocales(lcidCount
);
112 for(int i
= 0; i
< lcidCount
; i
+= 1) {
113 UErrorCode status
= U_ZERO_ERROR
;
114 WCHAR longDateFormat
[81], longTimeFormat
[81], wdBuffer
[256], wtBuffer
[256];
117 // NULL localeID means ICU didn't recognize this locale
118 if (lcidRecords
[i
].localeID
== NULL
) {
122 GetLocaleInfoW(lcidRecords
[i
].lcid
, LOCALE_SLONGDATE
, longDateFormat
, 81);
123 GetLocaleInfoW(lcidRecords
[i
].lcid
, LOCALE_STIMEFORMAT
, longTimeFormat
, 81);
124 GetLocaleInfoW(lcidRecords
[i
].lcid
, LOCALE_RETURN_NUMBER
|LOCALE_ICALENDARTYPE
, (LPWSTR
) calType
, sizeof(int32_t));
128 uprv_strcpy(localeID
, lcidRecords
[i
].localeID
);
129 uprv_strcat(localeID
, getCalendarType(calType
));
131 UnicodeString ubBuffer
, udBuffer
, utBuffer
;
132 Locale
ulocale(localeID
);
133 int32_t wdLength
, wtLength
;
135 wdLength
= GetDateFormatW(lcidRecords
[i
].lcid
, DATE_LONGDATE
, &winNow
, NULL
, wdBuffer
, ARRAY_SIZE(wdBuffer
));
136 wtLength
= GetTimeFormatW(lcidRecords
[i
].lcid
, 0, &winNow
, NULL
, wtBuffer
, ARRAY_SIZE(wtBuffer
));
138 if (uprv_strchr(localeID
, '@') > 0) {
139 uprv_strcat(localeID
, ";");
141 uprv_strcat(localeID
, "@");
144 uprv_strcat(localeID
, "compat=host");
146 Locale
wlocale(localeID
);
147 DateFormat
*wbf
= DateFormat::createDateTimeInstance(DateFormat::kFull
, DateFormat::kFull
, wlocale
);
148 DateFormat
*wdf
= DateFormat::createDateInstance(DateFormat::kFull
, wlocale
);
149 DateFormat
*wtf
= DateFormat::createTimeInstance(DateFormat::kFull
, wlocale
);
151 wbf
->format(icuNow
, ubBuffer
);
152 wdf
->format(icuNow
, udBuffer
);
153 wtf
->format(icuNow
, utBuffer
);
155 if (ubBuffer
.indexOf(wdBuffer
, wdLength
- 1, 0) < 0) {
156 UnicodeString
baseName(wlocale
.getBaseName());
157 UnicodeString
expected(wdBuffer
);
159 log
->errln("DateTime format error for locale " + baseName
+ ": expected date \"" + expected
+
160 "\" got \"" + ubBuffer
+ "\"");
163 if (ubBuffer
.indexOf(wtBuffer
, wtLength
- 1, 0) < 0) {
164 UnicodeString
baseName(wlocale
.getBaseName());
165 UnicodeString
expected(wtBuffer
);
167 log
->errln("DateTime format error for locale " + baseName
+ ": expected time \"" + expected
+
168 "\" got \"" + ubBuffer
+ "\"");
171 if (udBuffer
.compare(wdBuffer
) != 0) {
172 UnicodeString
baseName(wlocale
.getBaseName());
173 UnicodeString
expected(wdBuffer
);
175 log
->errln("Date format error for locale " + baseName
+ ": expected \"" + expected
+
176 "\" got \"" + udBuffer
+ "\"");
179 if (utBuffer
.compare(wtBuffer
) != 0) {
180 UnicodeString
baseName(wlocale
.getBaseName());
181 UnicodeString
expected(wtBuffer
);
183 log
->errln("Time format error for locale " + baseName
+ ": expected \"" + expected
+
184 "\" got \"" + utBuffer
+ "\"");
191 Win32Utilities::freeLocales(lcidRecords
);
195 #endif /* #if !UCONFIG_NO_FORMATTING */
197 #endif /* #ifdef U_WINDOWS */