2 ********************************************************************************
3 * Copyright (C) 2005-2016, 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 static const char *getCalendarType(int32_t type
)
48 return "@calendar=gregorian";
51 return "@calendar=japanese";
54 return "@calendar=islamic";
57 return "@calendar=buddhist";
60 return "@calendar=hebrew";
67 void Win32DateTimeTest::testLocales(TestLog
*log
)
74 const TimeZone
*tz
= TimeZone::createDefault();
75 TIME_ZONE_INFORMATION tzi
;
78 if (! uprv_getWindowsTimeZoneInfo(&tzi
, zoneID
.getBuffer(), zoneID
.length())) {
80 int32_t ec
= TimeZone::countEquivalentIDs(zoneID
);
82 for (int z
= 0; z
< ec
; z
+= 1) {
83 UnicodeString equiv
= TimeZone::getEquivalentID(zoneID
, z
);
85 if (found
= uprv_getWindowsTimeZoneInfo(&tzi
, equiv
.getBuffer(), equiv
.length())) {
91 GetTimeZoneInformation(&tzi
);
96 SystemTimeToFileTime(&st
, &ft
);
97 SystemTimeToTzSpecificLocalTime(&tzi
, &st
, &winNow
);
99 int64_t wftNow
= ((int64_t) ft
.dwHighDateTime
<< 32) + ft
.dwLowDateTime
;
100 UErrorCode status
= U_ZERO_ERROR
;
102 int64_t udtsNow
= utmscale_fromInt64(wftNow
, UDTS_WINDOWS_FILE_TIME
, &status
);
104 icuNow
= (UDate
) utmscale_toInt64(udtsNow
, UDTS_ICU4C_TIME
, &status
);
106 int32_t lcidCount
= 0;
107 Win32Utilities::LCIDRecord
*lcidRecords
= Win32Utilities::getLocales(lcidCount
);
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];
114 // NULL localeID means ICU didn't recognize this locale
115 if (lcidRecords
[i
].localeID
== NULL
) {
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));
125 uprv_strcpy(localeID
, lcidRecords
[i
].localeID
);
126 uprv_strcat(localeID
, getCalendarType(calType
));
128 UnicodeString ubBuffer
, udBuffer
, utBuffer
;
129 Locale
ulocale(localeID
);
130 int32_t wdLength
, wtLength
;
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
));
135 if (uprv_strchr(localeID
, '@') > 0) {
136 uprv_strcat(localeID
, ";");
138 uprv_strcat(localeID
, "@");
141 uprv_strcat(localeID
, "compat=host");
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
);
148 wbf
->format(icuNow
, ubBuffer
);
149 wdf
->format(icuNow
, udBuffer
);
150 wtf
->format(icuNow
, utBuffer
);
152 if (ubBuffer
.indexOf(wdBuffer
, wdLength
- 1, 0) < 0) {
153 UnicodeString
baseName(wlocale
.getBaseName());
154 UnicodeString
expected(wdBuffer
);
156 log
->errln("DateTime format error for locale " + baseName
+ ": expected date \"" + expected
+
157 "\" got \"" + ubBuffer
+ "\"");
160 if (ubBuffer
.indexOf(wtBuffer
, wtLength
- 1, 0) < 0) {
161 UnicodeString
baseName(wlocale
.getBaseName());
162 UnicodeString
expected(wtBuffer
);
164 log
->errln("DateTime format error for locale " + baseName
+ ": expected time \"" + expected
+
165 "\" got \"" + ubBuffer
+ "\"");
168 if (udBuffer
.compare(wdBuffer
) != 0) {
169 UnicodeString
baseName(wlocale
.getBaseName());
170 UnicodeString
expected(wdBuffer
);
172 log
->errln("Date format error for locale " + baseName
+ ": expected \"" + expected
+
173 "\" got \"" + udBuffer
+ "\"");
176 if (utBuffer
.compare(wtBuffer
) != 0) {
177 UnicodeString
baseName(wlocale
.getBaseName());
178 UnicodeString
expected(wtBuffer
);
180 log
->errln("Time format error for locale " + baseName
+ ": expected \"" + expected
+
181 "\" got \"" + utBuffer
+ "\"");
188 Win32Utilities::freeLocales(lcidRecords
);
192 #endif /* #if !UCONFIG_NO_FORMATTING */
194 #endif /* U_PLATFORM_HAS_WIN32_API */