michael@0: /* michael@0: ******************************************************************************** michael@0: * Copyright (C) 2005-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************** michael@0: * michael@0: * File WINDTFMT.CPP michael@0: * michael@0: ******************************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if U_PLATFORM_HAS_WIN32_API michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/ures.h" michael@0: #include "unicode/format.h" michael@0: #include "unicode/fmtable.h" michael@0: #include "unicode/datefmt.h" michael@0: #include "unicode/msgfmt.h" michael@0: #include "unicode/calendar.h" michael@0: #include "unicode/gregocal.h" michael@0: #include "unicode/locid.h" michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/timezone.h" michael@0: #include "unicode/utmscale.h" michael@0: michael@0: #include "cmemory.h" michael@0: #include "uresimp.h" michael@0: #include "windtfmt.h" michael@0: #include "wintzimpl.h" michael@0: michael@0: # define WIN32_LEAN_AND_MEAN michael@0: # define VC_EXTRALEAN michael@0: # define NOUSER michael@0: # define NOSERVICE michael@0: # define NOIME michael@0: # define NOMCX michael@0: #include michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32DateFormat) michael@0: michael@0: #define ARRAY_SIZE(array) (sizeof array / sizeof array[0]) michael@0: michael@0: #define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type)) michael@0: #define DELETE_ARRAY(array) uprv_free((void *) (array)) michael@0: michael@0: #define STACK_BUFFER_SIZE 64 michael@0: michael@0: UnicodeString* Win32DateFormat::getTimeDateFormat(const Calendar *cal, const Locale *locale, UErrorCode &status) const michael@0: { michael@0: UnicodeString *result = NULL; michael@0: const char *type = cal->getType(); michael@0: const char *base = locale->getBaseName(); michael@0: UResourceBundle *topBundle = ures_open((char *) 0, base, &status); michael@0: UResourceBundle *calBundle = ures_getByKey(topBundle, "calendar", NULL, &status); michael@0: UResourceBundle *typBundle = ures_getByKeyWithFallback(calBundle, type, NULL, &status); michael@0: UResourceBundle *patBundle = ures_getByKeyWithFallback(typBundle, "DateTimePatterns", NULL, &status); michael@0: michael@0: if (status == U_MISSING_RESOURCE_ERROR) { michael@0: status = U_ZERO_ERROR; michael@0: typBundle = ures_getByKeyWithFallback(calBundle, "gregorian", typBundle, &status); michael@0: patBundle = ures_getByKeyWithFallback(typBundle, "DateTimePatterns", patBundle, &status); michael@0: } michael@0: michael@0: if (U_FAILURE(status)) { michael@0: static const UChar defaultPattern[] = {0x007B, 0x0031, 0x007D, 0x0020, 0x007B, 0x0030, 0x007D, 0x0000}; // "{1} {0}" michael@0: return new UnicodeString(defaultPattern, ARRAY_SIZE(defaultPattern)); michael@0: } michael@0: michael@0: int32_t resStrLen = 0; michael@0: int32_t glueIndex = DateFormat::kDateTime; michael@0: int32_t patSize = ures_getSize(patBundle); michael@0: if (patSize >= (DateFormat::kDateTimeOffset + DateFormat::kShort + 1)) { michael@0: // Get proper date time format michael@0: glueIndex = (int32_t)(DateFormat::kDateTimeOffset + (fDateStyle - DateFormat::kDateOffset)); michael@0: } michael@0: const UChar *resStr = ures_getStringByIndex(patBundle, glueIndex, &resStrLen, &status); michael@0: michael@0: result = new UnicodeString(TRUE, resStr, resStrLen); michael@0: michael@0: ures_close(patBundle); michael@0: ures_close(typBundle); michael@0: ures_close(calBundle); michael@0: ures_close(topBundle); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: // TODO: Range-check timeStyle, dateStyle michael@0: Win32DateFormat::Win32DateFormat(DateFormat::EStyle timeStyle, DateFormat::EStyle dateStyle, const Locale &locale, UErrorCode &status) michael@0: : DateFormat(), fDateTimeMsg(NULL), fTimeStyle(timeStyle), fDateStyle(dateStyle), fLocale(locale), fZoneID() michael@0: { michael@0: if (U_SUCCESS(status)) { michael@0: fLCID = locale.getLCID(); michael@0: fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1); michael@0: uprv_memset(fTZI, 0, sizeof(TIME_ZONE_INFORMATION)); michael@0: adoptCalendar(Calendar::createInstance(locale, status)); michael@0: } michael@0: } michael@0: michael@0: Win32DateFormat::Win32DateFormat(const Win32DateFormat &other) michael@0: : DateFormat(other) michael@0: { michael@0: *this = other; michael@0: } michael@0: michael@0: Win32DateFormat::~Win32DateFormat() michael@0: { michael@0: // delete fCalendar; michael@0: uprv_free(fTZI); michael@0: delete fDateTimeMsg; michael@0: } michael@0: michael@0: Win32DateFormat &Win32DateFormat::operator=(const Win32DateFormat &other) michael@0: { michael@0: // The following handles fCalendar michael@0: DateFormat::operator=(other); michael@0: michael@0: // delete fCalendar; michael@0: michael@0: this->fDateTimeMsg = other.fDateTimeMsg; michael@0: this->fTimeStyle = other.fTimeStyle; michael@0: this->fDateStyle = other.fDateStyle; michael@0: this->fLocale = other.fLocale; michael@0: this->fLCID = other.fLCID; michael@0: // this->fCalendar = other.fCalendar->clone(); michael@0: this->fZoneID = other.fZoneID; michael@0: michael@0: this->fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1); michael@0: *this->fTZI = *other.fTZI; michael@0: michael@0: return *this; michael@0: } michael@0: michael@0: Format *Win32DateFormat::clone(void) const michael@0: { michael@0: return new Win32DateFormat(*this); michael@0: } michael@0: michael@0: // TODO: Is just ignoring pos the right thing? michael@0: UnicodeString &Win32DateFormat::format(Calendar &cal, UnicodeString &appendTo, FieldPosition &pos) const michael@0: { michael@0: FILETIME ft; michael@0: SYSTEMTIME st_gmt; michael@0: SYSTEMTIME st_local; michael@0: TIME_ZONE_INFORMATION tzi = *fTZI; michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: const TimeZone &tz = cal.getTimeZone(); michael@0: int64_t uct, uft; michael@0: michael@0: setTimeZoneInfo(&tzi, tz); michael@0: michael@0: uct = utmscale_fromInt64((int64_t) cal.getTime(status), UDTS_ICU4C_TIME, &status); michael@0: uft = utmscale_toInt64(uct, UDTS_WINDOWS_FILE_TIME, &status); michael@0: michael@0: ft.dwLowDateTime = (DWORD) (uft & 0xFFFFFFFF); michael@0: ft.dwHighDateTime = (DWORD) ((uft >> 32) & 0xFFFFFFFF); michael@0: michael@0: FileTimeToSystemTime(&ft, &st_gmt); michael@0: SystemTimeToTzSpecificLocalTime(&tzi, &st_gmt, &st_local); michael@0: michael@0: michael@0: if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) { michael@0: UnicodeString *date = new UnicodeString(); michael@0: UnicodeString *time = new UnicodeString(); michael@0: UnicodeString *pattern = fDateTimeMsg; michael@0: Formattable timeDateArray[2]; michael@0: michael@0: formatDate(&st_local, *date); michael@0: formatTime(&st_local, *time); michael@0: michael@0: timeDateArray[0].adoptString(time); michael@0: timeDateArray[1].adoptString(date); michael@0: michael@0: if (strcmp(fCalendar->getType(), cal.getType()) != 0) { michael@0: pattern = getTimeDateFormat(&cal, &fLocale, status); michael@0: } michael@0: michael@0: MessageFormat::format(*pattern, timeDateArray, 2, appendTo, status); michael@0: } else if (fDateStyle != DateFormat::kNone) { michael@0: formatDate(&st_local, appendTo); michael@0: } else if (fTimeStyle != DateFormat::kNone) { michael@0: formatTime(&st_local, appendTo); michael@0: } michael@0: michael@0: return appendTo; michael@0: } michael@0: michael@0: void Win32DateFormat::parse(const UnicodeString& text, Calendar& cal, ParsePosition& pos) const michael@0: { michael@0: pos.setErrorIndex(pos.getIndex()); michael@0: } michael@0: michael@0: void Win32DateFormat::adoptCalendar(Calendar *newCalendar) michael@0: { michael@0: if (fCalendar == NULL || strcmp(fCalendar->getType(), newCalendar->getType()) != 0) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: michael@0: if (fDateStyle != DateFormat::kNone && fTimeStyle != DateFormat::kNone) { michael@0: delete fDateTimeMsg; michael@0: fDateTimeMsg = getTimeDateFormat(newCalendar, &fLocale, status); michael@0: } michael@0: } michael@0: michael@0: delete fCalendar; michael@0: fCalendar = newCalendar; michael@0: michael@0: fZoneID = setTimeZoneInfo(fTZI, fCalendar->getTimeZone()); michael@0: } michael@0: michael@0: void Win32DateFormat::setCalendar(const Calendar &newCalendar) michael@0: { michael@0: adoptCalendar(newCalendar.clone()); michael@0: } michael@0: michael@0: void Win32DateFormat::adoptTimeZone(TimeZone *zoneToAdopt) michael@0: { michael@0: fZoneID = setTimeZoneInfo(fTZI, *zoneToAdopt); michael@0: fCalendar->adoptTimeZone(zoneToAdopt); michael@0: } michael@0: michael@0: void Win32DateFormat::setTimeZone(const TimeZone& zone) michael@0: { michael@0: fZoneID = setTimeZoneInfo(fTZI, zone); michael@0: fCalendar->setTimeZone(zone); michael@0: } michael@0: michael@0: static const DWORD dfFlags[] = {DATE_LONGDATE, DATE_LONGDATE, DATE_SHORTDATE, DATE_SHORTDATE}; michael@0: michael@0: void Win32DateFormat::formatDate(const SYSTEMTIME *st, UnicodeString &appendTo) const michael@0: { michael@0: int result; michael@0: UChar stackBuffer[STACK_BUFFER_SIZE]; michael@0: UChar *buffer = stackBuffer; michael@0: michael@0: result = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, STACK_BUFFER_SIZE); michael@0: michael@0: if (result == 0) { michael@0: if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { michael@0: int newLength = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, NULL, 0); michael@0: michael@0: buffer = NEW_ARRAY(UChar, newLength); michael@0: GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, newLength); michael@0: } michael@0: } michael@0: michael@0: appendTo.append(buffer, (int32_t) wcslen(buffer)); michael@0: michael@0: if (buffer != stackBuffer) { michael@0: DELETE_ARRAY(buffer); michael@0: } michael@0: } michael@0: michael@0: static const DWORD tfFlags[] = {0, 0, 0, TIME_NOSECONDS}; michael@0: michael@0: void Win32DateFormat::formatTime(const SYSTEMTIME *st, UnicodeString &appendTo) const michael@0: { michael@0: int result; michael@0: UChar stackBuffer[STACK_BUFFER_SIZE]; michael@0: UChar *buffer = stackBuffer; michael@0: michael@0: result = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, STACK_BUFFER_SIZE); michael@0: michael@0: if (result == 0) { michael@0: if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { michael@0: int newLength = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, NULL, 0); michael@0: michael@0: buffer = NEW_ARRAY(UChar, newLength); michael@0: GetDateFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, newLength); michael@0: } michael@0: } michael@0: michael@0: appendTo.append(buffer, (int32_t) wcslen(buffer)); michael@0: michael@0: if (buffer != stackBuffer) { michael@0: DELETE_ARRAY(buffer); michael@0: } michael@0: } michael@0: michael@0: UnicodeString Win32DateFormat::setTimeZoneInfo(TIME_ZONE_INFORMATION *tzi, const TimeZone &zone) const michael@0: { michael@0: UnicodeString zoneID; michael@0: michael@0: zone.getID(zoneID); michael@0: michael@0: if (zoneID.compare(fZoneID) != 0) { michael@0: UnicodeString icuid; michael@0: michael@0: zone.getID(icuid); michael@0: if (! uprv_getWindowsTimeZoneInfo(tzi, icuid.getBuffer(), icuid.length())) { michael@0: UBool found = FALSE; michael@0: int32_t ec = TimeZone::countEquivalentIDs(icuid); michael@0: michael@0: for (int z = 0; z < ec; z += 1) { michael@0: UnicodeString equiv = TimeZone::getEquivalentID(icuid, z); michael@0: michael@0: if (found = uprv_getWindowsTimeZoneInfo(tzi, equiv.getBuffer(), equiv.length())) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (! found) { michael@0: GetTimeZoneInformation(tzi); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return zoneID; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: #endif // U_PLATFORM_HAS_WIN32_API michael@0: