2007-04-09 22:54:03 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2008-01-04 01:21:37 +00:00
|
|
|
* Copyright (C) 2007-2008, International Business Machines Corporation and *
|
2007-04-09 22:54:03 +00:00
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
//#define DEBUG_RELDTFMT
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "reldtfmt.h"
|
|
|
|
#include "unicode/msgfmt.h"
|
|
|
|
|
|
|
|
#include "gregoimp.h" // for CalendarData
|
2007-04-09 23:11:21 +00:00
|
|
|
#include "cmemory.h"
|
2007-04-09 22:54:03 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of URelativeString structs is used to store the resource data loaded out of the bundle.
|
|
|
|
*/
|
|
|
|
struct URelativeString {
|
|
|
|
int32_t offset; /** offset of this item, such as, the relative date **/
|
|
|
|
int32_t len; /** length of the string **/
|
2007-06-15 02:35:33 +00:00
|
|
|
const UChar* string; /** string, or NULL if not set **/
|
2007-04-09 22:54:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RelativeDateFormat)
|
|
|
|
|
|
|
|
RelativeDateFormat::RelativeDateFormat(const RelativeDateFormat& other) :
|
2007-04-10 22:57:25 +00:00
|
|
|
DateFormat(other), fDateFormat(NULL), fTimeFormat(NULL), fCombinedFormat(NULL),
|
|
|
|
fDateStyle(other.fDateStyle), fTimeStyle(other.fTimeStyle), fLocale(other.fLocale),
|
2007-06-24 04:15:38 +00:00
|
|
|
fDayMin(other.fDayMin), fDayMax(other.fDayMax),
|
|
|
|
fDatesLen(other.fDatesLen), fDates(NULL)
|
2007-04-10 22:57:25 +00:00
|
|
|
{
|
2007-04-09 22:54:03 +00:00
|
|
|
if(other.fDateFormat != NULL) {
|
|
|
|
fDateFormat = (DateFormat*)other.fDateFormat->clone();
|
|
|
|
} else {
|
|
|
|
fDateFormat = NULL;
|
|
|
|
}
|
2007-06-15 02:35:33 +00:00
|
|
|
if (fDatesLen > 0) {
|
|
|
|
fDates = (URelativeString*) uprv_malloc(sizeof(fDates[0])*fDatesLen);
|
|
|
|
uprv_memcpy(fDates, other.fDates, sizeof(fDates[0])*fDatesLen);
|
|
|
|
}
|
|
|
|
//fCalendar = other.fCalendar->clone();
|
2007-04-10 22:57:25 +00:00
|
|
|
/*
|
2007-04-09 22:54:03 +00:00
|
|
|
if(other.fTimeFormat != NULL) {
|
|
|
|
fTimeFormat = (DateFormat*)other.fTimeFormat->clone();
|
|
|
|
} else {
|
|
|
|
fTimeFormat = NULL;
|
|
|
|
}
|
2007-04-10 22:57:25 +00:00
|
|
|
*/
|
2007-04-09 22:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RelativeDateFormat::RelativeDateFormat( UDateFormatStyle timeStyle, UDateFormatStyle dateStyle, const Locale& locale, UErrorCode& status)
|
2007-08-29 06:13:07 +00:00
|
|
|
: DateFormat(), fDateFormat(NULL), fTimeFormat(NULL), fCombinedFormat(NULL),
|
|
|
|
fDateStyle(dateStyle), fTimeStyle(timeStyle), fLocale(locale), fDatesLen(0), fDates(NULL)
|
2007-04-10 22:57:25 +00:00
|
|
|
{
|
2007-04-09 22:54:03 +00:00
|
|
|
if(U_FAILURE(status) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-04-10 22:57:25 +00:00
|
|
|
if(fDateStyle != UDAT_NONE) {
|
|
|
|
EStyle newStyle = (EStyle)(fDateStyle & ~UDAT_RELATIVE);
|
2007-04-09 22:54:03 +00:00
|
|
|
// Create a DateFormat in the non-relative style requested.
|
|
|
|
fDateFormat = createDateInstance(newStyle, locale);
|
|
|
|
}
|
2007-04-10 22:57:25 +00:00
|
|
|
if(fTimeStyle != UDAT_NONE) {
|
2007-04-09 22:54:03 +00:00
|
|
|
// don't support time style, for now
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the parent fCalendar, so that parse() works correctly.
|
|
|
|
initializeCalendar(NULL, locale, status);
|
2007-06-15 02:35:33 +00:00
|
|
|
loadDates(status);
|
2007-04-09 22:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RelativeDateFormat::~RelativeDateFormat() {
|
|
|
|
delete fDateFormat;
|
|
|
|
delete fTimeFormat;
|
|
|
|
delete fCombinedFormat;
|
2007-04-10 22:57:25 +00:00
|
|
|
uprv_free(fDates);
|
2007-04-09 22:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Format* RelativeDateFormat::clone(void) const {
|
2007-06-15 02:35:33 +00:00
|
|
|
return new RelativeDateFormat(*this);
|
2007-04-09 22:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UBool RelativeDateFormat::operator==(const Format& other) const {
|
|
|
|
if(DateFormat::operator==(other)) {
|
|
|
|
// DateFormat::operator== guarantees following cast is safe
|
|
|
|
RelativeDateFormat* that = (RelativeDateFormat*)&other;
|
2007-04-10 22:57:25 +00:00
|
|
|
return (fDateStyle==that->fDateStyle &&
|
|
|
|
fTimeStyle==that->fTimeStyle &&
|
|
|
|
fLocale==that->fLocale);
|
2007-04-09 22:54:03 +00:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& RelativeDateFormat::format( Calendar& cal,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& pos) const {
|
|
|
|
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
// calculate the difference, in days, between 'cal' and now.
|
|
|
|
int dayDiff = dayDifference(cal, status);
|
|
|
|
|
|
|
|
// look up string
|
|
|
|
int32_t len;
|
|
|
|
const UChar *theString = getStringForDay(dayDiff, len, status);
|
|
|
|
|
|
|
|
if(U_FAILURE(status) || (theString==NULL)) {
|
|
|
|
// didn't find it. Fall through to the fDateFormat
|
|
|
|
if(fDateFormat != NULL) {
|
|
|
|
return fDateFormat->format(cal,appendTo,pos);
|
|
|
|
} else {
|
|
|
|
return appendTo; // no op
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// found a relative string
|
|
|
|
return appendTo.append(theString, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-10 22:57:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
RelativeDateFormat::format(const Formattable& obj,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& pos,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
|
|
|
// this is just here to get around the hiding problem
|
|
|
|
// (the previous format() override would hide the version of
|
|
|
|
// format() on DateFormat that this function correspond to, so we
|
|
|
|
// have to redefine it here)
|
|
|
|
return DateFormat::format(obj, appendTo, pos, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-09 22:54:03 +00:00
|
|
|
void RelativeDateFormat::parse( const UnicodeString& text,
|
|
|
|
Calendar& cal,
|
|
|
|
ParsePosition& pos) const {
|
|
|
|
|
|
|
|
// Can the fDateFormat parse it?
|
|
|
|
if(fDateFormat != NULL) {
|
|
|
|
ParsePosition aPos(pos);
|
|
|
|
fDateFormat->parse(text,cal,aPos);
|
|
|
|
if((aPos.getIndex() != pos.getIndex()) &&
|
|
|
|
(aPos.getErrorIndex()==-1)) {
|
|
|
|
pos=aPos; // copy the sub parse
|
|
|
|
return; // parsed subfmt OK
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linear search the relative strings
|
2007-04-10 22:57:25 +00:00
|
|
|
for(int n=0;n<fDatesLen;n++) {
|
|
|
|
if(fDates[n].string != NULL &&
|
2007-04-09 22:54:03 +00:00
|
|
|
(0==text.compare(pos.getIndex(),
|
2007-04-10 22:57:25 +00:00
|
|
|
fDates[n].len,
|
|
|
|
fDates[n].string))) {
|
2007-04-09 22:54:03 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
// Set the calendar to now+offset
|
|
|
|
cal.setTime(Calendar::getNow(),status);
|
2007-04-10 22:57:25 +00:00
|
|
|
cal.add(UCAL_DATE,fDates[n].offset, status);
|
2007-04-09 22:54:03 +00:00
|
|
|
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
// failure in setting calendar fields
|
2007-04-10 22:57:25 +00:00
|
|
|
pos.setErrorIndex(pos.getIndex()+fDates[n].len);
|
2007-04-09 22:54:03 +00:00
|
|
|
} else {
|
2007-04-10 22:57:25 +00:00
|
|
|
pos.setIndex(pos.getIndex()+fDates[n].len);
|
2007-04-09 22:54:03 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse failed
|
|
|
|
}
|
|
|
|
|
2007-04-10 22:57:25 +00:00
|
|
|
UDate
|
|
|
|
RelativeDateFormat::parse( const UnicodeString& text,
|
|
|
|
ParsePosition& pos) const {
|
|
|
|
// redefined here because the other parse() function hides this function's
|
|
|
|
// cunterpart on DateFormat
|
|
|
|
return DateFormat::parse(text, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
UDate
|
|
|
|
RelativeDateFormat::parse(const UnicodeString& text, UErrorCode& status) const
|
|
|
|
{
|
|
|
|
// redefined here because the other parse() function hides this function's
|
|
|
|
// counterpart on DateFormat
|
|
|
|
return DateFormat::parse(text, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-09 22:54:03 +00:00
|
|
|
const UChar *RelativeDateFormat::getStringForDay(int32_t day, int32_t &len, UErrorCode &status) const {
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is it outside the resource bundle's range?
|
2007-04-10 22:57:25 +00:00
|
|
|
if(day < fDayMin || day > fDayMax) {
|
2007-04-09 22:54:03 +00:00
|
|
|
return NULL; // don't have it.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linear search the held strings
|
2007-04-10 22:57:25 +00:00
|
|
|
for(int n=0;n<fDatesLen;n++) {
|
|
|
|
if(fDates[n].offset == day) {
|
|
|
|
len = fDates[n].len;
|
|
|
|
return fDates[n].string;
|
2007-04-09 22:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL; // not found.
|
|
|
|
}
|
|
|
|
|
2007-06-15 02:35:33 +00:00
|
|
|
void RelativeDateFormat::loadDates(UErrorCode &status) {
|
|
|
|
CalendarData calData(fLocale, "gregorian", status);
|
|
|
|
UResourceBundle *strings = calData.getByKey3("fields", "day", "relative", status);
|
2007-04-09 22:54:03 +00:00
|
|
|
// set up min/max
|
2007-06-15 02:35:33 +00:00
|
|
|
fDayMin=-1;
|
|
|
|
fDayMax=1;
|
2007-04-09 22:54:03 +00:00
|
|
|
|
|
|
|
if(U_FAILURE(status)) {
|
2007-06-15 02:35:33 +00:00
|
|
|
fDatesLen=0;
|
2007-04-09 22:54:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-15 02:35:33 +00:00
|
|
|
fDatesLen = ures_getSize(strings);
|
|
|
|
fDates = (URelativeString*) uprv_malloc(sizeof(fDates[0])*fDatesLen);
|
2007-04-09 22:54:03 +00:00
|
|
|
|
|
|
|
// Load in each item into the array...
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
UResourceBundle *subString = NULL;
|
|
|
|
|
|
|
|
while(ures_hasNext(strings) && U_SUCCESS(status)) { // iterate over items
|
|
|
|
subString = ures_getNextResource(strings, subString, &status);
|
|
|
|
|
|
|
|
if(U_FAILURE(status) || (subString==NULL)) break;
|
|
|
|
|
|
|
|
// key = offset #
|
|
|
|
const char *key = ures_getKey(subString);
|
|
|
|
|
|
|
|
// load the string and length
|
|
|
|
int32_t aLen;
|
|
|
|
const UChar* aString = ures_getString(subString, &aLen, &status);
|
|
|
|
|
|
|
|
if(U_FAILURE(status) || aString == NULL) break;
|
|
|
|
|
|
|
|
// calculate the offset
|
|
|
|
int32_t offset = atoi(key);
|
|
|
|
|
|
|
|
// set min/max
|
2007-04-10 22:57:25 +00:00
|
|
|
if(offset < fDayMin) {
|
2007-06-15 02:35:33 +00:00
|
|
|
fDayMin = offset;
|
2007-04-09 22:54:03 +00:00
|
|
|
}
|
2007-04-10 22:57:25 +00:00
|
|
|
if(offset > fDayMax) {
|
2007-06-15 02:35:33 +00:00
|
|
|
fDayMax = offset;
|
2007-04-09 22:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy the string pointer
|
2007-06-15 02:35:33 +00:00
|
|
|
fDates[n].offset = offset;
|
|
|
|
fDates[n].string = aString;
|
|
|
|
fDates[n].len = aLen;
|
2007-04-09 22:54:03 +00:00
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
2007-06-15 02:35:33 +00:00
|
|
|
ures_close(subString);
|
2007-04-09 22:54:03 +00:00
|
|
|
|
2007-04-10 22:57:25 +00:00
|
|
|
// the fDates[] array could be sorted here, for direct access.
|
2007-04-09 22:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// this should to be in DateFormat, instead it was copied from SimpleDateFormat.
|
|
|
|
|
|
|
|
Calendar*
|
|
|
|
RelativeDateFormat::initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status)
|
|
|
|
{
|
|
|
|
if(!U_FAILURE(status)) {
|
|
|
|
fCalendar = Calendar::createInstance(adoptZone?adoptZone:TimeZone::createDefault(), locale, status);
|
|
|
|
}
|
|
|
|
if (U_SUCCESS(status) && fCalendar == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
return fCalendar;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t RelativeDateFormat::dayDifference(Calendar &cal, UErrorCode &status) {
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2008-01-04 01:21:37 +00:00
|
|
|
// TODO: Cache the nowCal to avoid heap allocs? Would be difficult, don't know the calendar type
|
2007-04-09 22:54:03 +00:00
|
|
|
Calendar *nowCal = cal.clone();
|
|
|
|
nowCal->setTime(Calendar::getNow(), status);
|
2008-01-04 01:21:37 +00:00
|
|
|
|
|
|
|
// For the day difference, we are interested in the difference in the (modified) julian day number
|
|
|
|
// which is midnight to midnight. Using fieldDifference() is NOT correct here, because
|
|
|
|
// 6pm Jan 4th to 10am Jan 5th should be considered "tomorrow".
|
|
|
|
int32_t dayDiff = cal.get(UCAL_JULIAN_DAY, status) - nowCal->get(UCAL_JULIAN_DAY, status);
|
|
|
|
|
2007-04-09 22:54:03 +00:00
|
|
|
delete nowCal;
|
|
|
|
return dayDiff;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2007-04-09 23:11:21 +00:00
|
|
|
#endif
|
|
|
|
|