• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ********************************************************************************
3 *   Copyright (C) 2005-2011, 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 #if U_PLATFORM_HAS_WIN32_API
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 "wintzimpl.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 
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32DateFormat)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* Win32DateFormat::getTimeDateFormat(const Calendar *cal, const Locale *locale, UErrorCode &status) const
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         static const 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     int32_t glueIndex = DateFormat::kDateTime;
78     int32_t patSize = ures_getSize(patBundle);
79     if (patSize >= (DateFormat::kDateTimeOffset + DateFormat::kShort + 1)) {
80         // Get proper date time format
81         glueIndex = (int32_t)(DateFormat::kDateTimeOffset + (fDateStyle - DateFormat::kDateOffset));
82     }
83     const UChar *resStr = ures_getStringByIndex(patBundle, glueIndex, &resStrLen, &status);
84 
85     result = new UnicodeString(TRUE, resStr, resStrLen);
86 
87     ures_close(patBundle);
88     ures_close(typBundle);
89     ures_close(calBundle);
90     ures_close(topBundle);
91 
92     return result;
93 }
94 
95 // TODO: Range-check timeStyle, dateStyle
Win32DateFormat(DateFormat::EStyle timeStyle,DateFormat::EStyle dateStyle,const Locale & locale,UErrorCode & status)96 Win32DateFormat::Win32DateFormat(DateFormat::EStyle timeStyle, DateFormat::EStyle dateStyle, const Locale &locale, UErrorCode &status)
97   : DateFormat(), fDateTimeMsg(NULL), fTimeStyle(timeStyle), fDateStyle(dateStyle), fLocale(&locale), fZoneID()
98 {
99     if (U_SUCCESS(status)) {
100         fLCID = locale.getLCID();
101         fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1);
102         uprv_memset(fTZI, 0, sizeof(TIME_ZONE_INFORMATION));
103         adoptCalendar(Calendar::createInstance(locale, status));
104     }
105 }
106 
Win32DateFormat(const Win32DateFormat & other)107 Win32DateFormat::Win32DateFormat(const Win32DateFormat &other)
108   : DateFormat(other)
109 {
110     *this = other;
111 }
112 
~Win32DateFormat()113 Win32DateFormat::~Win32DateFormat()
114 {
115 //    delete fCalendar;
116     uprv_free(fTZI);
117     delete fDateTimeMsg;
118 }
119 
operator =(const Win32DateFormat & other)120 Win32DateFormat &Win32DateFormat::operator=(const Win32DateFormat &other)
121 {
122     // The following handles fCalendar
123     DateFormat::operator=(other);
124 
125 //    delete fCalendar;
126 
127     this->fDateTimeMsg = other.fDateTimeMsg;
128     this->fTimeStyle   = other.fTimeStyle;
129     this->fDateStyle   = other.fDateStyle;
130     this->fLCID        = other.fLCID;
131 //    this->fCalendar    = other.fCalendar->clone();
132     this->fZoneID      = other.fZoneID;
133 
134     this->fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1);
135     *this->fTZI = *other.fTZI;
136 
137     return *this;
138 }
139 
clone(void) const140 Format *Win32DateFormat::clone(void) const
141 {
142     return new Win32DateFormat(*this);
143 }
144 
145 // TODO: Is just ignoring pos the right thing?
format(Calendar & cal,UnicodeString & appendTo,FieldPosition & pos) const146 UnicodeString &Win32DateFormat::format(Calendar &cal, UnicodeString &appendTo, FieldPosition &pos) const
147 {
148     FILETIME ft;
149     SYSTEMTIME st_gmt;
150     SYSTEMTIME st_local;
151     TIME_ZONE_INFORMATION tzi = *fTZI;
152     UErrorCode status = U_ZERO_ERROR;
153     const TimeZone &tz = cal.getTimeZone();
154     int64_t uct, uft;
155 
156     setTimeZoneInfo(&tzi, tz);
157 
158     uct = utmscale_fromInt64((int64_t) cal.getTime(status), UDTS_ICU4C_TIME, &status);
159     uft = utmscale_toInt64(uct, UDTS_WINDOWS_FILE_TIME, &status);
160 
161     ft.dwLowDateTime =  (DWORD) (uft & 0xFFFFFFFF);
162     ft.dwHighDateTime = (DWORD) ((uft >> 32) & 0xFFFFFFFF);
163 
164     FileTimeToSystemTime(&ft, &st_gmt);
165     SystemTimeToTzSpecificLocalTime(&tzi, &st_gmt, &st_local);
166 
167 
168     if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) {
169         UnicodeString *date = new UnicodeString();
170         UnicodeString *time = new UnicodeString();
171         UnicodeString *pattern = fDateTimeMsg;
172         Formattable timeDateArray[2];
173 
174         formatDate(&st_local, *date);
175         formatTime(&st_local, *time);
176 
177         timeDateArray[0].adoptString(time);
178         timeDateArray[1].adoptString(date);
179 
180         if (strcmp(fCalendar->getType(), cal.getType()) != 0) {
181             pattern = getTimeDateFormat(&cal, fLocale, status);
182         }
183 
184         MessageFormat::format(*pattern, timeDateArray, 2, appendTo, status);
185     } else if (fDateStyle != DateFormat::kNone) {
186         formatDate(&st_local, appendTo);
187     } else if (fTimeStyle != DateFormat::kNone) {
188         formatTime(&st_local, appendTo);
189     }
190 
191     return appendTo;
192 }
193 
parse(const UnicodeString & text,Calendar & cal,ParsePosition & pos) const194 void Win32DateFormat::parse(const UnicodeString& text, Calendar& cal, ParsePosition& pos) const
195 {
196     pos.setErrorIndex(pos.getIndex());
197 }
198 
adoptCalendar(Calendar * newCalendar)199 void Win32DateFormat::adoptCalendar(Calendar *newCalendar)
200 {
201     if (fCalendar == NULL || strcmp(fCalendar->getType(), newCalendar->getType()) != 0) {
202         UErrorCode status = U_ZERO_ERROR;
203 
204         if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) {
205             delete fDateTimeMsg;
206             fDateTimeMsg = getTimeDateFormat(newCalendar, fLocale, status);
207         }
208     }
209 
210     delete fCalendar;
211     fCalendar = newCalendar;
212 
213     fZoneID = setTimeZoneInfo(fTZI, fCalendar->getTimeZone());
214 }
215 
setCalendar(const Calendar & newCalendar)216 void Win32DateFormat::setCalendar(const Calendar &newCalendar)
217 {
218     adoptCalendar(newCalendar.clone());
219 }
220 
adoptTimeZone(TimeZone * zoneToAdopt)221 void Win32DateFormat::adoptTimeZone(TimeZone *zoneToAdopt)
222 {
223     fZoneID = setTimeZoneInfo(fTZI, *zoneToAdopt);
224     fCalendar->adoptTimeZone(zoneToAdopt);
225 }
226 
setTimeZone(const TimeZone & zone)227 void Win32DateFormat::setTimeZone(const TimeZone& zone)
228 {
229     fZoneID = setTimeZoneInfo(fTZI, zone);
230     fCalendar->setTimeZone(zone);
231 }
232 
233 static const DWORD dfFlags[] = {DATE_LONGDATE, DATE_LONGDATE, DATE_SHORTDATE, DATE_SHORTDATE};
234 
formatDate(const SYSTEMTIME * st,UnicodeString & appendTo) const235 void Win32DateFormat::formatDate(const SYSTEMTIME *st, UnicodeString &appendTo) const
236 {
237     int result;
238     UChar stackBuffer[STACK_BUFFER_SIZE];
239     UChar *buffer = stackBuffer;
240 
241     result = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, STACK_BUFFER_SIZE);
242 
243     if (result == 0) {
244         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
245             int newLength = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, NULL, 0);
246 
247             buffer = NEW_ARRAY(UChar, newLength);
248             GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, newLength);
249         }
250     }
251 
252     appendTo.append(buffer, (int32_t) wcslen(buffer));
253 
254     if (buffer != stackBuffer) {
255         DELETE_ARRAY(buffer);
256     }
257 }
258 
259 static const DWORD tfFlags[] = {0, 0, 0, TIME_NOSECONDS};
260 
formatTime(const SYSTEMTIME * st,UnicodeString & appendTo) const261 void Win32DateFormat::formatTime(const SYSTEMTIME *st, UnicodeString &appendTo) const
262 {
263     int result;
264     UChar stackBuffer[STACK_BUFFER_SIZE];
265     UChar *buffer = stackBuffer;
266 
267     result = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, STACK_BUFFER_SIZE);
268 
269     if (result == 0) {
270         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
271             int newLength = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, NULL, 0);
272 
273             buffer = NEW_ARRAY(UChar, newLength);
274             GetDateFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, newLength);
275         }
276     }
277 
278     appendTo.append(buffer, (int32_t) wcslen(buffer));
279 
280     if (buffer != stackBuffer) {
281         DELETE_ARRAY(buffer);
282     }
283 }
284 
setTimeZoneInfo(TIME_ZONE_INFORMATION * tzi,const TimeZone & zone) const285 UnicodeString Win32DateFormat::setTimeZoneInfo(TIME_ZONE_INFORMATION *tzi, const TimeZone &zone) const
286 {
287     UnicodeString zoneID;
288 
289     zone.getID(zoneID);
290 
291     if (zoneID.compare(fZoneID) != 0) {
292         UnicodeString icuid;
293 
294         zone.getID(icuid);
295         if (! uprv_getWindowsTimeZoneInfo(tzi, icuid.getBuffer(), icuid.length())) {
296             UBool found = FALSE;
297             int32_t ec = TimeZone::countEquivalentIDs(icuid);
298 
299             for (int z = 0; z < ec; z += 1) {
300                 UnicodeString equiv = TimeZone::getEquivalentID(icuid, z);
301 
302                 if (found = uprv_getWindowsTimeZoneInfo(tzi, equiv.getBuffer(), equiv.length())) {
303                     break;
304                 }
305             }
306 
307             if (! found) {
308                 GetTimeZoneInformation(tzi);
309             }
310         }
311     }
312 
313     return zoneID;
314 }
315 
316 U_NAMESPACE_END
317 
318 #endif /* #if !UCONFIG_NO_FORMATTING */
319 
320 #endif // U_PLATFORM_HAS_WIN32_API
321 
322