2006-01-12 19:53:58 +00:00
|
|
|
/*
|
|
|
|
********************************************************************************
|
2013-05-03 21:36:03 +00:00
|
|
|
* Copyright (C) 2005-2013, International Business Machines
|
2006-01-12 19:53:58 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
********************************************************************************
|
|
|
|
*
|
|
|
|
* File WINDTFMT.CPP
|
|
|
|
*
|
|
|
|
********************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
2011-07-23 00:10:30 +00:00
|
|
|
#if U_PLATFORM_HAS_WIN32_API
|
2006-01-12 19:53:58 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
#include "unicode/ures.h"
|
|
|
|
#include "unicode/format.h"
|
|
|
|
#include "unicode/fmtable.h"
|
|
|
|
#include "unicode/datefmt.h"
|
|
|
|
#include "unicode/msgfmt.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"
|
2008-09-18 21:13:52 +00:00
|
|
|
#include "wintzimpl.h"
|
2006-01-12 19:53:58 +00:00
|
|
|
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# define VC_EXTRALEAN
|
|
|
|
# define NOUSER
|
|
|
|
# define NOSERVICE
|
|
|
|
# define NOIME
|
|
|
|
# define NOMCX
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Win32DateFormat)
|
|
|
|
|
|
|
|
#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
|
|
|
|
|
|
|
|
#define NEW_ARRAY(type,count) (type *) uprv_malloc((count) * sizeof(type))
|
|
|
|
#define DELETE_ARRAY(array) uprv_free((void *) (array))
|
|
|
|
|
|
|
|
#define STACK_BUFFER_SIZE 64
|
|
|
|
|
2009-03-16 15:23:11 +00:00
|
|
|
UnicodeString* Win32DateFormat::getTimeDateFormat(const Calendar *cal, const Locale *locale, UErrorCode &status) const
|
2006-01-12 19:53:58 +00:00
|
|
|
{
|
|
|
|
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)) {
|
2007-06-15 17:21:17 +00:00
|
|
|
static const UChar defaultPattern[] = {0x007B, 0x0031, 0x007D, 0x0020, 0x007B, 0x0030, 0x007D, 0x0000}; // "{1} {0}"
|
2006-01-12 19:53:58 +00:00
|
|
|
return new UnicodeString(defaultPattern, ARRAY_SIZE(defaultPattern));
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t resStrLen = 0;
|
2009-03-16 22:25:05 +00:00
|
|
|
int32_t glueIndex = DateFormat::kDateTime;
|
|
|
|
int32_t patSize = ures_getSize(patBundle);
|
2009-03-26 19:06:50 +00:00
|
|
|
if (patSize >= (DateFormat::kDateTimeOffset + DateFormat::kShort + 1)) {
|
2009-03-25 20:25:39 +00:00
|
|
|
// Get proper date time format
|
|
|
|
glueIndex = (int32_t)(DateFormat::kDateTimeOffset + (fDateStyle - DateFormat::kDateOffset));
|
2009-03-16 15:23:11 +00:00
|
|
|
}
|
|
|
|
const UChar *resStr = ures_getStringByIndex(patBundle, glueIndex, &resStrLen, &status);
|
2006-01-12 19:53:58 +00:00
|
|
|
|
|
|
|
result = new UnicodeString(TRUE, resStr, resStrLen);
|
|
|
|
|
|
|
|
ures_close(patBundle);
|
|
|
|
ures_close(typBundle);
|
|
|
|
ures_close(calBundle);
|
|
|
|
ures_close(topBundle);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Range-check timeStyle, dateStyle
|
|
|
|
Win32DateFormat::Win32DateFormat(DateFormat::EStyle timeStyle, DateFormat::EStyle dateStyle, const Locale &locale, UErrorCode &status)
|
2013-05-03 21:36:03 +00:00
|
|
|
: DateFormat(), fDateTimeMsg(NULL), fTimeStyle(timeStyle), fDateStyle(dateStyle), fLocale(locale), fZoneID()
|
2006-01-12 19:53:58 +00:00
|
|
|
{
|
2006-02-06 18:21:06 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
2006-01-12 19:53:58 +00:00
|
|
|
fLCID = locale.getLCID();
|
2006-02-06 18:21:06 +00:00
|
|
|
fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1);
|
|
|
|
uprv_memset(fTZI, 0, sizeof(TIME_ZONE_INFORMATION));
|
2006-01-12 19:53:58 +00:00
|
|
|
adoptCalendar(Calendar::createInstance(locale, status));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Win32DateFormat::Win32DateFormat(const Win32DateFormat &other)
|
|
|
|
: DateFormat(other)
|
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
Win32DateFormat::~Win32DateFormat()
|
|
|
|
{
|
2006-02-06 18:21:06 +00:00
|
|
|
// delete fCalendar;
|
|
|
|
uprv_free(fTZI);
|
2006-01-12 19:53:58 +00:00
|
|
|
delete fDateTimeMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
Win32DateFormat &Win32DateFormat::operator=(const Win32DateFormat &other)
|
|
|
|
{
|
2006-02-06 18:21:06 +00:00
|
|
|
// The following handles fCalendar
|
2006-01-12 19:53:58 +00:00
|
|
|
DateFormat::operator=(other);
|
|
|
|
|
2006-02-06 18:21:06 +00:00
|
|
|
// delete fCalendar;
|
2006-01-12 19:53:58 +00:00
|
|
|
|
|
|
|
this->fDateTimeMsg = other.fDateTimeMsg;
|
|
|
|
this->fTimeStyle = other.fTimeStyle;
|
|
|
|
this->fDateStyle = other.fDateStyle;
|
2013-05-03 21:36:03 +00:00
|
|
|
this->fLocale = other.fLocale;
|
2006-01-12 19:53:58 +00:00
|
|
|
this->fLCID = other.fLCID;
|
2006-02-06 18:21:06 +00:00
|
|
|
// this->fCalendar = other.fCalendar->clone();
|
2006-01-12 19:53:58 +00:00
|
|
|
this->fZoneID = other.fZoneID;
|
|
|
|
|
2006-02-06 18:21:06 +00:00
|
|
|
this->fTZI = NEW_ARRAY(TIME_ZONE_INFORMATION, 1);
|
2006-01-12 19:53:58 +00:00
|
|
|
*this->fTZI = *other.fTZI;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Format *Win32DateFormat::clone(void) 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;
|
|
|
|
|
2006-01-24 22:16:39 +00:00
|
|
|
setTimeZoneInfo(&tzi, tz);
|
2006-01-12 19:53:58 +00:00
|
|
|
|
|
|
|
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 = new UnicodeString();
|
|
|
|
UnicodeString *time = new UnicodeString();
|
|
|
|
UnicodeString *pattern = fDateTimeMsg;
|
|
|
|
Formattable timeDateArray[2];
|
|
|
|
|
|
|
|
formatDate(&st_local, *date);
|
|
|
|
formatTime(&st_local, *time);
|
|
|
|
|
|
|
|
timeDateArray[0].adoptString(time);
|
|
|
|
timeDateArray[1].adoptString(date);
|
|
|
|
|
|
|
|
if (strcmp(fCalendar->getType(), cal.getType()) != 0) {
|
2013-05-03 21:36:03 +00:00
|
|
|
pattern = getTimeDateFormat(&cal, &fLocale, status);
|
2006-01-12 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageFormat::format(*pattern, timeDateArray, 2, appendTo, status);
|
|
|
|
} else if (fDateStyle != DateFormat::kNone) {
|
|
|
|
formatDate(&st_local, appendTo);
|
2006-01-23 23:25:00 +00:00
|
|
|
} else if (fTimeStyle != DateFormat::kNone) {
|
2006-01-12 19:53:58 +00:00
|
|
|
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;
|
2013-05-03 21:36:03 +00:00
|
|
|
fDateTimeMsg = getTimeDateFormat(newCalendar, &fLocale, status);
|
2006-01-12 19:53:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete fCalendar;
|
|
|
|
fCalendar = newCalendar;
|
|
|
|
|
2006-01-24 22:16:39 +00:00
|
|
|
fZoneID = setTimeZoneInfo(fTZI, fCalendar->getTimeZone());
|
2006-01-12 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Win32DateFormat::setCalendar(const Calendar &newCalendar)
|
|
|
|
{
|
|
|
|
adoptCalendar(newCalendar.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Win32DateFormat::adoptTimeZone(TimeZone *zoneToAdopt)
|
|
|
|
{
|
2006-01-24 22:16:39 +00:00
|
|
|
fZoneID = setTimeZoneInfo(fTZI, *zoneToAdopt);
|
2006-01-12 19:53:58 +00:00
|
|
|
fCalendar->adoptTimeZone(zoneToAdopt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Win32DateFormat::setTimeZone(const TimeZone& zone)
|
|
|
|
{
|
2006-01-24 22:16:39 +00:00
|
|
|
fZoneID = setTimeZoneInfo(fTZI, zone);
|
2006-01-12 19:53:58 +00:00
|
|
|
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;
|
|
|
|
UChar stackBuffer[STACK_BUFFER_SIZE];
|
|
|
|
UChar *buffer = stackBuffer;
|
|
|
|
|
2006-01-23 23:25:00 +00:00
|
|
|
result = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, STACK_BUFFER_SIZE);
|
2006-01-12 19:53:58 +00:00
|
|
|
|
|
|
|
if (result == 0) {
|
|
|
|
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
2006-05-05 23:21:23 +00:00
|
|
|
int newLength = GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, NULL, 0);
|
2006-01-12 19:53:58 +00:00
|
|
|
|
|
|
|
buffer = NEW_ARRAY(UChar, newLength);
|
2006-05-05 23:21:23 +00:00
|
|
|
GetDateFormatW(fLCID, dfFlags[fDateStyle - kDateOffset], st, NULL, buffer, newLength);
|
2006-01-12 19:53:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
appendTo.append(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;
|
|
|
|
UChar stackBuffer[STACK_BUFFER_SIZE];
|
|
|
|
UChar *buffer = stackBuffer;
|
|
|
|
|
|
|
|
result = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, STACK_BUFFER_SIZE);
|
|
|
|
|
|
|
|
if (result == 0) {
|
|
|
|
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
int newLength = GetTimeFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, NULL, 0);
|
|
|
|
|
|
|
|
buffer = NEW_ARRAY(UChar, newLength);
|
|
|
|
GetDateFormatW(fLCID, tfFlags[fTimeStyle], st, NULL, buffer, newLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
appendTo.append(buffer, (int32_t) wcslen(buffer));
|
|
|
|
|
|
|
|
if (buffer != stackBuffer) {
|
|
|
|
DELETE_ARRAY(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-24 22:16:39 +00:00
|
|
|
UnicodeString Win32DateFormat::setTimeZoneInfo(TIME_ZONE_INFORMATION *tzi, const TimeZone &zone) const
|
2006-01-12 19:53:58 +00:00
|
|
|
{
|
|
|
|
UnicodeString zoneID;
|
|
|
|
|
|
|
|
zone.getID(zoneID);
|
|
|
|
|
|
|
|
if (zoneID.compare(fZoneID) != 0) {
|
|
|
|
UnicodeString icuid;
|
|
|
|
|
|
|
|
zone.getID(icuid);
|
2006-01-24 22:16:39 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
if (found = uprv_getWindowsTimeZoneInfo(tzi, equiv.getBuffer(), equiv.length())) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! found) {
|
|
|
|
GetTimeZoneInformation(tzi);
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 19:53:58 +00:00
|
|
|
}
|
2006-01-24 22:16:39 +00:00
|
|
|
|
|
|
|
return zoneID;
|
2006-01-12 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
2011-07-23 00:10:30 +00:00
|
|
|
#endif // U_PLATFORM_HAS_WIN32_API
|
2006-01-12 19:53:58 +00:00
|
|
|
|