/*******************************************************************************
* Copyright (C) 2008-2014, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*
* File DTITVFMT.CPP
*
*******************************************************************************
*/
#include "utypeinfo.h" // for 'typeid' to work
#include "unicode/dtitvfmt.h"
#if !UCONFIG_NO_FORMATTING
//TODO: put in compilation
//#define DTITVFMT_DEBUG 1
#include "cstring.h"
#include "unicode/msgfmt.h"
#include "unicode/dtptngen.h"
#include "unicode/dtitvinf.h"
#include "unicode/calendar.h"
#include "dtitv_impl.h"
#ifdef DTITVFMT_DEBUG
#include <iostream>
#include "cstring.h"
#endif
#include "gregoimp.h"
U_NAMESPACE_BEGIN
#ifdef DTITVFMT_DEBUG
#define PRINTMESG(msg) { std::cout << "(" << __FILE__ << ":" << __LINE__ << ") " << msg << "\n"; }
#endif
static const UChar gDateFormatSkeleton[][11] = {
//yMMMMEEEEd
{LOW_Y, CAP_M, CAP_M, CAP_M, CAP_M, CAP_E, CAP_E, CAP_E, CAP_E, LOW_D, 0},
//yMMMMd
{LOW_Y, CAP_M, CAP_M, CAP_M, CAP_M, LOW_D, 0},
//yMMMd
{LOW_Y, CAP_M, CAP_M, CAP_M, LOW_D, 0},
//yMd
{LOW_Y, CAP_M, LOW_D, 0} };
static const char gDateTimePatternsTag[]="DateTimePatterns";
// latestFirst:
static const UChar gLaterFirstPrefix[] = {LOW_L, LOW_A, LOW_T, LOW_E, LOW_S,LOW_T, CAP_F, LOW_I, LOW_R, LOW_S, LOW_T, COLON};
// earliestFirst:
static const UChar gEarlierFirstPrefix[] = {LOW_E, LOW_A, LOW_R, LOW_L, LOW_I, LOW_E, LOW_S, LOW_T, CAP_F, LOW_I, LOW_R, LOW_S, LOW_T, COLON};
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateIntervalFormat)
DateIntervalFormat* U_EXPORT2
DateIntervalFormat::createInstance(const UnicodeString& skeleton,
UErrorCode& status) {
return createInstance(skeleton, Locale::getDefault(), status);
}
DateIntervalFormat* U_EXPORT2
DateIntervalFormat::createInstance(const UnicodeString& skeleton,
const Locale& locale,
UErrorCode& status) {
#ifdef DTITVFMT_DEBUG
char result[1000];
char result_1[1000];
char mesg[2000];
skeleton.extract(0, skeleton.length(), result, "UTF-8");
UnicodeString pat;
((SimpleDateFormat*)dtfmt)->toPattern(pat);
pat.extract(0, pat.length(), result_1, "UTF-8");
sprintf(mesg, "skeleton: %s; pattern: %s\n", result, result_1);
PRINTMESG(mesg)
#endif
DateIntervalInfo* dtitvinf = new DateIntervalInfo(locale, status);
return create(locale, dtitvinf, &skeleton, status);
}
DateIntervalFormat* U_EXPORT2
DateIntervalFormat::createInstance(const UnicodeString& skeleton,
const DateIntervalInfo& dtitvinf,
UErrorCode& status) {
return createInstance(skeleton, Locale::getDefault(), dtitvinf, status);
}
DateIntervalFormat* U_EXPORT2
DateIntervalFormat::createInstance(const UnicodeString& skeleton,
const Locale& locale,
const DateIntervalInfo& dtitvinf,
UErrorCode& status) {
DateIntervalInfo* ptn = dtitvinf.clone();
return create(locale, ptn, &skeleton, status);
}
DateIntervalFormat::DateIntervalFormat()
: fInfo(NULL),
fDateFormat(NULL),
fFromCalendar(NULL),
fToCalendar(NULL),
fDtpng(NULL)
{}
DateIntervalFormat::DateIntervalFormat(const DateIntervalFormat& itvfmt)
: Format(itvfmt),
fInfo(NULL),
fDateFormat(NULL),
fFromCalendar(NULL),
fToCalendar(NULL),
fDtpng(NULL) {
*this = itvfmt;
}
DateIntervalFormat&
DateIntervalFormat::operator=(const DateIntervalFormat& itvfmt) {
if ( this != &itvfmt ) {
delete fDateFormat;
delete fInfo;
delete fFromCalendar;
delete fToCalendar;
delete fDtpng;
if ( itvfmt.fDateFormat ) {
fDateFormat = (SimpleDateFormat*)itvfmt.fDateFormat->clone();
} else {
fDateFormat = NULL;
}
if ( itvfmt.fInfo ) {
fInfo = itvfmt.fInfo->clone();
} else {
fInfo = NULL;
}
if ( itvfmt.fFromCalendar ) {
fFromCalendar = itvfmt.fFromCalendar->clone();
} else {
fFromCalendar = NULL;
}
if ( itvfmt.fToCalendar ) {
fToCalendar = itvfmt.fToCalendar->clone();
} else {
fToCalendar = NULL;
}
fSkeleton = itvfmt.fSkeleton;
int8_t i;
for ( i = 0; i< DateIntervalInfo::kIPI_MAX_INDEX; ++i ) {
fIntervalPatterns[i] = itvfmt.fIntervalPatterns[i];
}
if (itvfmt.fDtpng) {
fDtpng = itvfmt.fDtpng->clone();
}
}
return *this;
}
DateIntervalFormat::~DateIntervalFormat() {
delete fInfo;
delete fDateFormat;
delete fFromCalendar;
delete fToCalendar;
delete fDtpng;
}
Format*
DateIntervalFormat::clone(void) const {
return new DateIntervalFormat(*this);
}
UBool
DateIntervalFormat::operator==(const Format& other) const {
if (typeid(*this) == typeid(other)) {
const DateIntervalFormat* fmt = (DateIntervalFormat*)&other;
#ifdef DTITVFMT_DEBUG
UBool equal;
equal = (this == fmt);
equal = (*fInfo == *fmt->fInfo);
equal = (*fDateFormat == *fmt->fDateFormat);
equal = fFromCalendar->isEquivalentTo(*fmt->fFromCalendar) ;
equal = fToCalendar->isEquivalentTo(*fmt->fToCalendar) ;
equal = (fSkeleton == fmt->fSkeleton);
#endif
UBool res;
res = ( this == fmt ) ||
( Format::operator==(other) &&
fInfo &&
( *fInfo == *fmt->fInfo ) &&
fDateFormat &&
( *fDateFormat == *fmt->fDateFormat ) &&
fFromCalendar &&
fFromCalendar->isEquivalentTo(*fmt->fFromCalendar) &&
fToCalendar &&
fToCalendar->isEquivalentTo(*fmt->fToCalendar) &&
fSkeleton == fmt->fSkeleton &&
fDtpng &&
(*fDtpng == *fmt->fDtpng) );
int8_t i;
for (i = 0; i< DateIntervalInfo::kIPI_MAX_INDEX && res == TRUE; ++i ) {
res = ( fIntervalPatterns[i].firstPart ==
fmt->fIntervalPatterns[i].firstPart) &&
( fIntervalPatterns[i].secondPart ==
fmt->fIntervalPatterns[i].secondPart ) &&
( fIntervalPatterns[i].laterDateFirst ==
fmt->fIntervalPatterns[i].laterDateFirst) ;
}
return res;
}
return FALSE;
}
UnicodeString&
DateIntervalFormat::format(const Formattable& obj,
UnicodeString& appendTo,
FieldPosition& fieldPosition,
UErrorCode& status) const {
if ( U_FAILURE(status) ) {
return appendTo;
}
if ( obj.getType() == Formattable::kObject ) {
const UObject* formatObj = obj.getObject();
const DateInterval* interval = dynamic_cast<const DateInterval*>(formatObj);
if (interval != NULL){
return format(interval, appendTo, fieldPosition, status);
}
}
status = U_ILLEGAL_ARGUMENT_ERROR;
return appendTo;
}
UnicodeString&
DateIntervalFormat::format(const DateInterval* dtInterval,
UnicodeString& appendTo,
FieldPosition& fieldPosition,
UErrorCode& status) const {
if ( U_FAILURE(status) ) {
return appendTo;
}
if ( fFromCalendar != NULL && fToCalendar != NULL &&
fDateFormat != NULL && fInfo != NULL ) {
fFromCalendar->setTime(dtInterval->getFromDate(), status);
fToCalendar->setTime(dtInterval->getToDate(), status);
if ( U_SUCCESS(status) ) {
return format(*fFromCalendar, *fToCalendar, appendTo,fieldPosition