1 /*******************************************************************************
2 * Copyright (C) 2008-2016, International Business Machines Corporation and
3 * others. All Rights Reserved.
4 *******************************************************************************
8 *******************************************************************************
11 #include "utypeinfo.h" // for 'typeid' to work
13 #include "unicode/dtitvfmt.h"
15 #if !UCONFIG_NO_FORMATTING
17 //TODO: put in compilation
18 //#define DTITVFMT_DEBUG 1
20 #include "unicode/calendar.h"
21 #include "unicode/dtptngen.h"
22 #include "unicode/dtitvinf.h"
23 #include "unicode/udateintervalformat.h"
24 #include "unicode/simpleformatter.h"
27 #include "dtitv_impl.h"
40 #define PRINTMESG(msg) { std::cout << "(" << __FILE__ << ":" << __LINE__ << ") " << msg << "\n"; }
44 static const UChar gDateFormatSkeleton
[][11] = {
46 {LOW_Y
, CAP_M
, CAP_M
, CAP_M
, CAP_M
, CAP_E
, CAP_E
, CAP_E
, CAP_E
, LOW_D
, 0},
48 {LOW_Y
, CAP_M
, CAP_M
, CAP_M
, CAP_M
, LOW_D
, 0},
50 {LOW_Y
, CAP_M
, CAP_M
, CAP_M
, LOW_D
, 0},
52 {LOW_Y
, CAP_M
, LOW_D
, 0} };
55 static const char gDateTimePatternsTag
[]="DateTimePatterns";
59 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
};
62 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
};
65 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateIntervalFormat
)
67 // Mutex, protects access to fDateFormat, fFromCalendar and fToCalendar.
68 // Needed because these data members are modified by const methods of DateIntervalFormat.
70 static UMutex gFormatterMutex
= U_MUTEX_INITIALIZER
;
72 DateIntervalFormat
* U_EXPORT2
73 DateIntervalFormat::createInstance(const UnicodeString
& skeleton
,
75 return createInstance(skeleton
, Locale::getDefault(), status
);
79 DateIntervalFormat
* U_EXPORT2
80 DateIntervalFormat::createInstance(const UnicodeString
& skeleton
,
87 skeleton
.extract(0, skeleton
.length(), result
, "UTF-8");
89 ((SimpleDateFormat
*)dtfmt
)->toPattern(pat
);
90 pat
.extract(0, pat
.length(), result_1
, "UTF-8");
91 sprintf(mesg
, "skeleton: %s; pattern: %s\n", result
, result_1
);
95 DateIntervalInfo
* dtitvinf
= new DateIntervalInfo(locale
, status
);
96 return create(locale
, dtitvinf
, &skeleton
, status
);
101 DateIntervalFormat
* U_EXPORT2
102 DateIntervalFormat::createInstance(const UnicodeString
& skeleton
,
103 const DateIntervalInfo
& dtitvinf
,
104 UErrorCode
& status
) {
105 return createInstance(skeleton
, Locale::getDefault(), dtitvinf
, status
);
109 DateIntervalFormat
* U_EXPORT2
110 DateIntervalFormat::createInstance(const UnicodeString
& skeleton
,
111 const Locale
& locale
,
112 const DateIntervalInfo
& dtitvinf
,
113 UErrorCode
& status
) {
114 DateIntervalInfo
* ptn
= dtitvinf
.clone();
115 return create(locale
, ptn
, &skeleton
, status
);
119 DateIntervalFormat::DateIntervalFormat()
124 fLocale(Locale::getRoot()),
127 fDateTimeFormat(NULL
),
128 fMinimizeType(UDTITVFMT_MINIMIZE_NONE
)
132 DateIntervalFormat::DateIntervalFormat(const DateIntervalFormat
& itvfmt
)
138 fLocale(itvfmt
.fLocale
),
141 fDateTimeFormat(NULL
),
142 fMinimizeType(UDTITVFMT_MINIMIZE_NONE
) {
148 DateIntervalFormat::operator=(const DateIntervalFormat
& itvfmt
) {
149 if ( this != &itvfmt
) {
152 delete fFromCalendar
;
156 delete fDateTimeFormat
;
158 Mutex
lock(&gFormatterMutex
);
159 if ( itvfmt
.fDateFormat
) {
160 fDateFormat
= (SimpleDateFormat
*)itvfmt
.fDateFormat
->clone();
164 if ( itvfmt
.fFromCalendar
) {
165 fFromCalendar
= itvfmt
.fFromCalendar
->clone();
167 fFromCalendar
= NULL
;
169 if ( itvfmt
.fToCalendar
) {
170 fToCalendar
= itvfmt
.fToCalendar
->clone();
175 if ( itvfmt
.fInfo
) {
176 fInfo
= itvfmt
.fInfo
->clone();
180 fSkeleton
= itvfmt
.fSkeleton
;
182 for ( i
= 0; i
< DateIntervalInfo::kIPI_MAX_INDEX
; ++i
) {
183 fIntervalPatterns
[i
] = itvfmt
.fIntervalPatterns
[i
];
185 fLocale
= itvfmt
.fLocale
;
186 fDatePattern
= (itvfmt
.fDatePattern
)? (UnicodeString
*)itvfmt
.fDatePattern
->clone(): NULL
;
187 fTimePattern
= (itvfmt
.fTimePattern
)? (UnicodeString
*)itvfmt
.fTimePattern
->clone(): NULL
;
188 fDateTimeFormat
= (itvfmt
.fDateTimeFormat
)? (UnicodeString
*)itvfmt
.fDateTimeFormat
->clone(): NULL
;
194 DateIntervalFormat::~DateIntervalFormat() {
197 delete fFromCalendar
;
201 delete fDateTimeFormat
;
206 DateIntervalFormat::clone(void) const {
207 return new DateIntervalFormat(*this);
212 DateIntervalFormat::operator==(const Format
& other
) const {
213 if (typeid(*this) != typeid(other
)) {return FALSE
;}
214 const DateIntervalFormat
* fmt
= (DateIntervalFormat
*)&other
;
215 if (this == fmt
) {return TRUE
;}
216 if (!Format::operator==(other
)) {return FALSE
;}
217 if ((fInfo
!= fmt
->fInfo
) && (fInfo
== NULL
|| fmt
->fInfo
== NULL
)) {return FALSE
;}
218 if (fInfo
&& fmt
->fInfo
&& (*fInfo
!= *fmt
->fInfo
)) {return FALSE
;}
220 Mutex
lock(&gFormatterMutex
);
221 if (fDateFormat
!= fmt
->fDateFormat
&& (fDateFormat
== NULL
|| fmt
->fDateFormat
== NULL
)) {return FALSE
;}
222 if (fDateFormat
&& fmt
->fDateFormat
&& (*fDateFormat
!= *fmt
->fDateFormat
)) {return FALSE
;}
224 // TODO: should operator == ignore the From and ToCalendar? They hold transient values during
225 // formatting of a DateInterval.
226 if (fFromCalendar
!= fmt
->fFromCalendar
&& (fFromCalendar
== NULL
|| fmt
->fFromCalendar
== NULL
)) {return FALSE
;}
227 if (fFromCalendar
&& fmt
->fFromCalendar
&& !fFromCalendar
->isEquivalentTo(*fmt
->fFromCalendar
)) {return FALSE
;}
229 if (fToCalendar
!= fmt
->fToCalendar
&& (fToCalendar
== NULL
|| fmt
->fToCalendar
== NULL
)) {return FALSE
;}
230 if (fToCalendar
&& fmt
->fToCalendar
&& !fToCalendar
->isEquivalentTo(*fmt
->fToCalendar
)) {return FALSE
;}
232 if (fSkeleton
!= fmt
->fSkeleton
) {return FALSE
;}
233 if (fDatePattern
!= fmt
->fDatePattern
&& (fDatePattern
== NULL
|| fmt
->fDatePattern
== NULL
)) {return FALSE
;}
234 if (fDatePattern
&& fmt
->fDatePattern
&& (*fDatePattern
!= *fmt
->fDatePattern
)) {return FALSE
;}
235 if (fTimePattern
!= fmt
->fTimePattern
&& (fTimePattern
== NULL
|| fmt
->fTimePattern
== NULL
)) {return FALSE
;}
236 if (fTimePattern
&& fmt
->fTimePattern
&& (*fTimePattern
!= *fmt
->fTimePattern
)) {return FALSE
;}
237 if (fDateTimeFormat
!= fmt
->fDateTimeFormat
&& (fDateTimeFormat
== NULL
|| fmt
->fDateTimeFormat
== NULL
)) {return FALSE
;}
238 if (fDateTimeFormat
&& fmt
->fDateTimeFormat
&& (*fDateTimeFormat
!= *fmt
->fDateTimeFormat
)) {return FALSE
;}
239 if (fLocale
!= fmt
->fLocale
) {return FALSE
;}
241 for (int32_t i
= 0; i
< DateIntervalInfo::kIPI_MAX_INDEX
; ++i
) {
242 if (fIntervalPatterns
[i
].firstPart
!= fmt
->fIntervalPatterns
[i
].firstPart
) {return FALSE
;}
243 if (fIntervalPatterns
[i
].secondPart
!= fmt
->fIntervalPatterns
[i
].secondPart
) {return FALSE
;}
244 if (fIntervalPatterns
[i
].laterDateFirst
!= fmt
->fIntervalPatterns
[i
].laterDateFirst
) {return FALSE
;}
251 DateIntervalFormat::format(const Formattable
& obj
,
252 UnicodeString
& appendTo
,
253 FieldPosition
& fieldPosition
,
254 UErrorCode
& status
) const {
255 if ( U_FAILURE(status
) ) {
259 if ( obj
.getType() == Formattable::kObject
) {
260 const UObject
* formatObj
= obj
.getObject();
261 const DateInterval
* interval
= dynamic_cast<const DateInterval
*>(formatObj
);
262 if (interval
!= NULL
) {
263 return format(interval
, appendTo
, fieldPosition
, status
);
266 status
= U_ILLEGAL_ARGUMENT_ERROR
;
272 DateIntervalFormat::format(const DateInterval
* dtInterval
,
273 UnicodeString
& appendTo
,
274 FieldPosition
& fieldPosition
,
275 UErrorCode
& status
) const {
276 if ( U_FAILURE(status
) ) {
279 if (fFromCalendar
== NULL
|| fToCalendar
== NULL
|| fDateFormat
== NULL
|| fInfo
== NULL
) {
280 status
= U_INVALID_STATE_ERROR
;
284 Mutex
lock(&gFormatterMutex
);
285 fFromCalendar
->setTime(dtInterval
->getFromDate(), status
);
286 fToCalendar
->setTime(dtInterval
->getToDate(), status
);
287 return formatImpl(*fFromCalendar
, *fToCalendar
, appendTo
,fieldPosition
, status
);
292 DateIntervalFormat::format(Calendar
& fromCalendar
,
293 Calendar
& toCalendar
,
294 UnicodeString
& appendTo
,
296 UErrorCode
& status
) const {
297 Mutex
lock(&gFormatterMutex
);
298 return formatImpl(fromCalendar
, toCalendar
, appendTo
, pos
, status
);
303 DateIntervalFormat::formatImpl(Calendar
& fromCalendar
,
304 Calendar
& toCalendar
,
305 UnicodeString
& appendTo
,
307 UErrorCode
& status
) const {
308 if ( U_FAILURE(status
) ) {
312 // not support different calendar types and time zones
313 //if ( fromCalendar.getType() != toCalendar.getType() ) {
314 if ( !fromCalendar
.isEquivalentTo(toCalendar
) ) {
315 status
= U_ILLEGAL_ARGUMENT_ERROR
;
319 // First, find the largest different calendar field.
320 UCalendarDateFields field
= UCAL_FIELD_COUNT
;
321 UChar patternDay
= 0x0064; // d
322 UChar patternYear
= 0x0079; // y
324 if ( fromCalendar
.get(UCAL_ERA
,status
) != toCalendar
.get(UCAL_ERA
,status
)) {
326 } else if ( fromCalendar
.get(UCAL_YEAR
, status
) !=
327 toCalendar
.get(UCAL_YEAR
, status
) ) {
329 if (fMinimizeType
== UDTITVFMT_MINIMIZE_ADJACENT_MONTHS
&& fSkeleton
.indexOf(patternDay
) >= 0 && fSkeleton
.indexOf(patternYear
) < 0) {
330 UDate fromDate
= fromCalendar
.getTime(status
);
331 UDate toDate
= toCalendar
.getTime(status
);
332 int32_t fromDay
= fromCalendar
.get(UCAL_DATE
, status
);
333 int32_t toDay
= toCalendar
.get(UCAL_DATE
, status
);
334 fromCalendar
.add(UCAL_MONTH
, 1, status
);
335 if ( fromDate
< toDate
&& fromCalendar
.getTime(status
) > toDate
&& fromDay
> toDay
) {
338 fromCalendar
.setTime(fromDate
, status
);
340 } else if ( fromCalendar
.get(UCAL_MONTH
, status
) !=
341 toCalendar
.get(UCAL_MONTH
, status
) ) {
343 if (fMinimizeType
== UDTITVFMT_MINIMIZE_ADJACENT_MONTHS
&& fSkeleton
.indexOf(patternDay
) >= 0) {
344 UDate fromDate
= fromCalendar
.getTime(status
);
345 UDate toDate
= toCalendar
.getTime(status
);
346 int32_t fromDay
= fromCalendar
.get(UCAL_DATE
, status
);
347 int32_t toDay
= toCalendar
.get(UCAL_DATE
, status
);
348 fromCalendar
.add(UCAL_MONTH
, 1, status
);
349 if ( fromDate
< toDate
&& fromCalendar
.getTime(status
) > toDate
&& fromDay
> toDay
) {
352 fromCalendar
.setTime(fromDate
, status
);
354 } else if ( fromCalendar
.get(UCAL_DATE
, status
) !=
355 toCalendar
.get(UCAL_DATE
, status
) ) {
357 if (fMinimizeType
== UDTITVFMT_MINIMIZE_ADJACENT_DAYS
&&
358 // check normalized skeleton for 'H', 'h', 'j'
359 (fSkeleton
.indexOf(0x0048) >= 0 || fSkeleton
.indexOf(0x0068) >= 0 || fSkeleton
.indexOf(0x006A) >= 0)) {
360 UDate fromDate
= fromCalendar
.getTime(status
);
361 UDate toDate
= toCalendar
.getTime(status
);
362 int32_t fromHour
= fromCalendar
.get(UCAL_HOUR
, status
);
363 int32_t toHour
= toCalendar
.get(UCAL_HOUR
, status
);
364 fromCalendar
.add(UCAL_HOUR_OF_DAY
, 12, status
);
365 if ( fromDate
< toDate
&& fromCalendar
.getTime(status
) > toDate
&& fromHour
> toHour
) {
368 fromCalendar
.setTime(fromDate
, status
);
370 } else if ( fromCalendar
.get(UCAL_AM_PM
, status
) !=
371 toCalendar
.get(UCAL_AM_PM
, status
) ) {
373 } else if ( fromCalendar
.get(UCAL_HOUR
, status
) !=
374 toCalendar
.get(UCAL_HOUR
, status
) ) {
376 } else if ( fromCalendar
.get(UCAL_MINUTE
, status
) !=
377 toCalendar
.get(UCAL_MINUTE
, status
) ) {
379 } else if ( fromCalendar
.get(UCAL_SECOND
, status
) !=
380 toCalendar
.get(UCAL_SECOND
, status
) ) {
384 if ( U_FAILURE(status
) ) {
387 if ( field
== UCAL_FIELD_COUNT
) {
388 /* ignore the millisecond etc. small fields' difference.
389 * use single date when all the above are the same.
391 return fDateFormat
->format(fromCalendar
, appendTo
, pos
);
393 UBool fromToOnSameDay
= (field
==UCAL_AM_PM
|| field
==UCAL_HOUR
|| field
==UCAL_MINUTE
|| field
==UCAL_SECOND
);
395 // following call should not set wrong status,
396 // all the pass-in fields are valid till here
397 int32_t itvPtnIndex
= DateIntervalInfo::calendarFieldToIntervalIndex(field
,
399 const PatternInfo
& intervalPattern
= fIntervalPatterns
[itvPtnIndex
];
401 if ( intervalPattern
.firstPart
.isEmpty() &&
402 intervalPattern
.secondPart
.isEmpty() ) {
403 if ( fDateFormat
->isFieldUnitIgnored(field
) ) {
404 /* the largest different calendar field is small than
405 * the smallest calendar field in pattern,
406 * return single date format.
408 return fDateFormat
->format(fromCalendar
, appendTo
, pos
);
410 return fallbackFormat(fromCalendar
, toCalendar
, fromToOnSameDay
, appendTo
, pos
, status
);
412 // If the first part in interval pattern is empty,
413 // the 2nd part of it saves the full-pattern used in fall-back.
414 // For a 'real' interval pattern, the first part will never be empty.
415 if ( intervalPattern
.firstPart
.isEmpty() ) {
417 UnicodeString originalPattern
;
418 fDateFormat
->toPattern(originalPattern
);
419 fDateFormat
->applyPattern(intervalPattern
.secondPart
);
420 appendTo
= fallbackFormat(fromCalendar
, toCalendar
, fromToOnSameDay
, appendTo
, pos
, status
);
421 fDateFormat
->applyPattern(originalPattern
);
426 if ( intervalPattern
.laterDateFirst
) {
427 firstCal
= &toCalendar
;
428 secondCal
= &fromCalendar
;
430 firstCal
= &fromCalendar
;
431 secondCal
= &toCalendar
;
433 // break the interval pattern into 2 parts,
434 // first part should not be empty,
435 UnicodeString originalPattern
;
436 fDateFormat
->toPattern(originalPattern
);
437 fDateFormat
->applyPattern(intervalPattern
.firstPart
);
438 fDateFormat
->format(*firstCal
, appendTo
, pos
);
439 if ( !intervalPattern
.secondPart
.isEmpty() ) {
440 fDateFormat
->applyPattern(intervalPattern
.secondPart
);
441 FieldPosition otherPos
;
442 otherPos
.setField(pos
.getField());
443 fDateFormat
->format(*secondCal
, appendTo
, otherPos
);
444 if (pos
.getEndIndex() == 0 && otherPos
.getEndIndex() > 0) {
448 fDateFormat
->applyPattern(originalPattern
);
455 DateIntervalFormat::parseObject(const UnicodeString
& /* source */,
456 Formattable
& /* result */,
457 ParsePosition
& /* parse_pos */) const {
458 // parseObject(const UnicodeString&, Formattable&, UErrorCode&) const
459 // will set status as U_INVALID_FORMAT_ERROR if
460 // parse_pos is still 0
466 const DateIntervalInfo
*
467 DateIntervalFormat::getDateIntervalInfo() const {
473 DateIntervalFormat::setDateIntervalInfo(const DateIntervalInfo
& newItvPattern
,
474 UErrorCode
& status
) {
476 fInfo
= new DateIntervalInfo(newItvPattern
);
478 // Delete patterns that get reset by initializePattern
483 delete fDateTimeFormat
;
484 fDateTimeFormat
= NULL
;
487 initializePattern(status
);
494 DateIntervalFormat::getDateFormat() const {
500 DateIntervalFormat::adoptTimeZone(TimeZone
* zone
)
502 if (fDateFormat
!= NULL
) {
503 fDateFormat
->adoptTimeZone(zone
);
505 // The fDateFormat has the master calendar for the DateIntervalFormat and has
506 // ownership of any adopted TimeZone; fFromCalendar and fToCalendar are internal
507 // work clones of that calendar (and should not also be given ownership of the
508 // adopted TimeZone).
510 fFromCalendar
->setTimeZone(*zone
);
513 fToCalendar
->setTimeZone(*zone
);
518 DateIntervalFormat::setTimeZone(const TimeZone
& zone
)
520 if (fDateFormat
!= NULL
) {
521 fDateFormat
->setTimeZone(zone
);
523 // The fDateFormat has the master calendar for the DateIntervalFormat;
524 // fFromCalendar and fToCalendar are internal work clones of that calendar.
526 fFromCalendar
->setTimeZone(zone
);
529 fToCalendar
->setTimeZone(zone
);
534 DateIntervalFormat::getTimeZone() const
536 if (fDateFormat
!= NULL
) {
537 Mutex
lock(&gFormatterMutex
);
538 return fDateFormat
->getTimeZone();
540 // If fDateFormat is NULL (unexpected), create default timezone.
541 return *(TimeZone::createDefault());
545 DateIntervalFormat::setAttribute(UDateIntervalFormatAttribute attr
,
546 UDateIntervalFormatAttributeValue value
,
549 if ( U_FAILURE(status
) ) {
552 if (attr
== UDTITVFMT_MINIMIZE_TYPE
) {
553 fMinimizeType
= value
;
555 status
= U_ILLEGAL_ARGUMENT_ERROR
;
559 DateIntervalFormat::DateIntervalFormat(const Locale
& locale
,
560 DateIntervalInfo
* dtItvInfo
,
561 const UnicodeString
* skeleton
,
570 fDateTimeFormat(NULL
),
571 fMinimizeType(UDTITVFMT_MINIMIZE_NONE
)
573 LocalPointer
<DateIntervalInfo
> info(dtItvInfo
, status
);
574 LocalPointer
<SimpleDateFormat
> dtfmt(static_cast<SimpleDateFormat
*>(
575 DateFormat::createInstanceForSkeleton(*skeleton
, locale
, status
)), status
);
576 if (U_FAILURE(status
)) {
581 fSkeleton
= *skeleton
;
583 fInfo
= info
.orphan();
584 fDateFormat
= dtfmt
.orphan();
585 if ( fDateFormat
->getCalendar() ) {
586 fFromCalendar
= fDateFormat
->getCalendar()->clone();
587 fToCalendar
= fDateFormat
->getCalendar()->clone();
589 initializePattern(status
);
592 DateIntervalFormat
* U_EXPORT2
593 DateIntervalFormat::create(const Locale
& locale
,
594 DateIntervalInfo
* dtitvinf
,
595 const UnicodeString
* skeleton
,
596 UErrorCode
& status
) {
597 DateIntervalFormat
* f
= new DateIntervalFormat(locale
, dtitvinf
,
600 status
= U_MEMORY_ALLOCATION_ERROR
;
602 } else if ( U_FAILURE(status
) ) {
603 // safe to delete f, although nothing acutally is saved
613 * Initialize interval patterns locale to this formatter
615 * This code is a bit complicated since
616 * 1. the interval patterns saved in resource bundle files are interval
617 * patterns based on date or time only.
618 * It does not have interval patterns based on both date and time.
619 * Interval patterns on both date and time are algorithm generated.
621 * For example, it has interval patterns on skeleton "dMy" and "hm",
622 * but it does not have interval patterns on skeleton "dMyhm".
624 * The rule to genearte interval patterns for both date and time skeleton are
625 * 1) when the year, month, or day differs, concatenate the two original
626 * expressions with a separator between,
627 * For example, interval pattern from "Jan 10, 2007 10:10 am"
628 * to "Jan 11, 2007 10:10am" is
629 * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
631 * 2) otherwise, present the date followed by the range expression
633 * For example, interval pattern from "Jan 10, 2007 10:10 am"
634 * to "Jan 10, 2007 11:10am" is
635 * "Jan 10, 2007 10:10 am - 11:10am"
637 * 2. even a pattern does not request a certion calendar field,
638 * the interval pattern needs to include such field if such fields are
639 * different between 2 dates.
640 * For example, a pattern/skeleton is "hm", but the interval pattern
641 * includes year, month, and date when year, month, and date differs.
643 * @param status output param set to success/failure code on exit
647 DateIntervalFormat::initializePattern(UErrorCode
& status
) {
648 if ( U_FAILURE(status
) ) {
651 const Locale
& locale
= fDateFormat
->getSmpFmtLocale();
652 if ( fSkeleton
.isEmpty() ) {
653 UnicodeString fullPattern
;
654 fDateFormat
->toPattern(fullPattern
);
655 #ifdef DTITVFMT_DEBUG
659 fSkeleton
.extract(0, fSkeleton
.length(), result
, "UTF-8");
660 sprintf(mesg
, "in getBestSkeleton: fSkeleton: %s; \n", result
);
663 // fSkeleton is already set by createDateIntervalInstance()
664 // or by createInstance(UnicodeString skeleton, .... )
665 fSkeleton
= DateTimePatternGenerator::staticGetSkeleton(
666 fullPattern
, status
);
667 if ( U_FAILURE(status
) ) {
672 // initialize the fIntervalPattern ordering
674 for ( i
= 0; i
< DateIntervalInfo::kIPI_MAX_INDEX
; ++i
) {
675 fIntervalPatterns
[i
].laterDateFirst
= fInfo
->getDefaultOrder();
678 /* Check whether the skeleton is a combination of date and time.
679 * For the complication reason 1 explained above.
681 UnicodeString dateSkeleton
;
682 UnicodeString timeSkeleton
;
683 UnicodeString normalizedTimeSkeleton
;
684 UnicodeString normalizedDateSkeleton
;
687 /* the difference between time skeleton and normalizedTimeSkeleton are:
688 * 1. (Formerly, normalized time skeleton folded 'H' to 'h'; no longer true)
689 * 2. 'a' is omitted in normalized time skeleton.
690 * 3. there is only one appearance for 'h' or 'H', 'm','v', 'z' in normalized
693 * The difference between date skeleton and normalizedDateSkeleton are:
694 * 1. both 'y' and 'd' appear only once in normalizeDateSkeleton
695 * 2. 'E' and 'EE' are normalized into 'EEE'
696 * 3. 'MM' is normalized into 'M'
698 getDateTimeSkeleton(fSkeleton
, dateSkeleton
, normalizedDateSkeleton
,
699 timeSkeleton
, normalizedTimeSkeleton
);
701 #ifdef DTITVFMT_DEBUG
705 fSkeleton
.extract(0, fSkeleton
.length(), result
, "UTF-8");
706 sprintf(mesg
, "in getBestSkeleton: fSkeleton: %s; \n", result
);
710 // move this up here since we need it for fallbacks
711 if ( timeSkeleton
.length() > 0 && dateSkeleton
.length() > 0 ) {
712 // Need the Date/Time pattern for concatenation of the date
713 // with the time interval.
714 // The date/time pattern ( such as {0} {1} ) is saved in
715 // calendar, that is why need to get the CalendarData here.
716 CalendarData
* calData
= new CalendarData(locale
, NULL
, status
);
717 if ( U_FAILURE(status
) ) {
721 if ( calData
== NULL
) {
722 status
= U_MEMORY_ALLOCATION_ERROR
;
726 const UResourceBundle
* dateTimePatternsRes
= calData
->getByKey(
727 gDateTimePatternsTag
, status
);
728 int32_t dateTimeFormatLength
;
729 const UChar
* dateTimeFormat
= ures_getStringByIndex(
731 (int32_t)DateFormat::kDateTime
,
732 &dateTimeFormatLength
, &status
);
733 if ( U_SUCCESS(status
) && dateTimeFormatLength
>= 3 ) {
734 fDateTimeFormat
= new UnicodeString(dateTimeFormat
, dateTimeFormatLength
);
739 UBool found
= setSeparateDateTimePtn(normalizedDateSkeleton
,
740 normalizedTimeSkeleton
);
742 // for skeletons with seconds, found is false and we enter this block
743 if ( found
== false ) {
745 // TODO: if user asks "m"(minute), but "d"(day) differ
746 if ( timeSkeleton
.length() != 0 ) {
747 if ( dateSkeleton
.length() == 0 ) {
749 timeSkeleton
.insert(0, gDateFormatSkeleton
[DateFormat::kShort
], -1);
750 UnicodeString pattern
= DateFormat::getBestPattern(
751 locale
, timeSkeleton
, status
);
752 if ( U_FAILURE(status
) ) {
755 // for fall back interval patterns,
756 // the first part of the pattern is empty,
757 // the second part of the pattern is the full-pattern
758 // should be used in fall-back.
759 setPatternInfo(UCAL_DATE
, NULL
, &pattern
, fInfo
->getDefaultOrder());
760 setPatternInfo(UCAL_MONTH
, NULL
, &pattern
, fInfo
->getDefaultOrder());
761 setPatternInfo(UCAL_YEAR
, NULL
, &pattern
, fInfo
->getDefaultOrder());
769 } // end of skeleton not found
770 // interval patterns for skeleton are found in resource
771 if ( timeSkeleton
.length() == 0 ) {
773 } else if ( dateSkeleton
.length() == 0 ) {
775 timeSkeleton
.insert(0, gDateFormatSkeleton
[DateFormat::kShort
], -1);
776 UnicodeString pattern
= DateFormat::getBestPattern(
777 locale
, timeSkeleton
, status
);
778 if ( U_FAILURE(status
) ) {
781 // for fall back interval patterns,
782 // the first part of the pattern is empty,
783 // the second part of the pattern is the full-pattern
784 // should be used in fall-back.
785 setPatternInfo(UCAL_DATE
, NULL
, &pattern
, fInfo
->getDefaultOrder());
786 setPatternInfo(UCAL_MONTH
, NULL
, &pattern
, fInfo
->getDefaultOrder());
787 setPatternInfo(UCAL_YEAR
, NULL
, &pattern
, fInfo
->getDefaultOrder());
790 * 1) when the year, month, or day differs,
791 * concatenate the two original expressions with a separator between,
792 * 2) otherwise, present the date followed by the
793 * range expression for the time.
796 * 1) when the year, month, or day differs,
797 * concatenate the two original expressions with a separator between,
799 // if field exists, use fall back
800 UnicodeString skeleton
= fSkeleton
;
801 if ( !fieldExistsInSkeleton(UCAL_DATE
, dateSkeleton
) ) {
802 // prefix skeleton with 'd'
803 skeleton
.insert(0, LOW_D
);
804 setFallbackPattern(UCAL_DATE
, skeleton
, status
);
806 if ( !fieldExistsInSkeleton(UCAL_MONTH
, dateSkeleton
) ) {
807 // then prefix skeleton with 'M'
808 skeleton
.insert(0, CAP_M
);
809 setFallbackPattern(UCAL_MONTH
, skeleton
, status
);
811 if ( !fieldExistsInSkeleton(UCAL_YEAR
, dateSkeleton
) ) {
812 // then prefix skeleton with 'y'
813 skeleton
.insert(0, LOW_Y
);
814 setFallbackPattern(UCAL_YEAR
, skeleton
, status
);
818 * 2) otherwise, present the date followed by the
819 * range expression for the time.
822 if ( fDateTimeFormat
== NULL
) {
823 // earlier failure getting dateTimeFormat
827 UnicodeString datePattern
= DateFormat::getBestPattern(
828 locale
, dateSkeleton
, status
);
830 concatSingleDate2TimeInterval(*fDateTimeFormat
, datePattern
, UCAL_AM_PM
, status
);
831 concatSingleDate2TimeInterval(*fDateTimeFormat
, datePattern
, UCAL_HOUR
, status
);
832 concatSingleDate2TimeInterval(*fDateTimeFormat
, datePattern
, UCAL_MINUTE
, status
);
839 DateIntervalFormat::getDateTimeSkeleton(const UnicodeString
& skeleton
,
840 UnicodeString
& dateSkeleton
,
841 UnicodeString
& normalizedDateSkeleton
,
842 UnicodeString
& timeSkeleton
,
843 UnicodeString
& normalizedTimeSkeleton
) {
844 // dateSkeleton follows the sequence of y*M*E*d*
845 // timeSkeleton follows the sequence of hm*[v|z]?
857 for (i
= 0; i
< skeleton
.length(); ++i
) {
858 UChar ch
= skeleton
[i
];
861 dateSkeleton
.append(ch
);
865 dateSkeleton
.append(ch
);
869 dateSkeleton
.append(ch
);
873 dateSkeleton
.append(ch
);
892 normalizedDateSkeleton
.append(ch
);
893 dateSkeleton
.append(ch
);
896 // 'a' is implicitly handled
897 timeSkeleton
.append(ch
);
900 timeSkeleton
.append(ch
);
904 timeSkeleton
.append(ch
);
908 timeSkeleton
.append(ch
);
913 timeSkeleton
.append(ch
);
917 timeSkeleton
.append(ch
);
927 timeSkeleton
.append(ch
);
928 normalizedTimeSkeleton
.append(ch
);
933 /* generate normalized form for date*/
935 for (i
= 0; i
< yCount
; ++i
) {
936 normalizedDateSkeleton
.append(LOW_Y
);
941 normalizedDateSkeleton
.append(CAP_M
);
944 for ( i
= 0; i
< MCount
&& i
< MAX_M_COUNT
; ++i
) {
945 normalizedDateSkeleton
.append(CAP_M
);
951 normalizedDateSkeleton
.append(CAP_E
);
954 for ( i
= 0; i
< ECount
&& i
< MAX_E_COUNT
; ++i
) {
955 normalizedDateSkeleton
.append(CAP_E
);
960 normalizedDateSkeleton
.append(LOW_D
);
963 /* generate normalized form for time */
965 normalizedTimeSkeleton
.append(CAP_H
);
967 else if ( hCount
!= 0 ) {
968 normalizedTimeSkeleton
.append(LOW_H
);
971 normalizedTimeSkeleton
.append(LOW_M
);
974 normalizedTimeSkeleton
.append(LOW_Z
);
977 normalizedTimeSkeleton
.append(LOW_V
);
983 * Generate date or time interval pattern from resource,
984 * and set them into the interval pattern locale to this formatter.
986 * It needs to handle the following:
987 * 1. need to adjust field width.
988 * For example, the interval patterns saved in DateIntervalInfo
989 * includes "dMMMy", but not "dMMMMy".
990 * Need to get interval patterns for dMMMMy from dMMMy.
991 * Another example, the interval patterns saved in DateIntervalInfo
992 * includes "hmv", but not "hmz".
993 * Need to get interval patterns for "hmz' from 'hmv'
995 * 2. there might be no pattern for 'y' differ for skeleton "Md",
996 * in order to get interval patterns for 'y' differ,
997 * need to look for it from skeleton 'yMd'
999 * @param dateSkeleton normalized date skeleton
1000 * @param timeSkeleton normalized time skeleton
1001 * @return whether the resource is found for the skeleton.
1002 * TRUE if interval pattern found for the skeleton,
1007 DateIntervalFormat::setSeparateDateTimePtn(
1008 const UnicodeString
& dateSkeleton
,
1009 const UnicodeString
& timeSkeleton
) {
1010 const UnicodeString
* skeleton
;
1011 // if both date and time skeleton present,
1012 // the final interval pattern might include time interval patterns
1013 // ( when, am_pm, hour, minute differ ),
1014 // but not date interval patterns ( when year, month, day differ ).
1015 // For year/month/day differ, it falls back to fall-back pattern.
1016 if ( timeSkeleton
.length() != 0 ) {
1017 skeleton
= &timeSkeleton
;
1019 skeleton
= &dateSkeleton
;
1022 /* interval patterns for skeleton "dMMMy" (but not "dMMMMy")
1023 * are defined in resource,
1024 * interval patterns for skeleton "dMMMMy" are calculated by
1025 * 1. get the best match skeleton for "dMMMMy", which is "dMMMy"
1026 * 2. get the interval patterns for "dMMMy",
1027 * 3. extend "MMM" to "MMMM" in above interval patterns for "dMMMMy"
1028 * getBestSkeleton() is step 1.
1030 // best skeleton, and the difference information
1031 int8_t differenceInfo
= 0;
1032 const UnicodeString
* bestSkeleton
= fInfo
->getBestSkeleton(*skeleton
,
1034 /* best skeleton could be NULL.
1035 For example: in "ca" resource file,
1036 interval format is defined as following
1038 fallback{"{0} - {1}"}
1040 there is no skeletons/interval patterns defined,
1041 and the best skeleton match could be NULL
1043 if ( bestSkeleton
== NULL
) {
1047 // Set patterns for fallback use, need to do this
1048 // before returning if differenceInfo == -1
1050 if ( dateSkeleton
.length() != 0) {
1051 status
= U_ZERO_ERROR
;
1052 fDatePattern
= new UnicodeString(DateFormat::getBestPattern(
1053 fLocale
, dateSkeleton
, status
));
1055 if ( timeSkeleton
.length() != 0) {
1056 status
= U_ZERO_ERROR
;
1057 fTimePattern
= new UnicodeString(DateFormat::getBestPattern(
1058 fLocale
, timeSkeleton
, status
));
1062 // 0 means the best matched skeleton is the same as input skeleton
1063 // 1 means the fields are the same, but field width are different
1064 // 2 means the only difference between fields are v/z,
1065 // -1 means there are other fields difference
1066 // (this will happen, for instance, if the supplied skeleton has seconds,
1067 // but no skeletons in the intervalFormats data do)
1068 if ( differenceInfo
== -1 ) {
1069 // skeleton has different fields, not only v/z difference
1073 if ( timeSkeleton
.length() == 0 ) {
1074 UnicodeString extendedSkeleton
;
1075 UnicodeString extendedBestSkeleton
;
1076 // only has date skeleton
1077 setIntervalPattern(UCAL_DATE
, skeleton
, bestSkeleton
, differenceInfo
,
1078 &extendedSkeleton
, &extendedBestSkeleton
);
1080 UBool extended
= setIntervalPattern(UCAL_MONTH
, skeleton
, bestSkeleton
,
1082 &extendedSkeleton
, &extendedBestSkeleton
);
1085 bestSkeleton
= &extendedBestSkeleton
;
1086 skeleton
= &extendedSkeleton
;
1088 setIntervalPattern(UCAL_YEAR
, skeleton
, bestSkeleton
, differenceInfo
,
1089 &extendedSkeleton
, &extendedBestSkeleton
);
1091 setIntervalPattern(UCAL_MINUTE
, skeleton
, bestSkeleton
, differenceInfo
);
1092 setIntervalPattern(UCAL_HOUR
, skeleton
, bestSkeleton
, differenceInfo
);
1093 setIntervalPattern(UCAL_AM_PM
, skeleton
, bestSkeleton
, differenceInfo
);
1101 DateIntervalFormat::setFallbackPattern(UCalendarDateFields field
,
1102 const UnicodeString
& skeleton
,
1103 UErrorCode
& status
) {
1104 if ( U_FAILURE(status
) ) {
1107 UnicodeString pattern
= DateFormat::getBestPattern(
1108 fLocale
, skeleton
, status
);
1109 if ( U_FAILURE(status
) ) {
1112 setPatternInfo(field
, NULL
, &pattern
, fInfo
->getDefaultOrder());
1119 DateIntervalFormat::setPatternInfo(UCalendarDateFields field
,
1120 const UnicodeString
* firstPart
,
1121 const UnicodeString
* secondPart
,
1122 UBool laterDateFirst
) {
1123 // for fall back interval patterns,
1124 // the first part of the pattern is empty,
1125 // the second part of the pattern is the full-pattern
1126 // should be used in fall-back.
1127 UErrorCode status
= U_ZERO_ERROR
;
1128 // following should not set any wrong status.
1129 int32_t itvPtnIndex
= DateIntervalInfo::calendarFieldToIntervalIndex(field
,
1131 if ( U_FAILURE(status
) ) {
1134 PatternInfo
& ptn
= fIntervalPatterns
[itvPtnIndex
];
1136 ptn
.firstPart
= *firstPart
;
1139 ptn
.secondPart
= *secondPart
;
1141 ptn
.laterDateFirst
= laterDateFirst
;
1145 DateIntervalFormat::setIntervalPattern(UCalendarDateFields field
,
1146 const UnicodeString
& intervalPattern
) {
1147 UBool order
= fInfo
->getDefaultOrder();
1148 setIntervalPattern(field
, intervalPattern
, order
);
1153 DateIntervalFormat::setIntervalPattern(UCalendarDateFields field
,
1154 const UnicodeString
& intervalPattern
,
1155 UBool laterDateFirst
) {
1156 const UnicodeString
* pattern
= &intervalPattern
;
1157 UBool order
= laterDateFirst
;
1158 // check for "latestFirst:" or "earliestFirst:" prefix
1159 int8_t prefixLength
= UPRV_LENGTHOF(gLaterFirstPrefix
);
1160 int8_t earliestFirstLength
= UPRV_LENGTHOF(gEarlierFirstPrefix
);
1161 UnicodeString realPattern
;
1162 if ( intervalPattern
.startsWith(gLaterFirstPrefix
, prefixLength
) ) {
1164 intervalPattern
.extract(prefixLength
,
1165 intervalPattern
.length() - prefixLength
,
1167 pattern
= &realPattern
;
1168 } else if ( intervalPattern
.startsWith(gEarlierFirstPrefix
,
1169 earliestFirstLength
) ) {
1171 intervalPattern
.extract(earliestFirstLength
,
1172 intervalPattern
.length() - earliestFirstLength
,
1174 pattern
= &realPattern
;
1177 int32_t splitPoint
= splitPatternInto2Part(*pattern
);
1179 UnicodeString firstPart
;
1180 UnicodeString secondPart
;
1181 pattern
->extract(0, splitPoint
, firstPart
);
1182 if ( splitPoint
< pattern
->length() ) {
1183 pattern
->extract(splitPoint
, pattern
->length()-splitPoint
, secondPart
);
1185 setPatternInfo(field
, &firstPart
, &secondPart
, order
);
1192 * Generate interval pattern from existing resource
1194 * It not only save the interval patterns,
1195 * but also return the extended skeleton and its best match skeleton.
1197 * @param field largest different calendar field
1198 * @param skeleton skeleton
1199 * @param bestSkeleton the best match skeleton which has interval pattern
1200 * defined in resource
1201 * @param differenceInfo the difference between skeleton and best skeleton
1202 * 0 means the best matched skeleton is the same as input skeleton
1203 * 1 means the fields are the same, but field width are different
1204 * 2 means the only difference between fields are v/z,
1205 * -1 means there are other fields difference
1207 * @param extendedSkeleton extended skeleton
1208 * @param extendedBestSkeleton extended best match skeleton
1209 * @return whether the interval pattern is found
1210 * through extending skeleton or not.
1211 * TRUE if interval pattern is found by
1212 * extending skeleton, FALSE otherwise.
1216 DateIntervalFormat::setIntervalPattern(UCalendarDateFields field
,
1217 const UnicodeString
* skeleton
,
1218 const UnicodeString
* bestSkeleton
,
1219 int8_t differenceInfo
,
1220 UnicodeString
* extendedSkeleton
,
1221 UnicodeString
* extendedBestSkeleton
) {
1222 UErrorCode status
= U_ZERO_ERROR
;
1223 // following getIntervalPattern() should not generate error status
1224 UnicodeString pattern
;
1225 fInfo
->getIntervalPattern(*bestSkeleton
, field
, pattern
, status
);
1226 if ( pattern
.isEmpty() ) {
1228 if ( SimpleDateFormat::isFieldUnitIgnored(*bestSkeleton
, field
) ) {
1229 // do nothing, format will handle it
1233 // for 24 hour system, interval patterns in resource file
1234 // might not include pattern when am_pm differ,
1235 // which should be the same as hour differ.
1236 // add it here for simplicity
1237 if ( field
== UCAL_AM_PM
) {
1238 fInfo
->getIntervalPattern(*bestSkeleton
, UCAL_HOUR
, pattern
,status
);
1239 if ( !pattern
.isEmpty() ) {
1240 setIntervalPattern(field
, pattern
);
1244 // else, looking for pattern when 'y' differ for 'dMMMM' skeleton,
1245 // first, get best match pattern "MMMd",
1246 // since there is no pattern for 'y' differs for skeleton 'MMMd',
1247 // need to look for it from skeleton 'yMMMd',
1248 // if found, adjust field width in interval pattern from
1250 UChar fieldLetter
= fgCalendarFieldToPatternLetter
[field
];
1251 if ( extendedSkeleton
) {
1252 *extendedSkeleton
= *skeleton
;
1253 *extendedBestSkeleton
= *bestSkeleton
;
1254 extendedSkeleton
->insert(0, fieldLetter
);
1255 extendedBestSkeleton
->insert(0, fieldLetter
);
1256 // for example, looking for patterns when 'y' differ for
1258 fInfo
->getIntervalPattern(*extendedBestSkeleton
,field
,pattern
,status
);
1259 if ( pattern
.isEmpty() && differenceInfo
== 0 ) {
1260 // if there is no skeleton "yMMMM" defined,
1261 // look for the best match skeleton, for example: "yMMM"
1262 const UnicodeString
* tmpBest
= fInfo
->getBestSkeleton(
1263 *extendedBestSkeleton
, differenceInfo
);
1264 if ( tmpBest
!= 0 && differenceInfo
!= -1 ) {
1265 fInfo
->getIntervalPattern(*tmpBest
, field
, pattern
, status
);
1266 bestSkeleton
= tmpBest
;
1271 if ( !pattern
.isEmpty() ) {
1272 if ( differenceInfo
!= 0 ) {
1273 UnicodeString adjustIntervalPattern
;
1274 adjustFieldWidth(*skeleton
, *bestSkeleton
, pattern
, differenceInfo
,
1275 adjustIntervalPattern
);
1276 setIntervalPattern(field
, adjustIntervalPattern
);
1278 setIntervalPattern(field
, pattern
);
1280 if ( extendedSkeleton
&& !extendedSkeleton
->isEmpty() ) {
1290 DateIntervalFormat::splitPatternInto2Part(const UnicodeString
& intervalPattern
) {
1291 UBool inQuote
= false;
1295 /* repeatedPattern used to record whether a pattern has already seen.
1296 It is a pattern applies to first calendar if it is first time seen,
1297 otherwise, it is a pattern applies to the second calendar
1299 UBool patternRepeated
[] =
1301 // A B C D E F G H I J K L M N O
1302 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1303 // P Q R S T U V W X Y Z
1304 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1305 // a b c d e f g h i j k l m n o
1306 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1307 // p q r s t u v w x y z
1308 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1311 int8_t PATTERN_CHAR_BASE
= 0x41;
1313 /* loop through the pattern string character by character looking for
1314 * the first repeated pattern letter, which breaks the interval pattern
1318 UBool foundRepetition
= false;
1319 for (i
= 0; i
< intervalPattern
.length(); ++i
) {
1320 UChar ch
= intervalPattern
.charAt(i
);
1322 if (ch
!= prevCh
&& count
> 0) {
1323 // check the repeativeness of pattern letter
1324 UBool repeated
= patternRepeated
[(int)(prevCh
- PATTERN_CHAR_BASE
)];
1325 if ( repeated
== FALSE
) {
1326 patternRepeated
[prevCh
- PATTERN_CHAR_BASE
] = TRUE
;
1328 foundRepetition
= true;
1334 // Consecutive single quotes are a single quote literal,
1335 // either outside of quotes or between quotes
1336 if ((i
+1) < intervalPattern
.length() &&
1337 intervalPattern
.charAt(i
+1) == '\'') {
1340 inQuote
= ! inQuote
;
1343 else if (!inQuote
&& ((ch
>= 0x0061 /*'a'*/ && ch
<= 0x007A /*'z'*/)
1344 || (ch
>= 0x0041 /*'A'*/ && ch
<= 0x005A /*'Z'*/))) {
1345 // ch is a date-time pattern character
1350 // check last pattern char, distinguish
1351 // "dd MM" ( no repetition ),
1352 // "d-d"(last char repeated ), and
1353 // "d-d MM" ( repetition found )
1354 if ( count
> 0 && foundRepetition
== FALSE
) {
1355 if ( patternRepeated
[(int)(prevCh
- PATTERN_CHAR_BASE
)] == FALSE
) {
1362 static const UChar bracketedZero
[] = {0x7B,0x30,0x7D};
1363 static const UChar bracketedOne
[] = {0x7B,0x31,0x7D};
1366 DateIntervalFormat::adjustPosition(UnicodeString
& combiningPattern
, // has {0} and {1} in it
1367 UnicodeString
& pat0
, FieldPosition
& pos0
, // pattern and pos corresponding to {0}
1368 UnicodeString
& pat1
, FieldPosition
& pos1
, // pattern and pos corresponding to {1}
1369 FieldPosition
& posResult
) {
1370 int32_t index0
= combiningPattern
.indexOf(bracketedZero
, 3, 0);
1371 int32_t index1
= combiningPattern
.indexOf(bracketedOne
, 3, 0);
1372 if (index0
< 0 || index1
< 0) {
1375 int32_t placeholderLen
= 3; // length of "{0}" or "{1}"
1376 if (index0
< index1
) {
1377 if (pos0
.getEndIndex() > 0) {
1378 posResult
.setBeginIndex(pos0
.getBeginIndex() + index0
);
1379 posResult
.setEndIndex(pos0
.getEndIndex() + index0
);
1380 } else if (pos1
.getEndIndex() > 0) {
1382 index1
+= pat0
.length() - placeholderLen
; // adjust for pat0 replacing {0}
1383 posResult
.setBeginIndex(pos1
.getBeginIndex() + index1
);
1384 posResult
.setEndIndex(pos1
.getEndIndex() + index1
);
1387 if (pos1
.getEndIndex() > 0) {
1388 posResult
.setBeginIndex(pos1
.getBeginIndex() + index1
);
1389 posResult
.setEndIndex(pos1
.getEndIndex() + index1
);
1390 } else if (pos0
.getEndIndex() > 0) {
1392 index0
+= pat1
.length() - placeholderLen
; // adjust for pat1 replacing {1}
1393 posResult
.setBeginIndex(pos0
.getBeginIndex() + index0
);
1394 posResult
.setEndIndex(pos0
.getEndIndex() + index0
);
1400 DateIntervalFormat::fallbackFormat(Calendar
& fromCalendar
,
1401 Calendar
& toCalendar
,
1402 UBool fromToOnSameDay
, // new
1403 UnicodeString
& appendTo
,
1405 UErrorCode
& status
) const {
1406 if ( U_FAILURE(status
) ) {
1409 UnicodeString fullPattern
; // for saving the pattern in fDateFormat
1410 UBool formatDatePlusTimeRange
= (fromToOnSameDay
&& fDatePattern
&& fTimePattern
);
1412 if (formatDatePlusTimeRange
) {
1413 fDateFormat
->toPattern(fullPattern
); // save current pattern, restore later
1414 fDateFormat
->applyPattern(*fTimePattern
);
1416 FieldPosition otherPos
;
1417 otherPos
.setField(pos
.getField());
1418 UnicodeString earlierDate
;
1419 fDateFormat
->format(fromCalendar
, earlierDate
, pos
);
1420 UnicodeString laterDate
;
1421 fDateFormat
->format(toCalendar
, laterDate
, otherPos
);
1422 UnicodeString fallbackPattern
;
1423 fInfo
->getFallbackIntervalPattern(fallbackPattern
);
1424 adjustPosition(fallbackPattern
, earlierDate
, pos
, laterDate
, otherPos
, pos
);
1425 UnicodeString fallbackRange
;
1426 SimpleFormatter(fallbackPattern
, 2, 2, status
).
1427 format(earlierDate
, laterDate
, fallbackRange
, status
);
1428 if ( U_SUCCESS(status
) && formatDatePlusTimeRange
) {
1429 // fallbackRange has just the time range, need to format the date part and combine that
1430 UnicodeString
dateTimeFormatNoQuote(*fDateTimeFormat
);
1431 dateTimeFormatNoQuote
.findAndReplace(UnicodeString(0x0027), UnicodeString());
1432 fDateFormat
->applyPattern(*fDatePattern
);
1433 UnicodeString datePortion
;
1434 otherPos
.setBeginIndex(0);
1435 otherPos
.setEndIndex(0);
1436 fDateFormat
->format(fromCalendar
, datePortion
, otherPos
);
1437 adjustPosition(dateTimeFormatNoQuote
, fallbackRange
, pos
, datePortion
, otherPos
, pos
);
1438 const UnicodeString
*values
[2] = {
1439 &fallbackRange
, // {0} is time range
1440 &datePortion
, // {1} is single date portion
1442 SimpleFormatter(dateTimeFormatNoQuote
, 2, 2, status
).
1443 formatAndReplace(values
, 2, fallbackRange
, NULL
, 0, status
);
1445 if ( U_SUCCESS(status
) ) {
1446 appendTo
.append(fallbackRange
);
1448 if (formatDatePlusTimeRange
) {
1449 // restore full pattern
1450 fDateFormat
->applyPattern(fullPattern
);
1459 DateIntervalFormat::fieldExistsInSkeleton(UCalendarDateFields field
,
1460 const UnicodeString
& skeleton
)
1462 const UChar fieldChar
= fgCalendarFieldToPatternLetter
[field
];
1463 return ( (skeleton
.indexOf(fieldChar
) == -1)?FALSE
:TRUE
) ;
1469 DateIntervalFormat::adjustFieldWidth(const UnicodeString
& inputSkeleton
,
1470 const UnicodeString
& bestMatchSkeleton
,
1471 const UnicodeString
& bestIntervalPattern
,
1472 int8_t differenceInfo
,
1473 UnicodeString
& adjustedPtn
) {
1474 adjustedPtn
= bestIntervalPattern
;
1475 int32_t inputSkeletonFieldWidth
[] =
1477 // A B C D E F G H I J K L M N O
1478 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1479 // P Q R S T U V W X Y Z
1480 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1481 // a b c d e f g h i j k l m n o
1482 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1483 // p q r s t u v w x y z
1484 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1487 int32_t bestMatchSkeletonFieldWidth
[] =
1489 // A B C D E F G H I J K L M N O
1490 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1491 // P Q R S T U V W X Y Z
1492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1493 // a b c d e f g h i j k l m n o
1494 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1495 // p q r s t u v w x y z
1496 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1499 DateIntervalInfo::parseSkeleton(inputSkeleton
, inputSkeletonFieldWidth
);
1500 DateIntervalInfo::parseSkeleton(bestMatchSkeleton
, bestMatchSkeletonFieldWidth
);
1501 if ( differenceInfo
== 2 ) {
1502 adjustedPtn
.findAndReplace(UnicodeString((UChar
)0x76 /* v */),
1503 UnicodeString((UChar
)0x7a /* z */));
1506 UBool inQuote
= false;
1510 const int8_t PATTERN_CHAR_BASE
= 0x41;
1512 // loop through the pattern string character by character
1513 int32_t adjustedPtnLength
= adjustedPtn
.length();
1515 for (i
= 0; i
< adjustedPtnLength
; ++i
) {
1516 UChar ch
= adjustedPtn
.charAt(i
);
1517 if (ch
!= prevCh
&& count
> 0) {
1518 // check the repeativeness of pattern letter
1519 UChar skeletonChar
= prevCh
;
1520 if ( skeletonChar
== CAP_L
) {
1521 // there is no "L" (always be "M") in skeleton,
1522 // but there is "L" in pattern.
1523 // for skeleton "M+", the pattern might be "...L..."
1524 skeletonChar
= CAP_M
;
1526 int32_t fieldCount
= bestMatchSkeletonFieldWidth
[(int)(skeletonChar
- PATTERN_CHAR_BASE
)];
1527 int32_t inputFieldCount
= inputSkeletonFieldWidth
[(int)(skeletonChar
- PATTERN_CHAR_BASE
)];
1528 if ( fieldCount
== count
&& inputFieldCount
> fieldCount
) {
1529 count
= inputFieldCount
- fieldCount
;
1531 for ( j
= 0; j
< count
; ++j
) {
1532 adjustedPtn
.insert(i
, prevCh
);
1535 adjustedPtnLength
+= count
;
1540 // Consecutive single quotes are a single quote literal,
1541 // either outside of quotes or between quotes
1542 if ((i
+1) < adjustedPtn
.length() && adjustedPtn
.charAt(i
+1) == '\'') {
1545 inQuote
= ! inQuote
;
1548 else if ( ! inQuote
&& ((ch
>= 0x0061 /*'a'*/ && ch
<= 0x007A /*'z'*/)
1549 || (ch
>= 0x0041 /*'A'*/ && ch
<= 0x005A /*'Z'*/))) {
1550 // ch is a date-time pattern character
1557 // check the repeativeness of pattern letter
1558 UChar skeletonChar
= prevCh
;
1559 if ( skeletonChar
== CAP_L
) {
1560 // there is no "L" (always be "M") in skeleton,
1561 // but there is "L" in pattern.
1562 // for skeleton "M+", the pattern might be "...L..."
1563 skeletonChar
= CAP_M
;
1565 int32_t fieldCount
= bestMatchSkeletonFieldWidth
[(int)(skeletonChar
- PATTERN_CHAR_BASE
)];
1566 int32_t inputFieldCount
= inputSkeletonFieldWidth
[(int)(skeletonChar
- PATTERN_CHAR_BASE
)];
1567 if ( fieldCount
== count
&& inputFieldCount
> fieldCount
) {
1568 count
= inputFieldCount
- fieldCount
;
1570 for ( j
= 0; j
< count
; ++j
) {
1571 adjustedPtn
.append(prevCh
);
1580 DateIntervalFormat::concatSingleDate2TimeInterval(UnicodeString
& format
,
1581 const UnicodeString
& datePattern
,
1582 UCalendarDateFields field
,
1583 UErrorCode
& status
) {
1584 // following should not set wrong status
1585 int32_t itvPtnIndex
= DateIntervalInfo::calendarFieldToIntervalIndex(field
,
1587 if ( U_FAILURE(status
) ) {
1590 PatternInfo
& timeItvPtnInfo
= fIntervalPatterns
[itvPtnIndex
];
1591 if ( !timeItvPtnInfo
.firstPart
.isEmpty() ) {
1592 UnicodeString
timeIntervalPattern(timeItvPtnInfo
.firstPart
);
1593 timeIntervalPattern
.append(timeItvPtnInfo
.secondPart
);
1594 UnicodeString combinedPattern
;
1595 SimpleFormatter(format
, 2, 2, status
).
1596 format(timeIntervalPattern
, datePattern
, combinedPattern
, status
);
1597 if ( U_FAILURE(status
) ) {
1600 setIntervalPattern(field
, combinedPattern
, timeItvPtnInfo
.laterDateFirst
);
1603 // it should not happen if the interval format defined is valid
1609 DateIntervalFormat::fgCalendarFieldToPatternLetter
[] =
1611 /*GyM*/ CAP_G
, LOW_Y
, CAP_M
,
1612 /*wWd*/ LOW_W
, CAP_W
, LOW_D
,
1613 /*DEF*/ CAP_D
, CAP_E
, CAP_F
,
1614 /*ahH*/ LOW_A
, LOW_H
, CAP_H
,
1615 /*msS*/ LOW_M
, LOW_S
, CAP_S
, // MINUTE, SECOND, MILLISECOND
1616 /*z.Y*/ LOW_Z
, SPACE
, CAP_Y
, // ZONE_OFFSET, DST_OFFSET, YEAR_WOY,
1617 /*eug*/ LOW_E
, LOW_U
, LOW_G
, // DOW_LOCAL, EXTENDED_YEAR, JULIAN_DAY,
1618 /*A..*/ CAP_A
, SPACE
, SPACE
, // MILLISECONDS_IN_DAY, IS_LEAP_MONTH, FIELD_COUNT