Imported Upstream version 58.1
[platform/upstream/icu.git] / source / i18n / windtfmt.cpp
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ********************************************************************************
5 *   Copyright (C) 2005-2016, International Business Machines
6 *   Corporation and others.  All Rights Reserved.
7 ********************************************************************************
8 *
9 * File WINDTFMT.CPP
10 *
11 ********************************************************************************
12 */
13
14 #include "unicode/utypes.h"
15
16 #if U_PLATFORM_HAS_WIN32_API
17
18 #if !UCONFIG_NO_FORMATTING
19
20 #include "unicode/ures.h"
21 #include "unicode/format.h"
22 #include "unicode/fmtable.h"
23 #include "unicode/datefmt.h"
24 #include "unicode/simpleformatter.h"
25 #include "unicode/calendar.h"
26 #include "unicode/gregocal.h"
27 #include "unicode/locid.h"
28 #include "unicode/unistr.h"
29 #include "unicode/ustring.h"
30 #include "unicode/timezone.h"
31 #include "unicode/utmscale.h"
32
33 #include "cmemory.h"
34 #include "uresimp.h"
35 #include "windtfmt.h"
36 #include "wintzimpl.h"
37
38 #   define WIN32_LEAN_AND_MEAN
39 #   define VC_EXTRALEAN
40 #   define NOUSER
41 #   define NOSERVICE
42 #   define NOIME
43 #   define NOMCX
44 #include <windows.h>
45
46 U_NAMESPACE_BEGIN
47
48 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32DateFormat)
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, UPRV_LENGTHOF(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
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
107 Win32DateFormat::Win32DateFormat(const Win32DateFormat &other)
108   : DateFormat(other)
109 {
110     *this = other;
111 }
112
113 Win32DateFormat::~Win32DateFormat()
114 {
115 //    delete fCalendar;
116     uprv_free(fTZI);
117     delete fDateTimeMsg;
118 }
119
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 == NULL ? NULL : new UnicodeString(*other.fDateTimeMsg);
128     this->fTimeStyle   = other.fTimeStyle;
129     this->fDateStyle   = other.fDateStyle;
130     this->fLocale      = other.fLocale;
131     this->fLCID        = other.fLCID;
132 //    this->fCalendar    = other.fCalendar->clone();
133     this->fZoneID      = other.fZoneID;
134
135     this->fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1);
136     *this->fTZI = *other.fTZI;
137
138     return *this;
139 }
140
141 Format *Win32DateFormat::clone(void) const
142 {
143     return new Win32DateFormat(*this);
144 }
145
146 // TODO: Is just ignoring pos the right thing?
147 UnicodeString &Win32DateFormat::format(Calendar &cal, UnicodeString &appendTo, FieldPosition &pos) const
148 {
149     FILETIME ft;
150     SYSTEMTIME st_gmt;
151     SYSTEMTIME st_local;
152     TIME_ZONE_INFORMATION tzi = *fTZI;
153     UErrorCode status = U_ZERO_ERROR;
154     const TimeZone &tz = cal.getTimeZone();
155     int64_t uct, uft;
156
157     setTimeZoneInfo(&tzi, tz);
158
159     uct = utmscale_fromInt64((int64_t) cal.getTime(status), UDTS_ICU4C_TIME, &status);
160     uft = utmscale_toInt64(uct, UDTS_WINDOWS_FILE_TIME, &status);
161
162     ft.dwLowDateTime =  (DWORD) (uft & 0xFFFFFFFF);
163     ft.dwHighDateTime = (DWORD) ((uft >> 32) & 0xFFFFFFFF);
164
165     FileTimeToSystemTime(&ft, &st_gmt);
166     SystemTimeToTzSpecificLocalTime(&tzi, &st_gmt, &st_local);
167
168
169     if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) {
170         UnicodeString date;
171         UnicodeString time;
172         UnicodeString *pattern = fDateTimeMsg;
173
174         formatDate(&st_local, date);
175         formatTime(&st_local, time);
176
177         if (strcmp(fCalendar->getType(), cal.getType()) != 0) {
178             pattern = getTimeDateFormat(&cal, &fLocale, status);
179         }
180
181         SimpleFormatter(*pattern, 2, 2, status).format(time, date, appendTo, status);
182     } else if (fDateStyle != DateFormat::kNone) {
183         formatDate(&st_local, appendTo);
184     } else if (fTimeStyle != DateFormat::kNone) {
185         formatTime(&st_local, appendTo);
186     }
187
188     return appendTo;
189 }
190
191 void Win32DateFormat::parse(const UnicodeString& text, Calendar& cal, ParsePosition& pos) const
192 {
193     pos.setErrorIndex(pos.getIndex());
194 }
195
196 void Win32DateFormat::adoptCalendar(Calendar *newCalendar)
197 {
198     if (fCalendar == NULL || strcmp(fCalendar->getType(), newCalendar->getType()) != 0) {
199         UErrorCode status = U_ZERO_ERROR;
200
201         if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) {
202             delete fDateTimeMsg;
203             fDateTimeMsg = getTimeDateFormat(newCalendar, &fLocale, status);
204         }
205     }
206
207     delete fCalendar;
208     fCalendar = newCalendar;
209
210     fZoneID = setTimeZoneInfo(fTZI, fCalendar->getTimeZone());
211 }
212
213 void Win32DateFormat::setCalendar(const Calendar &newCalendar)
214 {
215     adoptCalendar(newCalendar.clone());
216 }
217
218 void Win32DateFormat::adoptTimeZone(TimeZone *zoneToAdopt)
219 {
220     fZoneID = setTimeZoneInfo(fTZI, *zoneToAdopt);
221     fCalendar->adoptTimeZone(zoneToAdopt);
222 }
223
224 void Win32DateFormat::setTimeZone(const TimeZone& zone)
225 {
226     fZoneID = setTimeZoneInfo(fTZI, zone);
227     fCalendar->setTimeZone(zone);
228 }
229
230 static const DWORD dfFlags[] = {DATE_LONGDATE, DATE_LONGDATE, DATE_SHORTDATE, DATE_SHORTDATE};
231
232 void Win32DateFormat::formatDate(const SYSTEMTIME *st, UnicodeString &appendTo) const
233 {
234     int result;
235     wchar_t stackBuffer[STACK_BUFFER_SIZE];
236     wchar_t *buffer = stackBuffer;
237
238     result = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, STACK_BUFFER_SIZE);
239
240     if (result == 0) {
241         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
242             int newLength = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, NULL, 0);
243
244             buffer = NEW_ARRAY(wchar_t, newLength);
245             GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, newLength);
246         }
247     }
248
249     appendTo.append((const UChar *)buffer, (int32_t) wcslen(buffer));
250
251     if (buffer != stackBuffer) {
252         DELETE_ARRAY(buffer);
253     }
254 }
255
256 static const DWORD tfFlags[] = {0, 0, 0, TIME_NOSECONDS};
257
258 void Win32DateFormat::formatTime(const SYSTEMTIME *st, UnicodeString &appendTo) const
259 {
260     int result;
261     wchar_t stackBuffer[STACK_BUFFER_SIZE];
262     wchar_t *buffer = stackBuffer;
263
264     result = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, STACK_BUFFER_SIZE);
265
266     if (result == 0) {
267         if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
268             int newLength = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, NULL, 0);
269
270             buffer = NEW_ARRAY(wchar_t, newLength);
271             GetDateFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, newLength);
272         }
273     }
274
275     appendTo.append((const UChar *)buffer, (int32_t) wcslen(buffer));
276
277     if (buffer != stackBuffer) {
278         DELETE_ARRAY(buffer);
279     }
280 }
281
282 UnicodeString Win32DateFormat::setTimeZoneInfo(TIME_ZONE_INFORMATION *tzi, const TimeZone &zone) const
283 {
284     UnicodeString zoneID;
285
286     zone.getID(zoneID);
287
288     if (zoneID.compare(fZoneID) != 0) {
289         UnicodeString icuid;
290
291         zone.getID(icuid);
292         if (! uprv_getWindowsTimeZoneInfo(tzi, icuid.getBuffer(), icuid.length())) {
293             UBool found = FALSE;
294             int32_t ec = TimeZone::countEquivalentIDs(icuid);
295
296             for (int z = 0; z < ec; z += 1) {
297                 UnicodeString equiv = TimeZone::getEquivalentID(icuid, z);
298
299                 if (found = uprv_getWindowsTimeZoneInfo(tzi, equiv.getBuffer(), equiv.length())) {
300                     break;
301                 }
302             }
303
304             if (! found) {
305                 GetTimeZoneInformation(tzi);
306             }
307         }
308     }
309
310     return zoneID;
311 }
312
313 U_NAMESPACE_END
314
315 #endif /* #if !UCONFIG_NO_FORMATTING */
316
317 #endif // U_PLATFORM_HAS_WIN32_API
318