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