1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
********************************************************************************
* Copyright (C) 2005-2016, International Business Machines
* Corporation and others. All Rights Reserved.
********************************************************************************
*
* File WINDTFMT.CPP
*
********************************************************************************
*/
#include "unicode/utypes.h"
#if U_PLATFORM_USES_ONLY_WIN32_API
#if !UCONFIG_NO_FORMATTING
#include "unicode/ures.h"
#include "unicode/format.h"
#include "unicode/fmtable.h"
#include "unicode/datefmt.h"
#include "unicode/simpleformatter.h"
#include "unicode/calendar.h"
#include "unicode/gregocal.h"
#include "unicode/locid.h"
#include "unicode/unistr.h"
#include "unicode/ustring.h"
#include "unicode/timezone.h"
#include "unicode/utmscale.h"
#include "cmemory.h"
#include "uresimp.h"
#include "windtfmt.h"
#include "wintzimpl.h"
#ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif
# define VC_EXTRALEAN
# define NOUSER
# define NOSERVICE
# define NOIME
# define NOMCX
#include <windows.h>
U_NAMESPACE_BEGIN
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32DateFormat)
#define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type))
#define DELETE_ARRAY(array) uprv_free((void *) (array))
#define STACK_BUFFER_SIZE 64
UnicodeString* Win32DateFormat::getTimeDateFormat(const Calendar *cal, const Locale *locale, UErrorCode &status) const
{
UnicodeString *result = NULL;
const char *type = cal->getType();
const char *base = locale->getBaseName();
UResourceBundle *topBundle = ures_open((char *) 0, base, &status);
UResourceBundle *calBundle = ures_getByKey(topBundle, "calendar", NULL, &status);
UResourceBundle *typBundle = ures_getByKeyWithFallback(calBundle, type, NULL, &status);
UResourceBundle *patBundle = ures_getByKeyWithFallback(typBundle, "DateTimePatterns", NULL, &status);
if (status == U_MISSING_RESOURCE_ERROR) {
status = U_ZERO_ERROR;
typBundle = ures_getByKeyWithFallback(calBundle, "gregorian", typBundle, &status);
patBundle = ures_getByKeyWithFallback(typBundle, "DateTimePatterns", patBundle, &status);
}
if (U_FAILURE(status)) {
static const UChar defaultPattern[] = {0x007B, 0x0031, 0x007D, 0x0020, 0x007B, 0x0030, 0x007D, 0x0000}; // "{1} {0}"
return new UnicodeString(defaultPattern, UPRV_LENGTHOF(defaultPattern));
}
int32_t resStrLen = 0;
int32_t glueIndex = DateFormat::kDateTime;
int32_t patSize = ures_getSize(patBundle);
if (patSize >= (DateFormat::kDateTimeOffset + DateFormat::kShort + 1)) {
// Get proper date time format
glueIndex = (int32_t)(DateFormat::kDateTimeOffset + (fDateStyle - DateFormat::kDateOffset));
}
const UChar *resStr = ures_getStringByIndex(patBundle, glueIndex, &resStrLen, &status);
result = new UnicodeString(TRUE, resStr, resStrLen);
ures_close(patBundle);
ures_close(typBundle);
ures_close(calBundle);
ures_close(topBundle);
return result;
}
// TODO: This is copied in both winnmfmt.cpp and windtfmt.cpp, but really should
// be factored out into a common helper for both.
static UErrorCode GetEquivalentWindowsLocaleName(const Locale& locale, UnicodeString** buffer)
{
UErrorCode status = U_ZERO_ERROR;
char asciiBCP47Tag[LOCALE_NAME_MAX_LENGTH] = {};
// Convert from names like "en_CA" and "de_DE@collation=phonebook" to "en-CA" and "de-DE-u-co-phonebk".
(void)uloc_toLanguageTag(locale.getName(), asciiBCP47Tag, UPRV_LENGTHOF(asciiBCP47Tag), FALSE, &status);
if (U_SUCCESS(status))
{
// Need it to be UTF-16, not 8-bit
// TODO: This seems like a good thing for a helper
wchar_t bcp47Tag[LOCALE_NAME_MAX_LENGTH] = {};
int32_t i;
for (i = 0; i < UPRV_LENGTHOF(bcp47Tag); i++)
{
if (asciiBCP47Tag[i] == '\0')
{
break;
}
else
{
// normally just copy the character
bcp47Tag[i] = static_cast<wchar_t>(asciiBCP47Tag[i]);
}
}
// Ensure it's null terminated
if (i < (UPRV_LENGTHOF(bcp47Tag) - 1))
{
bcp47Tag[i] = L'\0';
}
else
{
// Ran out of room.
bcp47Tag[UPRV_LENGTHOF(bcp47Tag) - 1] = L'\0';
}
wchar_t windowsLocaleName[LOCALE_NAME_MAX_LENGTH] = {};
// Note: On Windows versions below 10, there is no support for locale name aliases.
// This means that it will fail for locales where ICU has a completely different
// name (like ku vs ckb), and it will also not work for alternate sort locale
// names like "de-DE-u-co-phonebk".
// TODO: We could add some sort of exception table for cases like ku vs ckb.
int length = ResolveLocaleName(bcp47Tag, windowsLocaleName, UPRV_LENGTHOF(windowsLocaleName));
if (length > 0)
{
*buffer = new UnicodeString(windowsLocaleName);
}
else
{
status = U_UNSUPPORTED_ERROR;
}
}
return status;
}
// TODO: Range-check timeStyle, dateStyle
Win32DateFormat::Win32DateFormat(DateFormat::EStyle timeStyle, DateFormat::EStyle dateStyle, const Locale &locale, UErrorCode &status)
: DateFormat(), fDateTimeMsg(NULL), fTimeStyle(timeStyle), fDateStyle(dateStyle), fLocale(locale), fZoneID(), fWindowsLocaleName(nullptr)
{
if (U_SUCCESS(status)) {
GetEquivalentWindowsLocaleName(locale, &fWindowsLocaleName);
// Note: In the previous code, it would look up the LCID for the locale, and if
// the locale was not recognized then it would get an LCID of 0, which is a
// synonym for LOCALE_USER_DEFAULT on Windows.
// If the above method fails, then fWindowsLocaleName will remain as nullptr, and
// then we will pass nullptr to API GetLocaleInfoEx, which is the same as passing
// LOCALE_USER_DEFAULT.
fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1);
uprv_memset(fTZI, 0, sizeof(TIME_ZONE_INFORMATION));
adoptCalendar(Calendar::createInstance(locale, status));
}
}
Win32DateFormat::Win32DateFormat(const Win32DateFormat &other)
: DateFormat(other)
{
*this = other;
}
Win32DateFormat::~Win32DateFormat()
{
// delete fCalendar;
uprv_free(fTZI);
delete fDateTimeMsg;
delete fWindowsLocaleName;
}
Win32DateFormat &Win32DateFormat::operator=(const Win32DateFormat &other)
{
// The following handles fCalendar
DateFormat::operator=(other);
// delete fCalendar;
this->fDateTimeMsg = other.fDateTimeMsg == NULL ? NULL : new UnicodeString(*other.fDateTimeMsg);
this->fTimeStyle = other.fTimeStyle;
this->fDateStyle = other.fDateStyle;
this->fLocale = other.fLocale;
// this->fCalendar = other.fCalendar->clone();
this->fZoneID = other.fZoneID;
this->fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1);
*this->fTZI = *other.fTZI;
this->fWindowsLocaleName = other.fWindowsLocaleName == NULL ? NULL : new UnicodeString(*other.fWindowsLocaleName);
return *this;
}
Win32DateFormat *Win32DateFormat::clone() const
{
return new Win32DateFormat(*this);
}
// TODO: Is just ignoring pos the right thing?
UnicodeString &Win32DateFormat::format(Calendar &cal, UnicodeString &appendTo, FieldPosition & /* pos */) const
{
FILETIME ft;
SYSTEMTIME st_gmt;
SYSTEMTIME st_local;
TIME_ZONE_INFORMATION tzi = *fTZI;
UErrorCode status = U_ZERO_ERROR;
const TimeZone &tz = cal.getTimeZone();
int64_t uct, uft;
setTimeZoneInfo(&tzi, tz);
uct = utmscale_fromInt64((int64_t) cal.getTime(status), UDTS_ICU4C_TIME, &status);
uft = utmscale_toInt64(uct, UDTS_WINDOWS_FILE_TIME, &status);
ft.dwLowDateTime = (DWORD) (uft & 0xFFFFFFFF);
ft.dwHighDateTime = (DWORD) ((uft >> 32) & 0xFFFFFFFF);
FileTimeToSystemTime(&ft, &st_gmt);
SystemTimeToTzSpecificLocalTime(&tzi, &st_gmt, &st_local);
if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) {
UnicodeString date;
UnicodeString time;
UnicodeString *pattern = fDateTimeMsg;
formatDate(&st_local, date);
formatTime(&st_local, time);
if (strcmp(fCalendar->getType(), cal.getType()) != 0) {
pattern = getTimeDateFormat(&cal, &fLocale, status);
}
SimpleFormatter(*pattern, 2, 2, status).format(time, date, appendTo, status);
} else if (fDateStyle != DateFormat::kNone) {
formatDate(&st_local, appendTo);
} else if (fTimeStyle != DateFormat::kNone) {
formatTime(&st_local, appendTo);
}
return appendTo;
}
void Win32DateFormat::parse(const UnicodeString& /* text */, Calendar& /* cal */, ParsePosition& pos) const
{
pos.setErrorIndex(pos.getIndex());
}
void Win32DateFormat::adoptCalendar(Calendar *newCalendar)
{
if (fCalendar == NULL || strcmp(fCalendar->getType(), newCalendar->getType()) != 0) {
UErrorCode status = U_ZERO_ERROR;
if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) {
delete fDateTimeMsg;
fDateTimeMsg = getTimeDateFormat(newCalendar, &fLocale, status);
}
}
delete fCalendar;
fCalendar = newCalendar;
fZoneID = setTimeZoneInfo(fTZI, fCalendar->getTimeZone());
}
void Win32DateFormat::setCalendar(const Calendar &newCalendar)
{
adoptCalendar(newCalendar.clone());
}
void Win32DateFormat::adoptTimeZone(TimeZone *zoneToAdopt)
{
fZoneID = setTimeZoneInfo(fTZI, *zoneToAdopt);
fCalendar->adoptTimeZone(zoneToAdopt);
}
void Win32DateFormat::setTimeZone(const TimeZone& zone)
{
fZoneID = setTimeZoneInfo(fTZI, zone);
fCalendar->setTimeZone(zone);
}
static const DWORD dfFlags[] = {DATE_LONGDATE, DATE_LONGDATE, DATE_SHORTDATE, DATE_SHORTDATE};
void Win32DateFormat::formatDate(const SYSTEMTIME *st, UnicodeString &appendTo) const
{
int result=0;
wchar_t stackBuffer[STACK_BUFFER_SIZE];
wchar_t *buffer = stackBuffer;
const wchar_t *localeName = nullptr;
if (fWindowsLocaleName != nullptr)
{
localeName = reinterpret_cast<const wchar_t*>(toOldUCharPtr(fWindowsLocaleName->getTerminatedBuffer()));
}
result = GetDateFormatEx(localeName, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, STACK_BUFFER_SIZE, NULL);
if (result == 0) {
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
int newLength = GetDateFormatEx(localeName, dfFlags[fDateStyle - kDateOffset], st, NULL, NULL, 0, NULL);
buffer = NEW_ARRAY(wchar_t, newLength);
GetDateFormatEx(localeName, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, newLength, NULL);
}
}
appendTo.append((const UChar *)buffer, (int32_t) wcslen(buffer));
if (buffer != stackBuffer) {
DELETE_ARRAY(buffer);
}
}
static const DWORD tfFlags[] = {0, 0, 0, TIME_NOSECONDS};
void Win32DateFormat::formatTime(const SYSTEMTIME *st, UnicodeString &appendTo) const
{
int result;
wchar_t stackBuffer[STACK_BUFFER_SIZE];
wchar_t *buffer = stackBuffer;
const wchar_t *localeName = nullptr;
if (fWindowsLocaleName != nullptr)
{
localeName = reinterpret_cast<const wchar_t*>(toOldUCharPtr(fWindowsLocaleName->getTerminatedBuffer()));
}
result = GetTimeFormatEx(localeName, tfFlags[fTimeStyle], st, NULL, buffer, STACK_BUFFER_SIZE);
if (result == 0) {
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
int newLength = GetTimeFormatEx(localeName, tfFlags[fTimeStyle], st, NULL, NULL, 0);
buffer = NEW_ARRAY(wchar_t, newLength);
GetTimeFormatEx(localeName, tfFlags[fTimeStyle], st, NULL, buffer, newLength);
}
}
appendTo.append((const UChar *)buffer, (int32_t) wcslen(buffer));
if (buffer != stackBuffer) {
DELETE_ARRAY(buffer);
}
}
UnicodeString Win32DateFormat::setTimeZoneInfo(TIME_ZONE_INFORMATION *tzi, const TimeZone &zone) const
{
UnicodeString zoneID;
zone.getID(zoneID);
if (zoneID.compare(fZoneID) != 0) {
UnicodeString icuid;
zone.getID(icuid);
if (! uprv_getWindowsTimeZoneInfo(tzi, icuid.getBuffer(), icuid.length())) {
UBool found = FALSE;
int32_t ec = TimeZone::countEquivalentIDs(icuid);
for (int z = 0; z < ec; z += 1) {
UnicodeString equiv = TimeZone::getEquivalentID(icuid, z);
found = uprv_getWindowsTimeZoneInfo(tzi, equiv.getBuffer(), equiv.length());
if (found) {
break;
}
}
if (! found) {
GetTimeZoneInformation(tzi);
}
}
}
return zoneID;
}
U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif // U_PLATFORM_USES_ONLY_WIN32_API
|