OLD | NEW |
1 /* | 1 /* |
2 ******************************************************************************* | 2 ******************************************************************************* |
3 * Copyright (C) 1997-2014, International Business Machines Corporation and * | 3 * Copyright (C) 1997-2015, International Business Machines Corporation and * |
4 * others. All Rights Reserved. * | 4 * others. All Rights Reserved. * |
5 ******************************************************************************* | 5 ******************************************************************************* |
6 * | 6 * |
7 * File DATEFMT.CPP | 7 * File DATEFMT.CPP |
8 * | 8 * |
9 * Modification History: | 9 * Modification History: |
10 * | 10 * |
11 * Date Name Description | 11 * Date Name Description |
12 * 02/19/97 aliu Converted from java. | 12 * 02/19/97 aliu Converted from java. |
13 * 03/31/97 aliu Modified extensively to work with 50 locales. | 13 * 03/31/97 aliu Modified extensively to work with 50 locales. |
14 * 04/01/97 aliu Added support for centuries. | 14 * 04/01/97 aliu Added support for centuries. |
15 * 08/12/97 aliu Fixed operator== to use Calendar::equivalentTo. | 15 * 08/12/97 aliu Fixed operator== to use Calendar::equivalentTo. |
16 * 07/20/98 stephen Changed ParsePosition initialization | 16 * 07/20/98 stephen Changed ParsePosition initialization |
17 *******************************************************************************
* | 17 *******************************************************************************
* |
18 */ | 18 */ |
19 | 19 |
20 #include "unicode/utypes.h" | 20 #include "unicode/utypes.h" |
21 | 21 |
22 #if !UCONFIG_NO_FORMATTING | 22 #if !UCONFIG_NO_FORMATTING |
23 | 23 |
24 #include "unicode/ures.h" | 24 #include "unicode/ures.h" |
25 #include "unicode/datefmt.h" | 25 #include "unicode/datefmt.h" |
26 #include "unicode/smpdtfmt.h" | 26 #include "unicode/smpdtfmt.h" |
27 #include "unicode/dtptngen.h" | 27 #include "unicode/dtptngen.h" |
28 #include "unicode/udisplaycontext.h" | 28 #include "unicode/udisplaycontext.h" |
29 #include "reldtfmt.h" | 29 #include "reldtfmt.h" |
| 30 #include "sharedobject.h" |
| 31 #include "unifiedcache.h" |
| 32 #include "uarrsort.h" |
30 | 33 |
31 #include "cstring.h" | 34 #include "cstring.h" |
32 #include "windtfmt.h" | 35 #include "windtfmt.h" |
33 | 36 |
34 #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL) | 37 #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL) |
35 #include <stdio.h> | 38 #include <stdio.h> |
36 #endif | 39 #endif |
37 | 40 |
38 // ***************************************************************************** | 41 // ***************************************************************************** |
39 // class DateFormat | 42 // class DateFormat |
40 // ***************************************************************************** | 43 // ***************************************************************************** |
41 | 44 |
42 U_NAMESPACE_BEGIN | 45 U_NAMESPACE_BEGIN |
43 | 46 |
| 47 class U_I18N_API DateFmtBestPattern : public SharedObject { |
| 48 public: |
| 49 UnicodeString fPattern; |
| 50 |
| 51 DateFmtBestPattern(const UnicodeString &pattern) |
| 52 : fPattern(pattern) { } |
| 53 ~DateFmtBestPattern(); |
| 54 }; |
| 55 |
| 56 DateFmtBestPattern::~DateFmtBestPattern() { |
| 57 } |
| 58 |
| 59 template<> U_I18N_API |
| 60 const DateFmtBestPattern *LocaleCacheKey<DateFmtBestPattern>::createObject( |
| 61 const void * /*creationContext*/, UErrorCode &status) const { |
| 62 status = U_UNSUPPORTED_ERROR; |
| 63 return NULL; |
| 64 } |
| 65 |
| 66 class U_I18N_API DateFmtBestPatternKey : public LocaleCacheKey<DateFmtBestPatter
n> { |
| 67 private: |
| 68 UnicodeString fSkeleton; |
| 69 public: |
| 70 DateFmtBestPatternKey( |
| 71 const Locale &loc, |
| 72 const UnicodeString &skeleton, |
| 73 UErrorCode &status) |
| 74 : LocaleCacheKey<DateFmtBestPattern>(loc), |
| 75 fSkeleton(DateTimePatternGenerator::staticGetSkeleton(skeleton, st
atus)) { } |
| 76 DateFmtBestPatternKey(const DateFmtBestPatternKey &other) : |
| 77 LocaleCacheKey<DateFmtBestPattern>(other), |
| 78 fSkeleton(other.fSkeleton) { } |
| 79 virtual ~DateFmtBestPatternKey(); |
| 80 virtual int32_t hashCode() const { |
| 81 return 37 * LocaleCacheKey<DateFmtBestPattern>::hashCode() + fSkeleton.h
ashCode(); |
| 82 } |
| 83 virtual UBool operator==(const CacheKeyBase &other) const { |
| 84 // reflexive |
| 85 if (this == &other) { |
| 86 return TRUE; |
| 87 } |
| 88 if (!LocaleCacheKey<DateFmtBestPattern>::operator==(other)) { |
| 89 return FALSE; |
| 90 } |
| 91 // We know that this and other are of same class if we get this far. |
| 92 const DateFmtBestPatternKey &realOther = |
| 93 static_cast<const DateFmtBestPatternKey &>(other); |
| 94 return (realOther.fSkeleton == fSkeleton); |
| 95 } |
| 96 virtual CacheKeyBase *clone() const { |
| 97 return new DateFmtBestPatternKey(*this); |
| 98 } |
| 99 virtual const DateFmtBestPattern *createObject( |
| 100 const void * /*unused*/, UErrorCode &status) const { |
| 101 LocalPointer<DateTimePatternGenerator> dtpg( |
| 102 DateTimePatternGenerator::createInstance(fLoc, status)); |
| 103 if (U_FAILURE(status)) { |
| 104 return NULL; |
| 105 } |
| 106 |
| 107 LocalPointer<DateFmtBestPattern> pattern( |
| 108 new DateFmtBestPattern( |
| 109 dtpg->getBestPattern(fSkeleton, status)), |
| 110 status); |
| 111 if (U_FAILURE(status)) { |
| 112 return NULL; |
| 113 } |
| 114 DateFmtBestPattern *result = pattern.orphan(); |
| 115 result->addRef(); |
| 116 return result; |
| 117 } |
| 118 }; |
| 119 |
| 120 DateFmtBestPatternKey::~DateFmtBestPatternKey() { } |
| 121 |
| 122 |
44 DateFormat::DateFormat() | 123 DateFormat::DateFormat() |
45 : fCalendar(0), | 124 : fCalendar(0), |
46 fNumberFormat(0), | 125 fNumberFormat(0), |
47 fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE) | 126 fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
48 { | 127 { |
49 } | 128 } |
50 | 129 |
51 //---------------------------------------------------------------------- | 130 //---------------------------------------------------------------------- |
52 | 131 |
53 DateFormat::DateFormat(const DateFormat& other) | 132 DateFormat::DateFormat(const DateFormat& other) |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 { | 381 { |
303 result.setDate(parse(source, pos)); | 382 result.setDate(parse(source, pos)); |
304 } | 383 } |
305 | 384 |
306 //---------------------------------------------------------------------- | 385 //---------------------------------------------------------------------- |
307 | 386 |
308 DateFormat* U_EXPORT2 | 387 DateFormat* U_EXPORT2 |
309 DateFormat::createTimeInstance(DateFormat::EStyle style, | 388 DateFormat::createTimeInstance(DateFormat::EStyle style, |
310 const Locale& aLocale) | 389 const Locale& aLocale) |
311 { | 390 { |
312 return create(style, kNone, aLocale); | 391 return createDateTimeInstance(kNone, style, aLocale); |
313 } | 392 } |
314 | 393 |
315 //---------------------------------------------------------------------- | 394 //---------------------------------------------------------------------- |
316 | 395 |
317 DateFormat* U_EXPORT2 | 396 DateFormat* U_EXPORT2 |
318 DateFormat::createDateInstance(DateFormat::EStyle style, | 397 DateFormat::createDateInstance(DateFormat::EStyle style, |
319 const Locale& aLocale) | 398 const Locale& aLocale) |
320 { | 399 { |
321 // +4 to set the correct index for getting data out of | 400 return createDateTimeInstance(style, kNone, aLocale); |
322 // LocaleElements. | |
323 if(style != kNone) | |
324 { | |
325 style = (EStyle) (style + kDateOffset); | |
326 } | |
327 return create(kNone, (EStyle) (style), aLocale); | |
328 } | 401 } |
329 | 402 |
330 //---------------------------------------------------------------------- | 403 //---------------------------------------------------------------------- |
331 | 404 |
332 DateFormat* U_EXPORT2 | 405 DateFormat* U_EXPORT2 |
333 DateFormat::createDateTimeInstance(EStyle dateStyle, | 406 DateFormat::createDateTimeInstance(EStyle dateStyle, |
334 EStyle timeStyle, | 407 EStyle timeStyle, |
335 const Locale& aLocale) | 408 const Locale& aLocale) |
336 { | 409 { |
337 if(dateStyle != kNone) | 410 if(dateStyle != kNone) |
338 { | 411 { |
339 dateStyle = (EStyle) (dateStyle + kDateOffset); | 412 dateStyle = (EStyle) (dateStyle + kDateOffset); |
340 } | 413 } |
341 return create(timeStyle, dateStyle, aLocale); | 414 return create(timeStyle, dateStyle, aLocale); |
342 } | 415 } |
343 | 416 |
344 //---------------------------------------------------------------------- | 417 //---------------------------------------------------------------------- |
345 | 418 |
346 DateFormat* U_EXPORT2 | 419 DateFormat* U_EXPORT2 |
347 DateFormat::createInstance() | 420 DateFormat::createInstance() |
348 { | 421 { |
349 return create(kShort, (EStyle) (kShort + kDateOffset), Locale::getDefault())
; | 422 return createDateTimeInstance(kShort, kShort, Locale::getDefault()); |
350 } | 423 } |
351 | 424 |
352 //---------------------------------------------------------------------- | 425 //---------------------------------------------------------------------- |
| 426 |
| 427 UnicodeString U_EXPORT2 |
| 428 DateFormat::getBestPattern( |
| 429 const Locale &locale, |
| 430 const UnicodeString &skeleton, |
| 431 UErrorCode &status) { |
| 432 UnifiedCache *cache = UnifiedCache::getInstance(status); |
| 433 if (U_FAILURE(status)) { |
| 434 return UnicodeString(); |
| 435 } |
| 436 DateFmtBestPatternKey key(locale, skeleton, status); |
| 437 const DateFmtBestPattern *patternPtr = NULL; |
| 438 cache->get(key, patternPtr, status); |
| 439 if (U_FAILURE(status)) { |
| 440 return UnicodeString(); |
| 441 } |
| 442 UnicodeString result(patternPtr->fPattern); |
| 443 patternPtr->removeRef(); |
| 444 return result; |
| 445 } |
| 446 |
| 447 DateFormat* U_EXPORT2 |
| 448 DateFormat::createInstanceForSkeleton( |
| 449 Calendar *calendarToAdopt, |
| 450 const UnicodeString& skeleton, |
| 451 const Locale &locale, |
| 452 UErrorCode &status) { |
| 453 LocalPointer<Calendar> calendar(calendarToAdopt); |
| 454 if (U_FAILURE(status)) { |
| 455 return NULL; |
| 456 } |
| 457 if (calendar.isNull()) { |
| 458 status = U_ILLEGAL_ARGUMENT_ERROR; |
| 459 return NULL; |
| 460 } |
| 461 DateFormat *result = createInstanceForSkeleton(skeleton, locale, status); |
| 462 if (U_FAILURE(status)) { |
| 463 return NULL; |
| 464 } |
| 465 result->adoptCalendar(calendar.orphan()); |
| 466 return result; |
| 467 } |
| 468 |
| 469 DateFormat* U_EXPORT2 |
| 470 DateFormat::createInstanceForSkeleton( |
| 471 const UnicodeString& skeleton, |
| 472 const Locale &locale, |
| 473 UErrorCode &status) { |
| 474 if (U_FAILURE(status)) { |
| 475 return NULL; |
| 476 } |
| 477 LocalPointer<DateFormat> df( |
| 478 new SimpleDateFormat( |
| 479 getBestPattern(locale, skeleton, status), |
| 480 locale, status), |
| 481 status); |
| 482 return U_SUCCESS(status) ? df.orphan() : NULL; |
| 483 } |
| 484 |
| 485 DateFormat* U_EXPORT2 |
| 486 DateFormat::createInstanceForSkeleton( |
| 487 const UnicodeString& skeleton, |
| 488 UErrorCode &status) { |
| 489 return createInstanceForSkeleton( |
| 490 skeleton, Locale::getDefault(), status); |
| 491 } |
| 492 |
| 493 //---------------------------------------------------------------------- |
353 | 494 |
354 DateFormat* U_EXPORT2 | 495 DateFormat* U_EXPORT2 |
355 DateFormat::create(EStyle timeStyle, EStyle dateStyle, const Locale& locale) | 496 DateFormat::create(EStyle timeStyle, EStyle dateStyle, const Locale& locale) |
356 { | 497 { |
357 UErrorCode status = U_ZERO_ERROR; | 498 UErrorCode status = U_ZERO_ERROR; |
358 #if U_PLATFORM_HAS_WIN32_API | 499 #if U_PLATFORM_HAS_WIN32_API |
359 char buffer[8]; | 500 char buffer[8]; |
360 int32_t count = locale.getKeywordValue("compat", buffer, sizeof(buffer), sta
tus); | 501 int32_t count = locale.getKeywordValue("compat", buffer, sizeof(buffer), sta
tus); |
361 | 502 |
362 // if the locale has "@compat=host", create a host-specific DateFormat... | 503 // if the locale has "@compat=host", create a host-specific DateFormat... |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
596 DateFormat::getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &/*
status*/) const { | 737 DateFormat::getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &/*
status*/) const { |
597 | 738 |
598 return fBoolFlags.get(attr); | 739 return fBoolFlags.get(attr); |
599 } | 740 } |
600 | 741 |
601 U_NAMESPACE_END | 742 U_NAMESPACE_END |
602 | 743 |
603 #endif /* #if !UCONFIG_NO_FORMATTING */ | 744 #endif /* #if !UCONFIG_NO_FORMATTING */ |
604 | 745 |
605 //eof | 746 //eof |
OLD | NEW |