2013-11-22 18:26:22 +00:00
|
|
|
/*
|
2014-01-11 00:30:39 +00:00
|
|
|
******************************************************************************
|
|
|
|
* Copyright (C) 2014, International Business Machines Corporation and
|
2013-11-22 18:26:22 +00:00
|
|
|
* others. All Rights Reserved.
|
2014-01-11 00:30:39 +00:00
|
|
|
******************************************************************************
|
2013-11-22 18:26:22 +00:00
|
|
|
*
|
|
|
|
* File RELDATEFMT.CPP
|
2014-01-11 00:30:39 +00:00
|
|
|
******************************************************************************
|
2013-11-22 18:26:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/reldatefmt.h"
|
|
|
|
|
2014-07-02 05:09:05 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION
|
2013-12-03 04:48:00 +00:00
|
|
|
|
2013-11-22 18:26:22 +00:00
|
|
|
#include "unicode/localpointer.h"
|
2014-01-29 05:08:55 +00:00
|
|
|
#include "quantityformatter.h"
|
2013-11-22 18:26:22 +00:00
|
|
|
#include "unicode/plurrule.h"
|
|
|
|
#include "unicode/msgfmt.h"
|
|
|
|
#include "unicode/decimfmt.h"
|
|
|
|
#include "unicode/numfmt.h"
|
2014-05-22 22:46:53 +00:00
|
|
|
#include "unicode/brkiter.h"
|
2013-11-22 18:26:22 +00:00
|
|
|
#include "uresimp.h"
|
|
|
|
#include "unicode/ures.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "ucln_in.h"
|
2013-11-22 23:08:24 +00:00
|
|
|
#include "mutex.h"
|
2014-01-11 00:30:39 +00:00
|
|
|
#include "charstr.h"
|
2014-05-22 23:21:32 +00:00
|
|
|
#include "uassert.h"
|
2013-11-22 18:26:22 +00:00
|
|
|
|
2014-05-22 22:46:53 +00:00
|
|
|
#include "sharedbreakiterator.h"
|
2014-02-10 19:13:50 +00:00
|
|
|
#include "sharedpluralrules.h"
|
|
|
|
#include "sharednumberformat.h"
|
2014-08-20 21:46:02 +00:00
|
|
|
#include "unifiedcache.h"
|
2013-11-22 18:26:22 +00:00
|
|
|
|
2014-01-11 00:30:39 +00:00
|
|
|
// Copied from uscript_props.cpp
|
|
|
|
|
2014-05-22 22:46:53 +00:00
|
|
|
static UMutex gBrkIterMutex = U_MUTEX_INITIALIZER;
|
2013-11-22 18:26:22 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
// RelativeDateTimeFormatter specific data for a single locale
|
|
|
|
class RelativeDateTimeCacheData: public SharedObject {
|
2013-11-22 18:26:22 +00:00
|
|
|
public:
|
2014-02-10 19:13:50 +00:00
|
|
|
RelativeDateTimeCacheData() : combinedDateAndTime(NULL) { }
|
|
|
|
virtual ~RelativeDateTimeCacheData();
|
2013-11-22 18:26:22 +00:00
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
// no numbers: e.g Next Tuesday; Yesterday; etc.
|
2014-06-11 19:45:30 +00:00
|
|
|
UnicodeString absoluteUnits[UDAT_STYLE_COUNT][UDAT_ABSOLUTE_UNIT_COUNT][UDAT_DIRECTION_COUNT];
|
2013-11-22 18:26:22 +00:00
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
// has numbers: e.g Next Tuesday; Yesterday; etc. For second index, 0
|
|
|
|
// means past e.g 5 days ago; 1 means future e.g in 5 days.
|
2014-06-11 19:45:30 +00:00
|
|
|
QuantityFormatter relativeUnits[UDAT_STYLE_COUNT][UDAT_RELATIVE_UNIT_COUNT][2];
|
2014-02-10 19:13:50 +00:00
|
|
|
|
|
|
|
void adoptCombinedDateAndTime(MessageFormat *mfToAdopt) {
|
|
|
|
delete combinedDateAndTime;
|
|
|
|
combinedDateAndTime = mfToAdopt;
|
|
|
|
}
|
|
|
|
const MessageFormat *getCombinedDateAndTime() const {
|
|
|
|
return combinedDateAndTime;
|
|
|
|
}
|
2013-11-22 18:26:22 +00:00
|
|
|
private:
|
2014-02-10 19:13:50 +00:00
|
|
|
MessageFormat *combinedDateAndTime;
|
|
|
|
RelativeDateTimeCacheData(const RelativeDateTimeCacheData &other);
|
|
|
|
RelativeDateTimeCacheData& operator=(
|
|
|
|
const RelativeDateTimeCacheData &other);
|
2013-11-22 18:26:22 +00:00
|
|
|
};
|
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
RelativeDateTimeCacheData::~RelativeDateTimeCacheData() {
|
|
|
|
delete combinedDateAndTime;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 00:30:39 +00:00
|
|
|
static UBool getStringWithFallback(
|
2013-11-22 18:26:22 +00:00
|
|
|
const UResourceBundle *resource,
|
|
|
|
const char *key,
|
|
|
|
UnicodeString &result,
|
|
|
|
UErrorCode &status) {
|
|
|
|
int32_t len = 0;
|
|
|
|
const UChar *resStr = ures_getStringByKeyWithFallback(
|
|
|
|
resource, key, &len, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
2014-01-11 00:30:39 +00:00
|
|
|
return FALSE;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
result.setTo(TRUE, resStr, len);
|
2014-01-11 00:30:39 +00:00
|
|
|
return TRUE;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 00:30:39 +00:00
|
|
|
static UBool getOptionalStringWithFallback(
|
2013-11-22 18:26:22 +00:00
|
|
|
const UResourceBundle *resource,
|
|
|
|
const char *key,
|
|
|
|
UnicodeString &result,
|
|
|
|
UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
2014-01-11 00:30:39 +00:00
|
|
|
return FALSE;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
int32_t len = 0;
|
|
|
|
const UChar *resStr = ures_getStringByKey(
|
|
|
|
resource, key, &len, &status);
|
|
|
|
if (status == U_MISSING_RESOURCE_ERROR) {
|
|
|
|
result.remove();
|
|
|
|
status = U_ZERO_ERROR;
|
2014-01-11 00:30:39 +00:00
|
|
|
return TRUE;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
2014-01-11 00:30:39 +00:00
|
|
|
return FALSE;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
result.setTo(TRUE, resStr, len);
|
2014-01-11 00:30:39 +00:00
|
|
|
return TRUE;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 00:30:39 +00:00
|
|
|
static UBool getString(
|
2013-11-22 18:26:22 +00:00
|
|
|
const UResourceBundle *resource,
|
|
|
|
UnicodeString &result,
|
|
|
|
UErrorCode &status) {
|
|
|
|
int32_t len = 0;
|
|
|
|
const UChar *resStr = ures_getString(resource, &len, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
2014-01-11 00:30:39 +00:00
|
|
|
return FALSE;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
result.setTo(TRUE, resStr, len);
|
2014-01-11 00:30:39 +00:00
|
|
|
return TRUE;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 00:30:39 +00:00
|
|
|
static UBool getStringByIndex(
|
2013-11-22 18:26:22 +00:00
|
|
|
const UResourceBundle *resource,
|
|
|
|
int32_t idx,
|
|
|
|
UnicodeString &result,
|
|
|
|
UErrorCode &status) {
|
|
|
|
int32_t len = 0;
|
|
|
|
const UChar *resStr = ures_getStringByIndex(
|
|
|
|
resource, idx, &len, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
2014-01-11 00:30:39 +00:00
|
|
|
return FALSE;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
result.setTo(TRUE, resStr, len);
|
2014-01-11 00:30:39 +00:00
|
|
|
return TRUE;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
static void initAbsoluteUnit(
|
2013-11-22 18:26:22 +00:00
|
|
|
const UResourceBundle *resource,
|
|
|
|
const UnicodeString &unitName,
|
2014-02-10 19:13:50 +00:00
|
|
|
UnicodeString *absoluteUnit,
|
2013-11-22 18:26:22 +00:00
|
|
|
UErrorCode &status) {
|
|
|
|
getStringWithFallback(
|
|
|
|
resource,
|
|
|
|
"-1",
|
2014-02-10 19:13:50 +00:00
|
|
|
absoluteUnit[UDAT_DIRECTION_LAST],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
|
|
|
getStringWithFallback(
|
|
|
|
resource,
|
|
|
|
"0",
|
2014-02-10 19:13:50 +00:00
|
|
|
absoluteUnit[UDAT_DIRECTION_THIS],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
|
|
|
getStringWithFallback(
|
|
|
|
resource,
|
|
|
|
"1",
|
2014-02-10 19:13:50 +00:00
|
|
|
absoluteUnit[UDAT_DIRECTION_NEXT],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
|
|
|
getOptionalStringWithFallback(
|
|
|
|
resource,
|
|
|
|
"-2",
|
2014-02-10 19:13:50 +00:00
|
|
|
absoluteUnit[UDAT_DIRECTION_LAST_2],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
|
|
|
getOptionalStringWithFallback(
|
|
|
|
resource,
|
|
|
|
"2",
|
2014-02-10 19:13:50 +00:00
|
|
|
absoluteUnit[UDAT_DIRECTION_NEXT_2],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-02-10 19:13:50 +00:00
|
|
|
absoluteUnit[UDAT_DIRECTION_PLAIN] = unitName;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
static void initQuantityFormatter(
|
2013-11-22 18:26:22 +00:00
|
|
|
const UResourceBundle *resource,
|
2014-02-10 19:13:50 +00:00
|
|
|
QuantityFormatter &formatter,
|
2013-11-22 18:26:22 +00:00
|
|
|
UErrorCode &status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int32_t size = ures_getSize(resource);
|
|
|
|
for (int32_t i = 0; i < size; ++i) {
|
|
|
|
LocalUResourceBundlePointer pluralBundle(
|
|
|
|
ures_getByIndex(resource, i, NULL, &status));
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-01-29 05:08:55 +00:00
|
|
|
UnicodeString rawPattern;
|
|
|
|
if (!getString(pluralBundle.getAlias(), rawPattern, status)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
if (!formatter.add(
|
|
|
|
ures_getKey(pluralBundle.getAlias()),
|
|
|
|
rawPattern,
|
|
|
|
status)) {
|
2014-01-29 05:08:55 +00:00
|
|
|
return;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
static void initRelativeUnit(
|
2013-11-22 18:26:22 +00:00
|
|
|
const UResourceBundle *resource,
|
2014-02-10 19:13:50 +00:00
|
|
|
QuantityFormatter *relativeUnit,
|
2013-11-22 18:26:22 +00:00
|
|
|
UErrorCode &status) {
|
|
|
|
LocalUResourceBundlePointer topLevel(
|
2014-01-11 00:30:39 +00:00
|
|
|
ures_getByKeyWithFallback(
|
|
|
|
resource, "relativeTime", NULL, &status));
|
2013-11-22 18:26:22 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LocalUResourceBundlePointer futureBundle(ures_getByKeyWithFallback(
|
|
|
|
topLevel.getAlias(), "future", NULL, &status));
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
initQuantityFormatter(
|
2013-11-22 18:26:22 +00:00
|
|
|
futureBundle.getAlias(),
|
2014-02-10 19:13:50 +00:00
|
|
|
relativeUnit[1],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
|
|
|
LocalUResourceBundlePointer pastBundle(ures_getByKeyWithFallback(
|
|
|
|
topLevel.getAlias(), "past", NULL, &status));
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
initQuantityFormatter(
|
2013-11-22 18:26:22 +00:00
|
|
|
pastBundle.getAlias(),
|
2014-02-10 19:13:50 +00:00
|
|
|
relativeUnit[0],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
|
|
|
}
|
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
static void initRelativeUnit(
|
2013-11-22 18:26:22 +00:00
|
|
|
const UResourceBundle *resource,
|
|
|
|
const char *path,
|
2014-02-10 19:13:50 +00:00
|
|
|
QuantityFormatter *relativeUnit,
|
2013-11-22 18:26:22 +00:00
|
|
|
UErrorCode &status) {
|
|
|
|
LocalUResourceBundlePointer topLevel(
|
|
|
|
ures_getByKeyWithFallback(resource, path, NULL, &status));
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
initRelativeUnit(topLevel.getAlias(), relativeUnit, status);
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void addTimeUnit(
|
|
|
|
const UResourceBundle *resource,
|
|
|
|
const char *path,
|
2014-02-10 19:13:50 +00:00
|
|
|
QuantityFormatter *relativeUnit,
|
|
|
|
UnicodeString *absoluteUnit,
|
2013-11-22 18:26:22 +00:00
|
|
|
UErrorCode &status) {
|
|
|
|
LocalUResourceBundlePointer topLevel(
|
|
|
|
ures_getByKeyWithFallback(resource, path, NULL, &status));
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
initRelativeUnit(topLevel.getAlias(), relativeUnit, status);
|
2013-11-22 18:26:22 +00:00
|
|
|
UnicodeString unitName;
|
2014-01-11 00:30:39 +00:00
|
|
|
if (!getStringWithFallback(topLevel.getAlias(), "dn", unitName, status)) {
|
2013-11-22 18:26:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO(Travis Keep): This is a hack to get around CLDR bug 6818.
|
|
|
|
const char *localeId = ures_getLocaleByType(
|
|
|
|
topLevel.getAlias(), ULOC_ACTUAL_LOCALE, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Locale locale(localeId);
|
|
|
|
if (uprv_strcmp("en", locale.getLanguage()) == 0) {
|
|
|
|
unitName.toLower();
|
|
|
|
}
|
|
|
|
// end hack
|
|
|
|
ures_getByKeyWithFallback(
|
|
|
|
topLevel.getAlias(), "relative", topLevel.getAlias(), &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
initAbsoluteUnit(
|
2013-11-22 18:26:22 +00:00
|
|
|
topLevel.getAlias(),
|
|
|
|
unitName,
|
2014-02-10 19:13:50 +00:00
|
|
|
absoluteUnit,
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void readDaysOfWeek(
|
|
|
|
const UResourceBundle *resource,
|
|
|
|
const char *path,
|
|
|
|
UnicodeString *daysOfWeek,
|
|
|
|
UErrorCode &status) {
|
|
|
|
LocalUResourceBundlePointer topLevel(
|
|
|
|
ures_getByKeyWithFallback(resource, path, NULL, &status));
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int32_t size = ures_getSize(topLevel.getAlias());
|
|
|
|
if (size != 7) {
|
|
|
|
status = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int32_t i = 0; i < size; ++i) {
|
2014-01-11 00:30:39 +00:00
|
|
|
if (!getStringByIndex(topLevel.getAlias(), i, daysOfWeek[i], status)) {
|
2013-11-22 18:26:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addWeekDay(
|
|
|
|
const UResourceBundle *resource,
|
|
|
|
const char *path,
|
|
|
|
const UnicodeString *daysOfWeek,
|
|
|
|
UDateAbsoluteUnit absoluteUnit,
|
2014-02-10 19:13:50 +00:00
|
|
|
UnicodeString absoluteUnits[][UDAT_DIRECTION_COUNT],
|
2013-11-22 18:26:22 +00:00
|
|
|
UErrorCode &status) {
|
|
|
|
LocalUResourceBundlePointer topLevel(
|
|
|
|
ures_getByKeyWithFallback(resource, path, NULL, &status));
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
initAbsoluteUnit(
|
2013-11-22 18:26:22 +00:00
|
|
|
topLevel.getAlias(),
|
|
|
|
daysOfWeek[absoluteUnit - UDAT_ABSOLUTE_SUNDAY],
|
2014-02-10 19:13:50 +00:00
|
|
|
absoluteUnits[absoluteUnit],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
|
|
|
}
|
|
|
|
|
2014-05-22 22:46:53 +00:00
|
|
|
static void addTimeUnits(
|
2013-11-22 18:26:22 +00:00
|
|
|
const UResourceBundle *resource,
|
2014-05-22 22:46:53 +00:00
|
|
|
const char *path, const char *pathShort, const char *pathNarrow,
|
|
|
|
UDateRelativeUnit relativeUnit,
|
|
|
|
UDateAbsoluteUnit absoluteUnit,
|
2014-02-10 19:13:50 +00:00
|
|
|
RelativeDateTimeCacheData &cacheData,
|
2013-11-22 18:26:22 +00:00
|
|
|
UErrorCode &status) {
|
|
|
|
addTimeUnit(
|
2014-05-22 22:46:53 +00:00
|
|
|
resource,
|
|
|
|
path,
|
2014-06-17 16:34:46 +00:00
|
|
|
cacheData.relativeUnits[UDAT_STYLE_LONG][relativeUnit],
|
|
|
|
cacheData.absoluteUnits[UDAT_STYLE_LONG][absoluteUnit],
|
2014-05-22 22:46:53 +00:00
|
|
|
status);
|
|
|
|
addTimeUnit(
|
|
|
|
resource,
|
|
|
|
pathShort,
|
2014-06-17 16:34:46 +00:00
|
|
|
cacheData.relativeUnits[UDAT_STYLE_SHORT][relativeUnit],
|
|
|
|
cacheData.absoluteUnits[UDAT_STYLE_SHORT][absoluteUnit],
|
2014-05-22 22:46:53 +00:00
|
|
|
status);
|
|
|
|
addTimeUnit(
|
|
|
|
resource,
|
|
|
|
pathNarrow,
|
2014-06-17 16:34:46 +00:00
|
|
|
cacheData.relativeUnits[UDAT_STYLE_NARROW][relativeUnit],
|
|
|
|
cacheData.absoluteUnits[UDAT_STYLE_NARROW][absoluteUnit],
|
2014-05-22 22:46:53 +00:00
|
|
|
status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void initRelativeUnits(
|
|
|
|
const UResourceBundle *resource,
|
|
|
|
const char *path, const char *pathShort, const char *pathNarrow,
|
|
|
|
UDateRelativeUnit relativeUnit,
|
|
|
|
QuantityFormatter relativeUnits[][UDAT_RELATIVE_UNIT_COUNT][2],
|
|
|
|
UErrorCode &status) {
|
|
|
|
initRelativeUnit(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
2014-05-22 22:46:53 +00:00
|
|
|
path,
|
2014-06-17 16:34:46 +00:00
|
|
|
relativeUnits[UDAT_STYLE_LONG][relativeUnit],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
initRelativeUnit(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
2014-05-22 22:46:53 +00:00
|
|
|
pathShort,
|
2014-06-17 16:34:46 +00:00
|
|
|
relativeUnits[UDAT_STYLE_SHORT][relativeUnit],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
initRelativeUnit(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
2014-05-22 22:46:53 +00:00
|
|
|
pathNarrow,
|
2014-06-17 16:34:46 +00:00
|
|
|
relativeUnits[UDAT_STYLE_NARROW][relativeUnit],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void addWeekDays(
|
|
|
|
const UResourceBundle *resource,
|
|
|
|
const char *path, const char *pathShort, const char *pathNarrow,
|
|
|
|
const UnicodeString daysOfWeek[][7],
|
|
|
|
UDateAbsoluteUnit absoluteUnit,
|
|
|
|
UnicodeString absoluteUnits[][UDAT_ABSOLUTE_UNIT_COUNT][UDAT_DIRECTION_COUNT],
|
|
|
|
UErrorCode &status) {
|
|
|
|
addWeekDay(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
2014-05-22 22:46:53 +00:00
|
|
|
path,
|
2014-06-17 16:34:46 +00:00
|
|
|
daysOfWeek[UDAT_STYLE_LONG],
|
2014-05-22 22:46:53 +00:00
|
|
|
absoluteUnit,
|
2014-06-17 16:34:46 +00:00
|
|
|
absoluteUnits[UDAT_STYLE_LONG],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
addWeekDay(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
2014-05-22 22:46:53 +00:00
|
|
|
pathShort,
|
2014-06-17 16:34:46 +00:00
|
|
|
daysOfWeek[UDAT_STYLE_SHORT],
|
2014-05-22 22:46:53 +00:00
|
|
|
absoluteUnit,
|
2014-06-17 16:34:46 +00:00
|
|
|
absoluteUnits[UDAT_STYLE_SHORT],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
addWeekDay(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
2014-05-22 22:46:53 +00:00
|
|
|
pathNarrow,
|
2014-06-17 16:34:46 +00:00
|
|
|
daysOfWeek[UDAT_STYLE_NARROW],
|
2014-05-22 22:46:53 +00:00
|
|
|
absoluteUnit,
|
2014-06-17 16:34:46 +00:00
|
|
|
absoluteUnits[UDAT_STYLE_NARROW],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static UBool loadUnitData(
|
|
|
|
const UResourceBundle *resource,
|
|
|
|
RelativeDateTimeCacheData &cacheData,
|
|
|
|
UErrorCode &status) {
|
|
|
|
addTimeUnits(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
2014-05-22 22:46:53 +00:00
|
|
|
"fields/day", "fields/day-short", "fields/day-narrow",
|
|
|
|
UDAT_RELATIVE_DAYS,
|
|
|
|
UDAT_ABSOLUTE_DAY,
|
|
|
|
cacheData,
|
|
|
|
status);
|
|
|
|
addTimeUnits(
|
|
|
|
resource,
|
|
|
|
"fields/week", "fields/week-short", "fields/week-narrow",
|
|
|
|
UDAT_RELATIVE_WEEKS,
|
|
|
|
UDAT_ABSOLUTE_WEEK,
|
|
|
|
cacheData,
|
|
|
|
status);
|
|
|
|
addTimeUnits(
|
|
|
|
resource,
|
|
|
|
"fields/month", "fields/month-short", "fields/month-narrow",
|
|
|
|
UDAT_RELATIVE_MONTHS,
|
|
|
|
UDAT_ABSOLUTE_MONTH,
|
|
|
|
cacheData,
|
|
|
|
status);
|
|
|
|
addTimeUnits(
|
|
|
|
resource,
|
|
|
|
"fields/year", "fields/year-short", "fields/year-narrow",
|
|
|
|
UDAT_RELATIVE_YEARS,
|
|
|
|
UDAT_ABSOLUTE_YEAR,
|
|
|
|
cacheData,
|
|
|
|
status);
|
|
|
|
initRelativeUnits(
|
|
|
|
resource,
|
|
|
|
"fields/second", "fields/second-short", "fields/second-narrow",
|
|
|
|
UDAT_RELATIVE_SECONDS,
|
|
|
|
cacheData.relativeUnits,
|
|
|
|
status);
|
|
|
|
initRelativeUnits(
|
|
|
|
resource,
|
|
|
|
"fields/minute", "fields/minute-short", "fields/minute-narrow",
|
|
|
|
UDAT_RELATIVE_MINUTES,
|
|
|
|
cacheData.relativeUnits,
|
|
|
|
status);
|
|
|
|
initRelativeUnits(
|
|
|
|
resource,
|
|
|
|
"fields/hour", "fields/hour-short", "fields/hour-narrow",
|
|
|
|
UDAT_RELATIVE_HOURS,
|
|
|
|
cacheData.relativeUnits,
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
|
|
|
getStringWithFallback(
|
|
|
|
resource,
|
|
|
|
"fields/second/relative/0",
|
2014-06-17 16:34:46 +00:00
|
|
|
cacheData.absoluteUnits[UDAT_STYLE_LONG][UDAT_ABSOLUTE_NOW][UDAT_DIRECTION_PLAIN],
|
2014-05-22 22:46:53 +00:00
|
|
|
status);
|
|
|
|
getStringWithFallback(
|
|
|
|
resource,
|
|
|
|
"fields/second-short/relative/0",
|
2014-06-17 16:34:46 +00:00
|
|
|
cacheData.absoluteUnits[UDAT_STYLE_SHORT][UDAT_ABSOLUTE_NOW][UDAT_DIRECTION_PLAIN],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
getStringWithFallback(
|
|
|
|
resource,
|
|
|
|
"fields/second-narrow/relative/0",
|
2014-06-17 16:34:46 +00:00
|
|
|
cacheData.absoluteUnits[UDAT_STYLE_NARROW][UDAT_ABSOLUTE_NOW][UDAT_DIRECTION_PLAIN],
|
2014-05-22 22:46:53 +00:00
|
|
|
status);
|
2014-06-17 16:34:46 +00:00
|
|
|
UnicodeString daysOfWeek[UDAT_STYLE_COUNT][7];
|
2013-11-22 18:26:22 +00:00
|
|
|
readDaysOfWeek(
|
|
|
|
resource,
|
|
|
|
"calendar/gregorian/dayNames/stand-alone/wide",
|
2014-06-17 16:34:46 +00:00
|
|
|
daysOfWeek[UDAT_STYLE_LONG],
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
readDaysOfWeek(
|
|
|
|
resource,
|
|
|
|
"calendar/gregorian/dayNames/stand-alone/short",
|
2014-06-17 16:34:46 +00:00
|
|
|
daysOfWeek[UDAT_STYLE_SHORT],
|
2014-05-22 22:46:53 +00:00
|
|
|
status);
|
|
|
|
readDaysOfWeek(
|
|
|
|
resource,
|
|
|
|
"calendar/gregorian/dayNames/stand-alone/narrow",
|
2014-06-17 16:34:46 +00:00
|
|
|
daysOfWeek[UDAT_STYLE_NARROW],
|
2014-05-22 22:46:53 +00:00
|
|
|
status);
|
|
|
|
addWeekDays(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
|
|
|
"fields/mon/relative",
|
2014-05-22 22:46:53 +00:00
|
|
|
"fields/mon-short/relative",
|
|
|
|
"fields/mon-narrow/relative",
|
2013-11-22 18:26:22 +00:00
|
|
|
daysOfWeek,
|
|
|
|
UDAT_ABSOLUTE_MONDAY,
|
2014-02-10 19:13:50 +00:00
|
|
|
cacheData.absoluteUnits,
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
addWeekDays(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
|
|
|
"fields/tue/relative",
|
2014-05-22 22:46:53 +00:00
|
|
|
"fields/tue-short/relative",
|
|
|
|
"fields/tue-narrow/relative",
|
2013-11-22 18:26:22 +00:00
|
|
|
daysOfWeek,
|
|
|
|
UDAT_ABSOLUTE_TUESDAY,
|
2014-02-10 19:13:50 +00:00
|
|
|
cacheData.absoluteUnits,
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
addWeekDays(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
|
|
|
"fields/wed/relative",
|
2014-05-22 22:46:53 +00:00
|
|
|
"fields/wed-short/relative",
|
|
|
|
"fields/wed-narrow/relative",
|
2013-11-22 18:26:22 +00:00
|
|
|
daysOfWeek,
|
|
|
|
UDAT_ABSOLUTE_WEDNESDAY,
|
2014-02-10 19:13:50 +00:00
|
|
|
cacheData.absoluteUnits,
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
addWeekDays(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
|
|
|
"fields/thu/relative",
|
2014-05-22 22:46:53 +00:00
|
|
|
"fields/thu-short/relative",
|
|
|
|
"fields/thu-narrow/relative",
|
2013-11-22 18:26:22 +00:00
|
|
|
daysOfWeek,
|
|
|
|
UDAT_ABSOLUTE_THURSDAY,
|
2014-02-10 19:13:50 +00:00
|
|
|
cacheData.absoluteUnits,
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
addWeekDays(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
|
|
|
"fields/fri/relative",
|
2014-05-22 22:46:53 +00:00
|
|
|
"fields/fri-short/relative",
|
|
|
|
"fields/fri-narrow/relative",
|
2013-11-22 18:26:22 +00:00
|
|
|
daysOfWeek,
|
|
|
|
UDAT_ABSOLUTE_FRIDAY,
|
2014-02-10 19:13:50 +00:00
|
|
|
cacheData.absoluteUnits,
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
addWeekDays(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
|
|
|
"fields/sat/relative",
|
2014-05-22 22:46:53 +00:00
|
|
|
"fields/sat-short/relative",
|
|
|
|
"fields/sat-narrow/relative",
|
2013-11-22 18:26:22 +00:00
|
|
|
daysOfWeek,
|
|
|
|
UDAT_ABSOLUTE_SATURDAY,
|
2014-02-10 19:13:50 +00:00
|
|
|
cacheData.absoluteUnits,
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
addWeekDays(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
|
|
|
"fields/sun/relative",
|
2014-05-22 22:46:53 +00:00
|
|
|
"fields/sun-short/relative",
|
|
|
|
"fields/sun-narrow/relative",
|
2013-11-22 18:26:22 +00:00
|
|
|
daysOfWeek,
|
|
|
|
UDAT_ABSOLUTE_SUNDAY,
|
2014-02-10 19:13:50 +00:00
|
|
|
cacheData.absoluteUnits,
|
2013-11-22 18:26:22 +00:00
|
|
|
status);
|
2014-01-11 00:30:39 +00:00
|
|
|
return U_SUCCESS(status);
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 00:30:39 +00:00
|
|
|
static UBool getDateTimePattern(
|
2013-11-22 18:26:22 +00:00
|
|
|
const UResourceBundle *resource,
|
|
|
|
UnicodeString &result,
|
|
|
|
UErrorCode &status) {
|
|
|
|
UnicodeString defaultCalendarName;
|
2014-01-11 00:30:39 +00:00
|
|
|
if (!getStringWithFallback(
|
2013-11-22 18:26:22 +00:00
|
|
|
resource,
|
|
|
|
"calendar/default",
|
|
|
|
defaultCalendarName,
|
2014-01-11 00:30:39 +00:00
|
|
|
status)) {
|
|
|
|
return FALSE;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
2014-01-11 00:30:39 +00:00
|
|
|
CharString pathBuffer;
|
|
|
|
pathBuffer.append("calendar/", status)
|
|
|
|
.appendInvariantChars(defaultCalendarName, status)
|
|
|
|
.append("/DateTimePatterns", status);
|
2013-11-22 18:26:22 +00:00
|
|
|
LocalUResourceBundlePointer topLevel(
|
2014-01-11 00:30:39 +00:00
|
|
|
ures_getByKeyWithFallback(
|
|
|
|
resource, pathBuffer.data(), NULL, &status));
|
2013-11-22 18:26:22 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2014-01-11 00:30:39 +00:00
|
|
|
return FALSE;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
int32_t size = ures_getSize(topLevel.getAlias());
|
2014-01-11 00:30:39 +00:00
|
|
|
if (size <= 8) {
|
2013-11-22 18:26:22 +00:00
|
|
|
// Oops, size is to small to access the index that we want, fallback
|
|
|
|
// to a hard-coded value.
|
2013-12-03 23:27:49 +00:00
|
|
|
result = UNICODE_STRING_SIMPLE("{1} {0}");
|
2014-01-11 00:30:39 +00:00
|
|
|
return TRUE;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
2014-01-11 00:30:39 +00:00
|
|
|
return getStringByIndex(topLevel.getAlias(), 8, result, status);
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 21:46:02 +00:00
|
|
|
template<> U_I18N_API
|
|
|
|
const RelativeDateTimeCacheData *LocaleCacheKey<RelativeDateTimeCacheData>::createObject(const void * /*unused*/, UErrorCode &status) const {
|
|
|
|
const char *localeId = fLoc.getName();
|
2013-11-22 18:26:22 +00:00
|
|
|
LocalUResourceBundlePointer topLevel(ures_open(NULL, localeId, &status));
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
LocalPointer<RelativeDateTimeCacheData> result(
|
|
|
|
new RelativeDateTimeCacheData());
|
2014-03-11 17:40:23 +00:00
|
|
|
if (result.isNull()) {
|
2013-11-22 18:26:22 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
if (!loadUnitData(
|
2014-01-11 00:30:39 +00:00
|
|
|
topLevel.getAlias(),
|
2014-02-10 19:13:50 +00:00
|
|
|
*result,
|
2014-01-11 00:30:39 +00:00
|
|
|
status)) {
|
2013-11-22 18:26:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
UnicodeString dateTimePattern;
|
2014-01-11 00:30:39 +00:00
|
|
|
if (!getDateTimePattern(topLevel.getAlias(), dateTimePattern, status)) {
|
2013-11-22 18:26:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-10 19:13:50 +00:00
|
|
|
result->adoptCombinedDateAndTime(
|
2014-01-11 00:30:39 +00:00
|
|
|
new MessageFormat(dateTimePattern, localeId, status));
|
2013-11-22 18:26:22 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-08-20 21:46:02 +00:00
|
|
|
result->addRef();
|
2013-11-22 18:26:22 +00:00
|
|
|
return result.orphan();
|
|
|
|
}
|
|
|
|
|
2014-05-22 22:46:53 +00:00
|
|
|
RelativeDateTimeFormatter::RelativeDateTimeFormatter(UErrorCode& status) :
|
|
|
|
fCache(NULL),
|
|
|
|
fNumberFormat(NULL),
|
|
|
|
fPluralRules(NULL),
|
2014-06-11 18:31:47 +00:00
|
|
|
fStyle(UDAT_STYLE_LONG),
|
2014-05-22 22:46:53 +00:00
|
|
|
fContext(UDISPCTX_CAPITALIZATION_NONE),
|
|
|
|
fOptBreakIterator(NULL) {
|
|
|
|
init(NULL, NULL, status);
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 00:30:39 +00:00
|
|
|
RelativeDateTimeFormatter::RelativeDateTimeFormatter(
|
2014-05-22 22:46:53 +00:00
|
|
|
const Locale& locale, UErrorCode& status) :
|
|
|
|
fCache(NULL),
|
|
|
|
fNumberFormat(NULL),
|
|
|
|
fPluralRules(NULL),
|
2014-06-11 18:31:47 +00:00
|
|
|
fStyle(UDAT_STYLE_LONG),
|
2014-05-22 22:46:53 +00:00
|
|
|
fContext(UDISPCTX_CAPITALIZATION_NONE),
|
|
|
|
fOptBreakIterator(NULL),
|
|
|
|
fLocale(locale) {
|
|
|
|
init(NULL, NULL, status);
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 22:50:25 +00:00
|
|
|
RelativeDateTimeFormatter::RelativeDateTimeFormatter(
|
2014-05-22 22:46:53 +00:00
|
|
|
const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status) :
|
|
|
|
fCache(NULL),
|
|
|
|
fNumberFormat(NULL),
|
|
|
|
fPluralRules(NULL),
|
2014-06-11 18:31:47 +00:00
|
|
|
fStyle(UDAT_STYLE_LONG),
|
2014-05-22 22:46:53 +00:00
|
|
|
fContext(UDISPCTX_CAPITALIZATION_NONE),
|
|
|
|
fOptBreakIterator(NULL),
|
|
|
|
fLocale(locale) {
|
|
|
|
init(nfToAdopt, NULL, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
RelativeDateTimeFormatter::RelativeDateTimeFormatter(
|
|
|
|
const Locale& locale,
|
|
|
|
NumberFormat *nfToAdopt,
|
2014-06-11 18:31:47 +00:00
|
|
|
UDateRelativeDateTimeFormatterStyle styl,
|
2014-05-22 22:46:53 +00:00
|
|
|
UDisplayContext capitalizationContext,
|
|
|
|
UErrorCode& status) :
|
|
|
|
fCache(NULL),
|
|
|
|
fNumberFormat(NULL),
|
|
|
|
fPluralRules(NULL),
|
|
|
|
fStyle(styl),
|
|
|
|
fContext(capitalizationContext),
|
|
|
|
fOptBreakIterator(NULL),
|
|
|
|
fLocale(locale) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((capitalizationContext >> 8) != UDISPCTX_TYPE_CAPITALIZATION) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE) {
|
|
|
|
BreakIterator *bi = BreakIterator::createSentenceInstance(locale, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
init(nfToAdopt, bi, status);
|
|
|
|
} else {
|
|
|
|
init(nfToAdopt, NULL, status);
|
|
|
|
}
|
2013-12-04 22:50:25 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 00:30:39 +00:00
|
|
|
RelativeDateTimeFormatter::RelativeDateTimeFormatter(
|
2014-02-10 19:13:50 +00:00
|
|
|
const RelativeDateTimeFormatter& other)
|
2014-06-12 20:14:04 +00:00
|
|
|
: UObject(other),
|
|
|
|
fCache(other.fCache),
|
2014-05-22 22:46:53 +00:00
|
|
|
fNumberFormat(other.fNumberFormat),
|
|
|
|
fPluralRules(other.fPluralRules),
|
|
|
|
fStyle(other.fStyle),
|
|
|
|
fContext(other.fContext),
|
|
|
|
fOptBreakIterator(other.fOptBreakIterator),
|
|
|
|
fLocale(other.fLocale) {
|
|
|
|
fCache->addRef();
|
|
|
|
fNumberFormat->addRef();
|
|
|
|
fPluralRules->addRef();
|
|
|
|
if (fOptBreakIterator != NULL) {
|
|
|
|
fOptBreakIterator->addRef();
|
|
|
|
}
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 00:30:39 +00:00
|
|
|
RelativeDateTimeFormatter& RelativeDateTimeFormatter::operator=(
|
|
|
|
const RelativeDateTimeFormatter& other) {
|
2013-11-22 18:26:22 +00:00
|
|
|
if (this != &other) {
|
2014-05-22 22:46:53 +00:00
|
|
|
SharedObject::copyPtr(other.fCache, fCache);
|
|
|
|
SharedObject::copyPtr(other.fNumberFormat, fNumberFormat);
|
|
|
|
SharedObject::copyPtr(other.fPluralRules, fPluralRules);
|
|
|
|
SharedObject::copyPtr(other.fOptBreakIterator, fOptBreakIterator);
|
|
|
|
fStyle = other.fStyle;
|
|
|
|
fContext = other.fContext;
|
|
|
|
fLocale = other.fLocale;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
RelativeDateTimeFormatter::~RelativeDateTimeFormatter() {
|
2014-05-22 22:46:53 +00:00
|
|
|
if (fCache != NULL) {
|
|
|
|
fCache->removeRef();
|
2014-02-10 19:13:50 +00:00
|
|
|
}
|
2014-05-22 22:46:53 +00:00
|
|
|
if (fNumberFormat != NULL) {
|
|
|
|
fNumberFormat->removeRef();
|
2014-02-10 19:13:50 +00:00
|
|
|
}
|
2014-05-22 22:46:53 +00:00
|
|
|
if (fPluralRules != NULL) {
|
|
|
|
fPluralRules->removeRef();
|
|
|
|
}
|
|
|
|
if (fOptBreakIterator != NULL) {
|
|
|
|
fOptBreakIterator->removeRef();
|
2014-01-22 21:20:24 +00:00
|
|
|
}
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 00:30:39 +00:00
|
|
|
const NumberFormat& RelativeDateTimeFormatter::getNumberFormat() const {
|
2014-05-22 22:46:53 +00:00
|
|
|
return **fNumberFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
UDisplayContext RelativeDateTimeFormatter::getCapitalizationContext() const {
|
|
|
|
return fContext;
|
|
|
|
}
|
|
|
|
|
2014-06-11 18:31:47 +00:00
|
|
|
UDateRelativeDateTimeFormatterStyle RelativeDateTimeFormatter::getFormatStyle() const {
|
2014-05-22 22:46:53 +00:00
|
|
|
return fStyle;
|
2014-01-11 00:30:39 +00:00
|
|
|
}
|
2013-11-22 18:26:22 +00:00
|
|
|
|
|
|
|
UnicodeString& RelativeDateTimeFormatter::format(
|
|
|
|
double quantity, UDateDirection direction, UDateRelativeUnit unit,
|
|
|
|
UnicodeString& appendTo, UErrorCode& status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
if (direction != UDAT_DIRECTION_LAST && direction != UDAT_DIRECTION_NEXT) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
int32_t bFuture = direction == UDAT_DIRECTION_NEXT ? 1 : 0;
|
2014-02-04 00:29:17 +00:00
|
|
|
FieldPosition pos(FieldPosition::DONT_CARE);
|
2014-05-22 22:46:53 +00:00
|
|
|
if (fOptBreakIterator == NULL) {
|
2014-06-17 16:34:46 +00:00
|
|
|
return fCache->relativeUnits[fStyle][unit][bFuture].format(
|
2014-01-29 05:08:55 +00:00
|
|
|
quantity,
|
2014-05-22 22:46:53 +00:00
|
|
|
**fNumberFormat,
|
|
|
|
**fPluralRules,
|
2014-01-29 05:08:55 +00:00
|
|
|
appendTo,
|
2014-02-04 00:29:17 +00:00
|
|
|
pos,
|
2014-01-29 05:08:55 +00:00
|
|
|
status);
|
2014-05-22 22:46:53 +00:00
|
|
|
}
|
|
|
|
UnicodeString result;
|
2014-06-17 16:34:46 +00:00
|
|
|
fCache->relativeUnits[fStyle][unit][bFuture].format(
|
2014-05-22 22:46:53 +00:00
|
|
|
quantity,
|
|
|
|
**fNumberFormat,
|
|
|
|
**fPluralRules,
|
|
|
|
result,
|
|
|
|
pos,
|
|
|
|
status);
|
|
|
|
adjustForContext(result);
|
|
|
|
return appendTo.append(result);
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& RelativeDateTimeFormatter::format(
|
|
|
|
UDateDirection direction, UDateAbsoluteUnit unit,
|
|
|
|
UnicodeString& appendTo, UErrorCode& status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return appendTo;
|
|
|
|
}
|
|
|
|
if (unit == UDAT_ABSOLUTE_NOW && direction != UDAT_DIRECTION_PLAIN) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return appendTo;
|
|
|
|
}
|
2014-05-22 22:46:53 +00:00
|
|
|
if (fOptBreakIterator == NULL) {
|
2014-06-17 16:34:46 +00:00
|
|
|
return appendTo.append(fCache->absoluteUnits[fStyle][unit][direction]);
|
2014-05-22 22:46:53 +00:00
|
|
|
}
|
2014-06-17 16:34:46 +00:00
|
|
|
UnicodeString result(fCache->absoluteUnits[fStyle][unit][direction]);
|
2014-05-22 22:46:53 +00:00
|
|
|
adjustForContext(result);
|
|
|
|
return appendTo.append(result);
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& RelativeDateTimeFormatter::combineDateAndTime(
|
|
|
|
const UnicodeString& relativeDateString, const UnicodeString& timeString,
|
|
|
|
UnicodeString& appendTo, UErrorCode& status) const {
|
2014-01-11 00:30:39 +00:00
|
|
|
Formattable args[2] = {timeString, relativeDateString};
|
2013-11-22 18:26:22 +00:00
|
|
|
FieldPosition fpos(0);
|
2014-05-22 22:46:53 +00:00
|
|
|
return fCache->getCombinedDateAndTime()->format(
|
2014-02-10 19:13:50 +00:00
|
|
|
args, 2, appendTo, fpos, status);
|
|
|
|
}
|
|
|
|
|
2014-05-22 22:46:53 +00:00
|
|
|
void RelativeDateTimeFormatter::adjustForContext(UnicodeString &str) const {
|
|
|
|
if (fOptBreakIterator == NULL
|
|
|
|
|| str.length() == 0 || !u_islower(str.char32At(0))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must guarantee that one thread at a time accesses the shared break
|
|
|
|
// iterator.
|
|
|
|
Mutex lock(&gBrkIterMutex);
|
|
|
|
str.toTitle(
|
|
|
|
fOptBreakIterator->get(),
|
|
|
|
fLocale,
|
|
|
|
U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT);
|
|
|
|
}
|
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
void RelativeDateTimeFormatter::init(
|
2014-05-22 22:46:53 +00:00
|
|
|
NumberFormat *nfToAdopt,
|
|
|
|
BreakIterator *biToAdopt,
|
|
|
|
UErrorCode &status) {
|
2014-02-10 22:52:01 +00:00
|
|
|
LocalPointer<NumberFormat> nf(nfToAdopt);
|
2014-05-22 22:46:53 +00:00
|
|
|
LocalPointer<BreakIterator> bi(biToAdopt);
|
2014-08-20 21:46:02 +00:00
|
|
|
UnifiedCache::getByLocale(fLocale, fCache, status);
|
|
|
|
if (U_FAILURE(status)) {
|
2014-02-10 19:13:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-05-22 22:46:53 +00:00
|
|
|
const SharedPluralRules *pr = PluralRules::createSharedInstance(
|
|
|
|
fLocale, UPLURAL_TYPE_CARDINAL, status);
|
2014-02-10 19:13:50 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 22:46:53 +00:00
|
|
|
SharedObject::copyPtr(pr, fPluralRules);
|
|
|
|
pr->removeRef();
|
2014-03-11 17:40:23 +00:00
|
|
|
if (nf.isNull()) {
|
2014-05-22 22:46:53 +00:00
|
|
|
const SharedNumberFormat *shared = NumberFormat::createSharedInstance(
|
|
|
|
fLocale, UNUM_DECIMAL, status);
|
2014-02-10 19:13:50 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 22:46:53 +00:00
|
|
|
SharedObject::copyPtr(shared, fNumberFormat);
|
|
|
|
shared->removeRef();
|
2014-02-10 19:13:50 +00:00
|
|
|
} else {
|
2014-02-10 22:52:01 +00:00
|
|
|
SharedNumberFormat *shared = new SharedNumberFormat(nf.getAlias());
|
2014-02-10 19:13:50 +00:00
|
|
|
if (shared == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 22:52:01 +00:00
|
|
|
nf.orphan();
|
2014-05-22 22:46:53 +00:00
|
|
|
SharedObject::copyPtr(shared, fNumberFormat);
|
|
|
|
}
|
|
|
|
if (bi.isNull()) {
|
|
|
|
SharedObject::clearPtr(fOptBreakIterator);
|
|
|
|
} else {
|
|
|
|
SharedBreakIterator *shared = new SharedBreakIterator(bi.getAlias());
|
|
|
|
if (shared == NULL) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bi.orphan();
|
|
|
|
SharedObject::copyPtr(shared, fOptBreakIterator);
|
2014-02-10 19:13:50 +00:00
|
|
|
}
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 19:13:50 +00:00
|
|
|
|
2013-11-22 18:26:22 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2013-12-03 04:48:00 +00:00
|
|
|
#endif /* !UCONFIG_NO_FORMATTING */
|
|
|
|
|