]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/windtfmt.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / i18n / windtfmt.cpp
1 /*
2 ********************************************************************************
3 * Copyright (C) 2005-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
6 *
7 * File WINDTFMT.CPP
8 *
9 ********************************************************************************
10 */
11
12 #include "unicode/utypes.h"
13
14 #ifdef U_WINDOWS
15
16 #if !UCONFIG_NO_FORMATTING
17
18 #include "unicode/ures.h"
19 #include "unicode/format.h"
20 #include "unicode/fmtable.h"
21 #include "unicode/datefmt.h"
22 #include "unicode/msgfmt.h"
23 #include "unicode/calendar.h"
24 #include "unicode/gregocal.h"
25 #include "unicode/locid.h"
26 #include "unicode/unistr.h"
27 #include "unicode/ustring.h"
28 #include "unicode/timezone.h"
29 #include "unicode/utmscale.h"
30
31 #include "cmemory.h"
32 #include "uresimp.h"
33 #include "windtfmt.h"
34 #include "wintz.h"
35
36 # define WIN32_LEAN_AND_MEAN
37 # define VC_EXTRALEAN
38 # define NOUSER
39 # define NOSERVICE
40 # define NOIME
41 # define NOMCX
42 #include <windows.h>
43
44 U_NAMESPACE_BEGIN
45
46 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32DateFormat)
47
48 #define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
49
50 #define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type))
51 #define DELETE_ARRAY(array) uprv_free((void *) (array))
52
53 #define STACK_BUFFER_SIZE 64
54
55 UnicodeString *getTimeDateFormat(const Calendar *cal, const Locale *locale, UErrorCode &status)
56 {
57 UnicodeString *result = NULL;
58 const char *type = cal->getType();
59 const char *base = locale->getBaseName();
60 UResourceBundle *topBundle = ures_open((char *) 0, base, &status);
61 UResourceBundle *calBundle = ures_getByKey(topBundle, "calendar", NULL, &status);
62 UResourceBundle *typBundle = ures_getByKeyWithFallback(calBundle, type, NULL, &status);
63 UResourceBundle *patBundle = ures_getByKeyWithFallback(typBundle, "DateTimePatterns", NULL, &status);
64
65 if (status == U_MISSING_RESOURCE_ERROR) {
66 status = U_ZERO_ERROR;
67 typBundle = ures_getByKeyWithFallback(calBundle, "gregorian", typBundle, &status);
68 patBundle = ures_getByKeyWithFallback(typBundle, "DateTimePatterns", patBundle, &status);
69 }
70
71 if (U_FAILURE(status)) {
72 UChar defaultPattern[] = {0x007B, 0x0031, 0x007D, 0x0020, 0x007B, 0x0030, 0x007D, 0x0000}; // "{1} {0}"
73 return new UnicodeString(defaultPattern, ARRAY_SIZE(defaultPattern));
74 }
75
76 int32_t resStrLen = 0;
77 const UChar *resStr = ures_getStringByIndex(patBundle, (int32_t)DateFormat::kDateTime, &resStrLen, &status);
78
79 result = new UnicodeString(TRUE, resStr, resStrLen);
80
81 ures_close(patBundle);
82 ures_close(typBundle);
83 ures_close(calBundle);
84 ures_close(topBundle);
85
86 return result;
87 }
88
89 // TODO: Range-check timeStyle, dateStyle
90 Win32DateFormat::Win32DateFormat(DateFormat::EStyle timeStyle, DateFormat::EStyle dateStyle, const Locale &locale, UErrorCode &status)
91 : DateFormat(), fDateTimeMsg(NULL), fTimeStyle(timeStyle), fDateStyle(dateStyle), fLocale(&locale), fZoneID()
92 {
93 if (U_SUCCESS(status)) {
94 fLCID = locale.getLCID();
95 fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1);
96 uprv_memset(fTZI, 0, sizeof(TIME_ZONE_INFORMATION));
97 adoptCalendar(Calendar::createInstance(locale, status));
98 }
99 }
100
101 Win32DateFormat::Win32DateFormat(const Win32DateFormat &other)
102 : DateFormat(other)
103 {
104 *this = other;
105 }
106
107 Win32DateFormat::~Win32DateFormat()
108 {
109 // delete fCalendar;
110 uprv_free(fTZI);
111 delete fDateTimeMsg;
112 }
113
114 Win32DateFormat &Win32DateFormat::operator=(const Win32DateFormat &other)
115 {
116 // The following handles fCalendar
117 DateFormat::operator=(other);
118
119 // delete fCalendar;
120
121 this->fDateTimeMsg = other.fDateTimeMsg;
122 this->fTimeStyle = other.fTimeStyle;
123 this->fDateStyle = other.fDateStyle;
124 this->fLCID = other.fLCID;
125 // this->fCalendar = other.fCalendar->clone();
126 this->fZoneID = other.fZoneID;
127
128 this->fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1);
129 *this->fTZI = *other.fTZI;
130
131 return *this;
132 }
133
134 Format *Win32DateFormat::clone(void) const
135 {
136 return new Win32DateFormat(*this);
137 }
138
139 // TODO: Is just ignoring pos the right thing?
140 UnicodeString &Win32DateFormat::format(Calendar &cal, UnicodeString &appendTo, FieldPosition &pos) const
141 {
142 FILETIME ft;
143 SYSTEMTIME st_gmt;
144 SYSTEMTIME st_local;
145 TIME_ZONE_INFORMATION tzi = *fTZI;
146 UErrorCode status = U_ZERO_ERROR;
147 const TimeZone &tz = cal.getTimeZone();
148 int64_t uct, uft;
149
150 setTimeZoneInfo(&tzi, tz);
151
152 uct = utmscale_fromInt64((int64_t) cal.getTime(status), UDTS_ICU4C_TIME, &status);
153 uft = utmscale_toInt64(uct, UDTS_WINDOWS_FILE_TIME, &status);
154
155 ft.dwLowDateTime = (DWORD) (uft & 0xFFFFFFFF);
156 ft.dwHighDateTime = (DWORD) ((uft >> 32) & 0xFFFFFFFF);
157
158 FileTimeToSystemTime(&ft, &st_gmt);
159 SystemTimeToTzSpecificLocalTime(&tzi, &st_gmt, &st_local);
160
161
162 if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) {
163 UnicodeString *date = new UnicodeString();
164 UnicodeString *time = new UnicodeString();
165 UnicodeString *pattern = fDateTimeMsg;
166 Formattable timeDateArray[2];
167
168 formatDate(&st_local, *date);
169 formatTime(&st_local, *time);
170
171 timeDateArray[0].adoptString(time);
172 timeDateArray[1].adoptString(date);
173
174 if (strcmp(fCalendar->getType(), cal.getType()) != 0) {
175 pattern = getTimeDateFormat(&cal, fLocale, status);
176 }
177
178 MessageFormat::format(*pattern, timeDateArray, 2, appendTo, status);
179 } else if (fDateStyle != DateFormat::kNone) {
180 formatDate(&st_local, appendTo);
181 } else if (fTimeStyle != DateFormat::kNone) {
182 formatTime(&st_local, appendTo);
183 }
184
185 return appendTo;
186 }
187
188 void Win32DateFormat::parse(const UnicodeString& text, Calendar& cal, ParsePosition& pos) const
189 {
190 pos.setErrorIndex(pos.getIndex());
191 }
192
193 void Win32DateFormat::adoptCalendar(Calendar *newCalendar)
194 {
195 if (fCalendar == NULL || strcmp(fCalendar->getType(), newCalendar->getType()) != 0) {
196 UErrorCode status = U_ZERO_ERROR;
197
198 if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) {
199 delete fDateTimeMsg;
200 fDateTimeMsg = getTimeDateFormat(newCalendar, fLocale, status);
201 }
202 }
203
204 delete fCalendar;
205 fCalendar = newCalendar;
206
207 fZoneID = setTimeZoneInfo(fTZI, fCalendar->getTimeZone());
208 }
209
210 void Win32DateFormat::setCalendar(const Calendar &newCalendar)
211 {
212 adoptCalendar(newCalendar.clone());
213 }
214
215 void Win32DateFormat::adoptTimeZone(TimeZone *zoneToAdopt)
216 {
217 fZoneID = setTimeZoneInfo(fTZI, *zoneToAdopt);
218 fCalendar->adoptTimeZone(zoneToAdopt);
219 }
220
221 void Win32DateFormat::setTimeZone(const TimeZone& zone)
222 {
223 fZoneID = setTimeZoneInfo(fTZI, zone);
224 fCalendar->setTimeZone(zone);
225 }
226
227 static const DWORD dfFlags[] = {DATE_LONGDATE, DATE_LONGDATE, DATE_SHORTDATE, DATE_SHORTDATE};
228
229 void Win32DateFormat::formatDate(const SYSTEMTIME *st, UnicodeString &appendTo) const
230 {
231 int result;
232 UChar stackBuffer[STACK_BUFFER_SIZE];
233 UChar *buffer = stackBuffer;
234
235 result = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, STACK_BUFFER_SIZE);
236
237 if (result == 0) {
238 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
239 int newLength = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, NULL, 0);
240
241 buffer = NEW_ARRAY(UChar, newLength);
242 GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, newLength);
243 }
244 }
245
246 appendTo.append(buffer, (int32_t) wcslen(buffer));
247
248 if (buffer != stackBuffer) {
249 DELETE_ARRAY(buffer);
250 }
251 }
252
253 static const DWORD tfFlags[] = {0, 0, 0, TIME_NOSECONDS};
254
255 void Win32DateFormat::formatTime(const SYSTEMTIME *st, UnicodeString &appendTo) const
256 {
257 int result;
258 UChar stackBuffer[STACK_BUFFER_SIZE];
259 UChar *buffer = stackBuffer;
260
261 result = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, STACK_BUFFER_SIZE);
262
263 if (result == 0) {
264 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
265 int newLength = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, NULL, 0);
266
267 buffer = NEW_ARRAY(UChar, newLength);
268 GetDateFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, newLength);
269 }
270 }
271
272 appendTo.append(buffer, (int32_t) wcslen(buffer));
273
274 if (buffer != stackBuffer) {
275 DELETE_ARRAY(buffer);
276 }
277 }
278
279 UnicodeString Win32DateFormat::setTimeZoneInfo(TIME_ZONE_INFORMATION *tzi, const TimeZone &zone) const
280 {
281 UnicodeString zoneID;
282
283 zone.getID(zoneID);
284
285 if (zoneID.compare(fZoneID) != 0) {
286 UnicodeString icuid;
287
288 zone.getID(icuid);
289 if (! uprv_getWindowsTimeZoneInfo(tzi, icuid.getBuffer(), icuid.length())) {
290 UBool found = FALSE;
291 int32_t ec = TimeZone::countEquivalentIDs(icuid);
292
293 for (int z = 0; z < ec; z += 1) {
294 UnicodeString equiv = TimeZone::getEquivalentID(icuid, z);
295
296 if (found = uprv_getWindowsTimeZoneInfo(tzi, equiv.getBuffer(), equiv.length())) {
297 break;
298 }
299 }
300
301 if (! found) {
302 GetTimeZoneInformation(tzi);
303 }
304 }
305 }
306
307 return zoneID;
308 }
309
310 U_NAMESPACE_END
311
312 #endif /* #if !UCONFIG_NO_FORMATTING */
313
314 #endif // #ifdef U_WINDOWS
315