/src/icu/source/i18n/reldtfmt.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ******************************************************************************* |
5 | | * Copyright (C) 2007-2016, International Business Machines Corporation and |
6 | | * others. All Rights Reserved. |
7 | | ******************************************************************************* |
8 | | */ |
9 | | |
10 | | #include "unicode/utypes.h" |
11 | | |
12 | | #if !UCONFIG_NO_FORMATTING |
13 | | |
14 | | #include <stdlib.h> |
15 | | |
16 | | #include "unicode/datefmt.h" |
17 | | #include "unicode/reldatefmt.h" |
18 | | #include "unicode/simpleformatter.h" |
19 | | #include "unicode/smpdtfmt.h" |
20 | | #include "unicode/udisplaycontext.h" |
21 | | #include "unicode/uchar.h" |
22 | | #include "unicode/brkiter.h" |
23 | | #include "unicode/ucasemap.h" |
24 | | #include "reldtfmt.h" |
25 | | #include "cmemory.h" |
26 | | #include "uresimp.h" |
27 | | |
28 | | U_NAMESPACE_BEGIN |
29 | | |
30 | | |
31 | | /** |
32 | | * An array of URelativeString structs is used to store the resource data loaded out of the bundle. |
33 | | */ |
34 | | struct URelativeString { |
35 | | int32_t offset; /** offset of this item, such as, the relative date **/ |
36 | | int32_t len; /** length of the string **/ |
37 | | const UChar* string; /** string, or NULL if not set **/ |
38 | | }; |
39 | | |
40 | | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RelativeDateFormat) |
41 | | |
42 | | RelativeDateFormat::RelativeDateFormat(const RelativeDateFormat& other) : |
43 | 0 | DateFormat(other), fDateTimeFormatter(NULL), fDatePattern(other.fDatePattern), |
44 | 0 | fTimePattern(other.fTimePattern), fCombinedFormat(NULL), |
45 | 0 | fDateStyle(other.fDateStyle), fLocale(other.fLocale), |
46 | 0 | fDatesLen(other.fDatesLen), fDates(NULL), |
47 | 0 | fCombinedHasDateAtStart(other.fCombinedHasDateAtStart), |
48 | 0 | fCapitalizationInfoSet(other.fCapitalizationInfoSet), |
49 | 0 | fCapitalizationOfRelativeUnitsForUIListMenu(other.fCapitalizationOfRelativeUnitsForUIListMenu), |
50 | 0 | fCapitalizationOfRelativeUnitsForStandAlone(other.fCapitalizationOfRelativeUnitsForStandAlone), |
51 | | fCapitalizationBrkIter(NULL) |
52 | 0 | { |
53 | 0 | if(other.fDateTimeFormatter != NULL) { |
54 | 0 | fDateTimeFormatter = other.fDateTimeFormatter->clone(); |
55 | 0 | } |
56 | 0 | if(other.fCombinedFormat != NULL) { |
57 | 0 | fCombinedFormat = new SimpleFormatter(*other.fCombinedFormat); |
58 | 0 | } |
59 | 0 | if (fDatesLen > 0) { |
60 | 0 | fDates = (URelativeString*) uprv_malloc(sizeof(fDates[0])*(size_t)fDatesLen); |
61 | 0 | uprv_memcpy(fDates, other.fDates, sizeof(fDates[0])*(size_t)fDatesLen); |
62 | 0 | } |
63 | 0 | #if !UCONFIG_NO_BREAK_ITERATION |
64 | 0 | if (other.fCapitalizationBrkIter != NULL) { |
65 | 0 | fCapitalizationBrkIter = (other.fCapitalizationBrkIter)->clone(); |
66 | 0 | } |
67 | 0 | #endif |
68 | 0 | } |
69 | | |
70 | | RelativeDateFormat::RelativeDateFormat( UDateFormatStyle timeStyle, UDateFormatStyle dateStyle, |
71 | | const Locale& locale, UErrorCode& status) : |
72 | 0 | DateFormat(), fDateTimeFormatter(NULL), fDatePattern(), fTimePattern(), fCombinedFormat(NULL), |
73 | 0 | fDateStyle(dateStyle), fLocale(locale), fDatesLen(0), fDates(NULL), |
74 | 0 | fCombinedHasDateAtStart(FALSE), fCapitalizationInfoSet(FALSE), |
75 | 0 | fCapitalizationOfRelativeUnitsForUIListMenu(FALSE), fCapitalizationOfRelativeUnitsForStandAlone(FALSE), |
76 | | fCapitalizationBrkIter(NULL) |
77 | 0 | { |
78 | 0 | if(U_FAILURE(status) ) { |
79 | 0 | return; |
80 | 0 | } |
81 | | |
82 | 0 | if (timeStyle < UDAT_NONE || timeStyle > UDAT_SHORT) { |
83 | | // don't support other time styles (e.g. relative styles), for now |
84 | 0 | status = U_ILLEGAL_ARGUMENT_ERROR; |
85 | 0 | return; |
86 | 0 | } |
87 | 0 | UDateFormatStyle baseDateStyle = (dateStyle > UDAT_SHORT)? (UDateFormatStyle)(dateStyle & ~UDAT_RELATIVE): dateStyle; |
88 | 0 | DateFormat * df; |
89 | | // Get fDateTimeFormatter from either date or time style (does not matter, we will override the pattern). |
90 | | // We do need to get separate patterns for the date & time styles. |
91 | 0 | if (baseDateStyle != UDAT_NONE) { |
92 | 0 | df = createDateInstance((EStyle)baseDateStyle, locale); |
93 | 0 | fDateTimeFormatter=dynamic_cast<SimpleDateFormat *>(df); |
94 | 0 | if (fDateTimeFormatter == NULL) { |
95 | 0 | status = U_UNSUPPORTED_ERROR; |
96 | 0 | return; |
97 | 0 | } |
98 | 0 | fDateTimeFormatter->toPattern(fDatePattern); |
99 | 0 | if (timeStyle != UDAT_NONE) { |
100 | 0 | df = createTimeInstance((EStyle)timeStyle, locale); |
101 | 0 | SimpleDateFormat *sdf = dynamic_cast<SimpleDateFormat *>(df); |
102 | 0 | if (sdf != NULL) { |
103 | 0 | sdf->toPattern(fTimePattern); |
104 | 0 | delete sdf; |
105 | 0 | } |
106 | 0 | } |
107 | 0 | } else { |
108 | | // does not matter whether timeStyle is UDAT_NONE, we need something for fDateTimeFormatter |
109 | 0 | df = createTimeInstance((EStyle)timeStyle, locale); |
110 | 0 | fDateTimeFormatter=dynamic_cast<SimpleDateFormat *>(df); |
111 | 0 | if (fDateTimeFormatter == NULL) { |
112 | 0 | status = U_UNSUPPORTED_ERROR; |
113 | 0 | delete df; |
114 | 0 | return; |
115 | 0 | } |
116 | 0 | fDateTimeFormatter->toPattern(fTimePattern); |
117 | 0 | } |
118 | | |
119 | | // Initialize the parent fCalendar, so that parse() works correctly. |
120 | 0 | initializeCalendar(NULL, locale, status); |
121 | 0 | loadDates(status); |
122 | 0 | } |
123 | | |
124 | 0 | RelativeDateFormat::~RelativeDateFormat() { |
125 | 0 | delete fDateTimeFormatter; |
126 | 0 | delete fCombinedFormat; |
127 | 0 | uprv_free(fDates); |
128 | 0 | #if !UCONFIG_NO_BREAK_ITERATION |
129 | 0 | delete fCapitalizationBrkIter; |
130 | 0 | #endif |
131 | 0 | } |
132 | | |
133 | | |
134 | 0 | RelativeDateFormat* RelativeDateFormat::clone() const { |
135 | 0 | return new RelativeDateFormat(*this); |
136 | 0 | } |
137 | | |
138 | 0 | bool RelativeDateFormat::operator==(const Format& other) const { |
139 | 0 | if(DateFormat::operator==(other)) { |
140 | | // The DateFormat::operator== check for fCapitalizationContext equality above |
141 | | // is sufficient to check equality of all derived context-related data. |
142 | | // DateFormat::operator== guarantees following cast is safe |
143 | 0 | RelativeDateFormat* that = (RelativeDateFormat*)&other; |
144 | 0 | return (fDateStyle==that->fDateStyle && |
145 | 0 | fDatePattern==that->fDatePattern && |
146 | 0 | fTimePattern==that->fTimePattern && |
147 | 0 | fLocale==that->fLocale ); |
148 | 0 | } |
149 | 0 | return FALSE; |
150 | 0 | } |
151 | | |
152 | | static const UChar APOSTROPHE = (UChar)0x0027; |
153 | | |
154 | | UnicodeString& RelativeDateFormat::format( Calendar& cal, |
155 | | UnicodeString& appendTo, |
156 | 0 | FieldPosition& pos) const { |
157 | | |
158 | 0 | UErrorCode status = U_ZERO_ERROR; |
159 | 0 | UnicodeString relativeDayString; |
160 | 0 | UDisplayContext capitalizationContext = getContext(UDISPCTX_TYPE_CAPITALIZATION, status); |
161 | | |
162 | | // calculate the difference, in days, between 'cal' and now. |
163 | 0 | int dayDiff = dayDifference(cal, status); |
164 | | |
165 | | // look up string |
166 | 0 | int32_t len = 0; |
167 | 0 | const UChar *theString = getStringForDay(dayDiff, len, status); |
168 | 0 | if(U_SUCCESS(status) && (theString!=NULL)) { |
169 | | // found a relative string |
170 | 0 | relativeDayString.setTo(theString, len); |
171 | 0 | } |
172 | |
|
173 | 0 | if ( relativeDayString.length() > 0 && !fDatePattern.isEmpty() && |
174 | 0 | (fTimePattern.isEmpty() || fCombinedFormat == NULL || fCombinedHasDateAtStart)) { |
175 | 0 | #if !UCONFIG_NO_BREAK_ITERATION |
176 | | // capitalize relativeDayString according to context for relative, set formatter no context |
177 | 0 | if ( u_islower(relativeDayString.char32At(0)) && fCapitalizationBrkIter!= NULL && |
178 | 0 | ( capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE || |
179 | 0 | (capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU && fCapitalizationOfRelativeUnitsForUIListMenu) || |
180 | 0 | (capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_STANDALONE && fCapitalizationOfRelativeUnitsForStandAlone) ) ) { |
181 | | // titlecase first word of relativeDayString |
182 | 0 | relativeDayString.toTitle(fCapitalizationBrkIter, fLocale, U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT); |
183 | 0 | } |
184 | 0 | #endif |
185 | 0 | fDateTimeFormatter->setContext(UDISPCTX_CAPITALIZATION_NONE, status); |
186 | 0 | } else { |
187 | | // set our context for the formatter |
188 | 0 | fDateTimeFormatter->setContext(capitalizationContext, status); |
189 | 0 | } |
190 | |
|
191 | 0 | if (fDatePattern.isEmpty()) { |
192 | 0 | fDateTimeFormatter->applyPattern(fTimePattern); |
193 | 0 | fDateTimeFormatter->format(cal,appendTo,pos); |
194 | 0 | } else if (fTimePattern.isEmpty() || fCombinedFormat == NULL) { |
195 | 0 | if (relativeDayString.length() > 0) { |
196 | 0 | appendTo.append(relativeDayString); |
197 | 0 | } else { |
198 | 0 | fDateTimeFormatter->applyPattern(fDatePattern); |
199 | 0 | fDateTimeFormatter->format(cal,appendTo,pos); |
200 | 0 | } |
201 | 0 | } else { |
202 | 0 | UnicodeString datePattern; |
203 | 0 | if (relativeDayString.length() > 0) { |
204 | | // Need to quote the relativeDayString to make it a legal date pattern |
205 | 0 | relativeDayString.findAndReplace(UNICODE_STRING("'", 1), UNICODE_STRING("''", 2)); // double any existing APOSTROPHE |
206 | 0 | relativeDayString.insert(0, APOSTROPHE); // add APOSTROPHE at beginning... |
207 | 0 | relativeDayString.append(APOSTROPHE); // and at end |
208 | 0 | datePattern.setTo(relativeDayString); |
209 | 0 | } else { |
210 | 0 | datePattern.setTo(fDatePattern); |
211 | 0 | } |
212 | 0 | UnicodeString combinedPattern; |
213 | 0 | fCombinedFormat->format(fTimePattern, datePattern, combinedPattern, status); |
214 | 0 | fDateTimeFormatter->applyPattern(combinedPattern); |
215 | 0 | fDateTimeFormatter->format(cal,appendTo,pos); |
216 | 0 | } |
217 | |
|
218 | 0 | return appendTo; |
219 | 0 | } |
220 | | |
221 | | |
222 | | |
223 | | UnicodeString& |
224 | | RelativeDateFormat::format(const Formattable& obj, |
225 | | UnicodeString& appendTo, |
226 | | FieldPosition& pos, |
227 | | UErrorCode& status) const |
228 | 0 | { |
229 | | // this is just here to get around the hiding problem |
230 | | // (the previous format() override would hide the version of |
231 | | // format() on DateFormat that this function correspond to, so we |
232 | | // have to redefine it here) |
233 | 0 | return DateFormat::format(obj, appendTo, pos, status); |
234 | 0 | } |
235 | | |
236 | | |
237 | | void RelativeDateFormat::parse( const UnicodeString& text, |
238 | | Calendar& cal, |
239 | 0 | ParsePosition& pos) const { |
240 | |
|
241 | 0 | int32_t startIndex = pos.getIndex(); |
242 | 0 | if (fDatePattern.isEmpty()) { |
243 | | // no date pattern, try parsing as time |
244 | 0 | fDateTimeFormatter->applyPattern(fTimePattern); |
245 | 0 | fDateTimeFormatter->parse(text,cal,pos); |
246 | 0 | } else if (fTimePattern.isEmpty() || fCombinedFormat == NULL) { |
247 | | // no time pattern or way to combine, try parsing as date |
248 | | // first check whether text matches a relativeDayString |
249 | 0 | UBool matchedRelative = FALSE; |
250 | 0 | for (int n=0; n < fDatesLen && !matchedRelative; n++) { |
251 | 0 | if (fDates[n].string != NULL && |
252 | 0 | text.compare(startIndex, fDates[n].len, fDates[n].string) == 0) { |
253 | | // it matched, handle the relative day string |
254 | 0 | UErrorCode status = U_ZERO_ERROR; |
255 | 0 | matchedRelative = TRUE; |
256 | | |
257 | | // Set the calendar to now+offset |
258 | 0 | cal.setTime(Calendar::getNow(),status); |
259 | 0 | cal.add(UCAL_DATE,fDates[n].offset, status); |
260 | |
|
261 | 0 | if(U_FAILURE(status)) { |
262 | | // failure in setting calendar field, set offset to beginning of rel day string |
263 | 0 | pos.setErrorIndex(startIndex); |
264 | 0 | } else { |
265 | 0 | pos.setIndex(startIndex + fDates[n].len); |
266 | 0 | } |
267 | 0 | } |
268 | 0 | } |
269 | 0 | if (!matchedRelative) { |
270 | | // just parse as normal date |
271 | 0 | fDateTimeFormatter->applyPattern(fDatePattern); |
272 | 0 | fDateTimeFormatter->parse(text,cal,pos); |
273 | 0 | } |
274 | 0 | } else { |
275 | | // Here we replace any relativeDayString in text with the equivalent date |
276 | | // formatted per fDatePattern, then parse text normally using the combined pattern. |
277 | 0 | UnicodeString modifiedText(text); |
278 | 0 | FieldPosition fPos; |
279 | 0 | int32_t dateStart = 0, origDateLen = 0, modDateLen = 0; |
280 | 0 | UErrorCode status = U_ZERO_ERROR; |
281 | 0 | for (int n=0; n < fDatesLen; n++) { |
282 | 0 | int32_t relativeStringOffset; |
283 | 0 | if (fDates[n].string != NULL && |
284 | 0 | (relativeStringOffset = modifiedText.indexOf(fDates[n].string, fDates[n].len, startIndex)) >= startIndex) { |
285 | | // it matched, replace the relative date with a real one for parsing |
286 | 0 | UnicodeString dateString; |
287 | 0 | Calendar * tempCal = cal.clone(); |
288 | | |
289 | | // Set the calendar to now+offset |
290 | 0 | tempCal->setTime(Calendar::getNow(),status); |
291 | 0 | tempCal->add(UCAL_DATE,fDates[n].offset, status); |
292 | 0 | if(U_FAILURE(status)) { |
293 | 0 | pos.setErrorIndex(startIndex); |
294 | 0 | delete tempCal; |
295 | 0 | return; |
296 | 0 | } |
297 | | |
298 | 0 | fDateTimeFormatter->applyPattern(fDatePattern); |
299 | 0 | fDateTimeFormatter->format(*tempCal, dateString, fPos); |
300 | 0 | dateStart = relativeStringOffset; |
301 | 0 | origDateLen = fDates[n].len; |
302 | 0 | modDateLen = dateString.length(); |
303 | 0 | modifiedText.replace(dateStart, origDateLen, dateString); |
304 | 0 | delete tempCal; |
305 | 0 | break; |
306 | 0 | } |
307 | 0 | } |
308 | 0 | UnicodeString combinedPattern; |
309 | 0 | fCombinedFormat->format(fTimePattern, fDatePattern, combinedPattern, status); |
310 | 0 | fDateTimeFormatter->applyPattern(combinedPattern); |
311 | 0 | fDateTimeFormatter->parse(modifiedText,cal,pos); |
312 | | |
313 | | // Adjust offsets |
314 | 0 | UBool noError = (pos.getErrorIndex() < 0); |
315 | 0 | int32_t offset = (noError)? pos.getIndex(): pos.getErrorIndex(); |
316 | 0 | if (offset >= dateStart + modDateLen) { |
317 | | // offset at or after the end of the replaced text, |
318 | | // correct by the difference between original and replacement |
319 | 0 | offset -= (modDateLen - origDateLen); |
320 | 0 | } else if (offset >= dateStart) { |
321 | | // offset in the replaced text, set it to the beginning of that text |
322 | | // (i.e. the beginning of the relative day string) |
323 | 0 | offset = dateStart; |
324 | 0 | } |
325 | 0 | if (noError) { |
326 | 0 | pos.setIndex(offset); |
327 | 0 | } else { |
328 | 0 | pos.setErrorIndex(offset); |
329 | 0 | } |
330 | 0 | } |
331 | 0 | } |
332 | | |
333 | | UDate |
334 | | RelativeDateFormat::parse( const UnicodeString& text, |
335 | 0 | ParsePosition& pos) const { |
336 | | // redefined here because the other parse() function hides this function's |
337 | | // counterpart on DateFormat |
338 | 0 | return DateFormat::parse(text, pos); |
339 | 0 | } |
340 | | |
341 | | UDate |
342 | | RelativeDateFormat::parse(const UnicodeString& text, UErrorCode& status) const |
343 | 0 | { |
344 | | // redefined here because the other parse() function hides this function's |
345 | | // counterpart on DateFormat |
346 | 0 | return DateFormat::parse(text, status); |
347 | 0 | } |
348 | | |
349 | | |
350 | 0 | const UChar *RelativeDateFormat::getStringForDay(int32_t day, int32_t &len, UErrorCode &status) const { |
351 | 0 | if(U_FAILURE(status)) { |
352 | 0 | return NULL; |
353 | 0 | } |
354 | | |
355 | | // Is it inside the resource bundle's range? |
356 | 0 | int n = day + UDAT_DIRECTION_THIS; |
357 | 0 | if (n >= 0 && n < fDatesLen) { |
358 | 0 | if (fDates[n].offset == day && fDates[n].string != NULL) { |
359 | 0 | len = fDates[n].len; |
360 | 0 | return fDates[n].string; |
361 | 0 | } |
362 | 0 | } |
363 | 0 | return NULL; // not found. |
364 | 0 | } |
365 | | |
366 | | UnicodeString& |
367 | | RelativeDateFormat::toPattern(UnicodeString& result, UErrorCode& status) const |
368 | 0 | { |
369 | 0 | if (!U_FAILURE(status)) { |
370 | 0 | result.remove(); |
371 | 0 | if (fDatePattern.isEmpty()) { |
372 | 0 | result.setTo(fTimePattern); |
373 | 0 | } else if (fTimePattern.isEmpty() || fCombinedFormat == NULL) { |
374 | 0 | result.setTo(fDatePattern); |
375 | 0 | } else { |
376 | 0 | fCombinedFormat->format(fTimePattern, fDatePattern, result, status); |
377 | 0 | } |
378 | 0 | } |
379 | 0 | return result; |
380 | 0 | } |
381 | | |
382 | | UnicodeString& |
383 | | RelativeDateFormat::toPatternDate(UnicodeString& result, UErrorCode& status) const |
384 | 0 | { |
385 | 0 | if (!U_FAILURE(status)) { |
386 | 0 | result.remove(); |
387 | 0 | result.setTo(fDatePattern); |
388 | 0 | } |
389 | 0 | return result; |
390 | 0 | } |
391 | | |
392 | | UnicodeString& |
393 | | RelativeDateFormat::toPatternTime(UnicodeString& result, UErrorCode& status) const |
394 | 0 | { |
395 | 0 | if (!U_FAILURE(status)) { |
396 | 0 | result.remove(); |
397 | 0 | result.setTo(fTimePattern); |
398 | 0 | } |
399 | 0 | return result; |
400 | 0 | } |
401 | | |
402 | | void |
403 | | RelativeDateFormat::applyPatterns(const UnicodeString& datePattern, const UnicodeString& timePattern, UErrorCode &status) |
404 | 0 | { |
405 | 0 | if (!U_FAILURE(status)) { |
406 | 0 | fDatePattern.setTo(datePattern); |
407 | 0 | fTimePattern.setTo(timePattern); |
408 | 0 | } |
409 | 0 | } |
410 | | |
411 | | const DateFormatSymbols* |
412 | | RelativeDateFormat::getDateFormatSymbols() const |
413 | 0 | { |
414 | 0 | return fDateTimeFormatter->getDateFormatSymbols(); |
415 | 0 | } |
416 | | |
417 | | // override the DateFormat implementation in order to |
418 | | // lazily initialize relevant items |
419 | | void |
420 | | RelativeDateFormat::setContext(UDisplayContext value, UErrorCode& status) |
421 | 0 | { |
422 | 0 | DateFormat::setContext(value, status); |
423 | 0 | if (U_SUCCESS(status)) { |
424 | 0 | if (!fCapitalizationInfoSet && |
425 | 0 | (value==UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU || value==UDISPCTX_CAPITALIZATION_FOR_STANDALONE)) { |
426 | 0 | initCapitalizationContextInfo(fLocale); |
427 | 0 | fCapitalizationInfoSet = TRUE; |
428 | 0 | } |
429 | 0 | #if !UCONFIG_NO_BREAK_ITERATION |
430 | 0 | if ( fCapitalizationBrkIter == NULL && (value==UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE || |
431 | 0 | (value==UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU && fCapitalizationOfRelativeUnitsForUIListMenu) || |
432 | 0 | (value==UDISPCTX_CAPITALIZATION_FOR_STANDALONE && fCapitalizationOfRelativeUnitsForStandAlone)) ) { |
433 | 0 | status = U_ZERO_ERROR; |
434 | 0 | fCapitalizationBrkIter = BreakIterator::createSentenceInstance(fLocale, status); |
435 | 0 | if (U_FAILURE(status)) { |
436 | 0 | delete fCapitalizationBrkIter; |
437 | 0 | fCapitalizationBrkIter = NULL; |
438 | 0 | } |
439 | 0 | } |
440 | 0 | #endif |
441 | 0 | } |
442 | 0 | } |
443 | | |
444 | | void |
445 | | RelativeDateFormat::initCapitalizationContextInfo(const Locale& thelocale) |
446 | 0 | { |
447 | 0 | #if !UCONFIG_NO_BREAK_ITERATION |
448 | 0 | const char * localeID = (thelocale != NULL)? thelocale.getBaseName(): NULL; |
449 | 0 | UErrorCode status = U_ZERO_ERROR; |
450 | 0 | LocalUResourceBundlePointer rb(ures_open(NULL, localeID, &status)); |
451 | 0 | ures_getByKeyWithFallback(rb.getAlias(), |
452 | 0 | "contextTransforms/relative", |
453 | 0 | rb.getAlias(), &status); |
454 | 0 | if (U_SUCCESS(status) && rb != NULL) { |
455 | 0 | int32_t len = 0; |
456 | 0 | const int32_t * intVector = ures_getIntVector(rb.getAlias(), |
457 | 0 | &len, &status); |
458 | 0 | if (U_SUCCESS(status) && intVector != NULL && len >= 2) { |
459 | 0 | fCapitalizationOfRelativeUnitsForUIListMenu = static_cast<UBool>(intVector[0]); |
460 | 0 | fCapitalizationOfRelativeUnitsForStandAlone = static_cast<UBool>(intVector[1]); |
461 | 0 | } |
462 | 0 | } |
463 | 0 | #endif |
464 | 0 | } |
465 | | |
466 | | namespace { |
467 | | |
468 | | /** |
469 | | * Sink for getting data from fields/day/relative data. |
470 | | * For loading relative day names, e.g., "yesterday", "today". |
471 | | */ |
472 | | |
473 | | struct RelDateFmtDataSink : public ResourceSink { |
474 | | URelativeString *fDatesPtr; |
475 | | int32_t fDatesLen; |
476 | | |
477 | 0 | RelDateFmtDataSink(URelativeString* fDates, int32_t len) : fDatesPtr(fDates), fDatesLen(len) { |
478 | 0 | for (int32_t i = 0; i < fDatesLen; ++i) { |
479 | 0 | fDatesPtr[i].offset = 0; |
480 | 0 | fDatesPtr[i].string = NULL; |
481 | 0 | fDatesPtr[i].len = -1; |
482 | 0 | } |
483 | 0 | } |
484 | | |
485 | | virtual ~RelDateFmtDataSink(); |
486 | | |
487 | | virtual void put(const char *key, ResourceValue &value, |
488 | 0 | UBool /*noFallback*/, UErrorCode &errorCode) { |
489 | 0 | ResourceTable relDayTable = value.getTable(errorCode); |
490 | 0 | int32_t n = 0; |
491 | 0 | int32_t len = 0; |
492 | 0 | for (int32_t i = 0; relDayTable.getKeyAndValue(i, key, value); ++i) { |
493 | | // Find the relative offset. |
494 | 0 | int32_t offset = atoi(key); |
495 | | |
496 | | // Put in the proper spot, but don't override existing data. |
497 | 0 | n = offset + UDAT_DIRECTION_THIS; // Converts to index in UDAT_R |
498 | 0 | if (n < fDatesLen && fDatesPtr[n].string == NULL) { |
499 | | // Not found and n is an empty slot. |
500 | 0 | fDatesPtr[n].offset = offset; |
501 | 0 | fDatesPtr[n].string = value.getString(len, errorCode); |
502 | 0 | fDatesPtr[n].len = len; |
503 | 0 | } |
504 | 0 | } |
505 | 0 | } |
506 | | }; |
507 | | |
508 | | |
509 | | // Virtual destructors must be defined out of line. |
510 | | RelDateFmtDataSink::~RelDateFmtDataSink() {} |
511 | | |
512 | | } // Namespace |
513 | | |
514 | | |
515 | | static const UChar patItem1[] = {0x7B,0x31,0x7D}; // "{1}" |
516 | | static const int32_t patItem1Len = 3; |
517 | | |
518 | 0 | void RelativeDateFormat::loadDates(UErrorCode &status) { |
519 | 0 | UResourceBundle *rb = ures_open(NULL, fLocale.getBaseName(), &status); |
520 | 0 | LocalUResourceBundlePointer dateTimePatterns( |
521 | 0 | ures_getByKeyWithFallback(rb, |
522 | 0 | "calendar/gregorian/DateTimePatterns", |
523 | 0 | (UResourceBundle*)NULL, &status)); |
524 | 0 | if(U_SUCCESS(status)) { |
525 | 0 | int32_t patternsSize = ures_getSize(dateTimePatterns.getAlias()); |
526 | 0 | if (patternsSize > kDateTime) { |
527 | 0 | int32_t resStrLen = 0; |
528 | 0 | int32_t glueIndex = kDateTime; |
529 | 0 | if (patternsSize >= (kDateTimeOffset + kShort + 1)) { |
530 | 0 | int32_t offsetIncrement = (fDateStyle & ~kRelative); // Remove relative bit. |
531 | 0 | if (offsetIncrement >= (int32_t)kFull && |
532 | 0 | offsetIncrement <= (int32_t)kShortRelative) { |
533 | 0 | glueIndex = kDateTimeOffset + offsetIncrement; |
534 | 0 | } |
535 | 0 | } |
536 | |
|
537 | 0 | const UChar *resStr = ures_getStringByIndex(dateTimePatterns.getAlias(), glueIndex, &resStrLen, &status); |
538 | 0 | if (U_SUCCESS(status) && resStrLen >= patItem1Len && u_strncmp(resStr,patItem1,patItem1Len)==0) { |
539 | 0 | fCombinedHasDateAtStart = TRUE; |
540 | 0 | } |
541 | 0 | fCombinedFormat = new SimpleFormatter(UnicodeString(TRUE, resStr, resStrLen), 2, 2, status); |
542 | 0 | } |
543 | 0 | } |
544 | | |
545 | | // Data loading for relative names, e.g., "yesterday", "today", "tomorrow". |
546 | 0 | fDatesLen = UDAT_DIRECTION_COUNT; // Maximum defined by data. |
547 | 0 | fDates = (URelativeString*) uprv_malloc(sizeof(fDates[0])*fDatesLen); |
548 | |
|
549 | 0 | RelDateFmtDataSink sink(fDates, fDatesLen); |
550 | 0 | ures_getAllItemsWithFallback(rb, "fields/day/relative", sink, status); |
551 | |
|
552 | 0 | ures_close(rb); |
553 | |
|
554 | 0 | if(U_FAILURE(status)) { |
555 | 0 | fDatesLen=0; |
556 | 0 | return; |
557 | 0 | } |
558 | 0 | } |
559 | | |
560 | | //---------------------------------------------------------------------- |
561 | | |
562 | | // this should to be in DateFormat, instead it was copied from SimpleDateFormat. |
563 | | |
564 | | Calendar* |
565 | | RelativeDateFormat::initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status) |
566 | 0 | { |
567 | 0 | if(!U_FAILURE(status)) { |
568 | 0 | fCalendar = Calendar::createInstance(adoptZone?adoptZone:TimeZone::createDefault(), locale, status); |
569 | 0 | } |
570 | 0 | if (U_SUCCESS(status) && fCalendar == NULL) { |
571 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
572 | 0 | } |
573 | 0 | return fCalendar; |
574 | 0 | } |
575 | | |
576 | 0 | int32_t RelativeDateFormat::dayDifference(Calendar &cal, UErrorCode &status) { |
577 | 0 | if(U_FAILURE(status)) { |
578 | 0 | return 0; |
579 | 0 | } |
580 | | // TODO: Cache the nowCal to avoid heap allocs? Would be difficult, don't know the calendar type |
581 | 0 | Calendar *nowCal = cal.clone(); |
582 | 0 | nowCal->setTime(Calendar::getNow(), status); |
583 | | |
584 | | // For the day difference, we are interested in the difference in the (modified) julian day number |
585 | | // which is midnight to midnight. Using fieldDifference() is NOT correct here, because |
586 | | // 6pm Jan 4th to 10am Jan 5th should be considered "tomorrow". |
587 | 0 | int32_t dayDiff = cal.get(UCAL_JULIAN_DAY, status) - nowCal->get(UCAL_JULIAN_DAY, status); |
588 | |
|
589 | 0 | delete nowCal; |
590 | 0 | return dayDiff; |
591 | 0 | } |
592 | | |
593 | | U_NAMESPACE_END |
594 | | |
595 | | #endif /* !UCONFIG_NO_FORMATTING */ |