| OLD | NEW |
| 1 /* | 1 /* |
| 2 ******************************************************************************* | 2 ******************************************************************************* |
| 3 * Copyright (C) 1997-2013, International Business Machines Corporation and * | 3 * Copyright (C) 1997-2014, 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 "reldtfmt.h" | 29 #include "reldtfmt.h" |
| 29 | 30 |
| 30 #include "cstring.h" | 31 #include "cstring.h" |
| 31 #include "windtfmt.h" | 32 #include "windtfmt.h" |
| 32 | 33 |
| 33 #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL) | 34 #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL) |
| 34 #include <stdio.h> | 35 #include <stdio.h> |
| 35 #endif | 36 #endif |
| 36 | 37 |
| 37 // ***************************************************************************** | 38 // ***************************************************************************** |
| 38 // class DateFormat | 39 // class DateFormat |
| 39 // ***************************************************************************** | 40 // ***************************************************************************** |
| 40 | 41 |
| 41 U_NAMESPACE_BEGIN | 42 U_NAMESPACE_BEGIN |
| 42 | 43 |
| 43 DateFormat::DateFormat() | 44 DateFormat::DateFormat() |
| 44 : fCalendar(0), | 45 : fCalendar(0), |
| 45 fNumberFormat(0) | 46 fNumberFormat(0), |
| 47 fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
| 46 { | 48 { |
| 47 } | 49 } |
| 48 | 50 |
| 49 //---------------------------------------------------------------------- | 51 //---------------------------------------------------------------------- |
| 50 | 52 |
| 51 DateFormat::DateFormat(const DateFormat& other) | 53 DateFormat::DateFormat(const DateFormat& other) |
| 52 : Format(other), | 54 : Format(other), |
| 53 fCalendar(0), | 55 fCalendar(0), |
| 54 fNumberFormat(0) | 56 fNumberFormat(0), |
| 57 fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE) |
| 55 { | 58 { |
| 56 *this = other; | 59 *this = other; |
| 57 } | 60 } |
| 58 | 61 |
| 59 //---------------------------------------------------------------------- | 62 //---------------------------------------------------------------------- |
| 60 | 63 |
| 61 DateFormat& DateFormat::operator=(const DateFormat& other) | 64 DateFormat& DateFormat::operator=(const DateFormat& other) |
| 62 { | 65 { |
| 63 if (this != &other) | 66 if (this != &other) |
| 64 { | 67 { |
| 65 delete fCalendar; | 68 delete fCalendar; |
| 66 delete fNumberFormat; | 69 delete fNumberFormat; |
| 67 if(other.fCalendar) { | 70 if(other.fCalendar) { |
| 68 fCalendar = other.fCalendar->clone(); | 71 fCalendar = other.fCalendar->clone(); |
| 69 } else { | 72 } else { |
| 70 fCalendar = NULL; | 73 fCalendar = NULL; |
| 71 } | 74 } |
| 72 if(other.fNumberFormat) { | 75 if(other.fNumberFormat) { |
| 73 fNumberFormat = (NumberFormat*)other.fNumberFormat->clone(); | 76 fNumberFormat = (NumberFormat*)other.fNumberFormat->clone(); |
| 74 } else { | 77 } else { |
| 75 fNumberFormat = NULL; | 78 fNumberFormat = NULL; |
| 76 } | 79 } |
| 77 fBoolFlags = other.fBoolFlags; | 80 fBoolFlags = other.fBoolFlags; |
| 81 fCapitalizationContext = other.fCapitalizationContext; |
| 78 } | 82 } |
| 79 return *this; | 83 return *this; |
| 80 } | 84 } |
| 81 | 85 |
| 82 //---------------------------------------------------------------------- | 86 //---------------------------------------------------------------------- |
| 83 | 87 |
| 84 DateFormat::~DateFormat() | 88 DateFormat::~DateFormat() |
| 85 { | 89 { |
| 86 delete fCalendar; | 90 delete fCalendar; |
| 87 delete fNumberFormat; | 91 delete fNumberFormat; |
| 88 } | 92 } |
| 89 | 93 |
| 90 //---------------------------------------------------------------------- | 94 //---------------------------------------------------------------------- |
| 91 | 95 |
| 92 UBool | 96 UBool |
| 93 DateFormat::operator==(const Format& other) const | 97 DateFormat::operator==(const Format& other) const |
| 94 { | 98 { |
| 95 // This protected comparison operator should only be called by subclasses | 99 // This protected comparison operator should only be called by subclasses |
| 96 // which have confirmed that the other object being compared against is | 100 // which have confirmed that the other object being compared against is |
| 97 // an instance of a sublcass of DateFormat. THIS IS IMPORTANT. | 101 // an instance of a sublcass of DateFormat. THIS IS IMPORTANT. |
| 98 | 102 |
| 99 // Format::operator== guarantees that this cast is safe | 103 // Format::operator== guarantees that this cast is safe |
| 100 DateFormat* fmt = (DateFormat*)&other; | 104 DateFormat* fmt = (DateFormat*)&other; |
| 101 | 105 |
| 102 return (this == fmt) || | 106 return (this == fmt) || |
| 103 (Format::operator==(other) && | 107 (Format::operator==(other) && |
| 104 fCalendar&&(fCalendar->isEquivalentTo(*fmt->fCalendar)) && | 108 fCalendar&&(fCalendar->isEquivalentTo(*fmt->fCalendar)) && |
| 105 (fNumberFormat && *fNumberFormat == *fmt->fNumberFormat)); | 109 (fNumberFormat && *fNumberFormat == *fmt->fNumberFormat) && |
| 110 (fCapitalizationContext == fmt->fCapitalizationContext) ); |
| 106 } | 111 } |
| 107 | 112 |
| 108 //---------------------------------------------------------------------- | 113 //---------------------------------------------------------------------- |
| 109 | 114 |
| 110 UnicodeString& | 115 UnicodeString& |
| 111 DateFormat::format(const Formattable& obj, | 116 DateFormat::format(const Formattable& obj, |
| 112 UnicodeString& appendTo, | 117 UnicodeString& appendTo, |
| 113 FieldPosition& fieldPosition, | 118 FieldPosition& fieldPosition, |
| 114 UErrorCode& status) const | 119 UErrorCode& status) const |
| 115 { | 120 { |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 } | 496 } |
| 492 | 497 |
| 493 //---------------------------------------------------------------------- | 498 //---------------------------------------------------------------------- |
| 494 | 499 |
| 495 void | 500 void |
| 496 DateFormat::setLenient(UBool lenient) | 501 DateFormat::setLenient(UBool lenient) |
| 497 { | 502 { |
| 498 if (fCalendar != NULL) { | 503 if (fCalendar != NULL) { |
| 499 fCalendar->setLenient(lenient); | 504 fCalendar->setLenient(lenient); |
| 500 } | 505 } |
| 506 UErrorCode status = U_ZERO_ERROR; |
| 507 setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, lenient, status); |
| 508 setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, lenient, status); |
| 501 } | 509 } |
| 502 | 510 |
| 503 //---------------------------------------------------------------------- | 511 //---------------------------------------------------------------------- |
| 504 | 512 |
| 505 UBool | 513 UBool |
| 506 DateFormat::isLenient() const | 514 DateFormat::isLenient() const |
| 507 { | 515 { |
| 516 UBool lenient = TRUE; |
| 517 if (fCalendar != NULL) { |
| 518 lenient = fCalendar->isLenient(); |
| 519 } |
| 520 UErrorCode status = U_ZERO_ERROR; |
| 521 return lenient |
| 522 && getBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, status) |
| 523 && getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, status); |
| 524 } |
| 525 |
| 526 void |
| 527 DateFormat::setCalendarLenient(UBool lenient) |
| 528 { |
| 529 if (fCalendar != NULL) { |
| 530 fCalendar->setLenient(lenient); |
| 531 } |
| 532 } |
| 533 |
| 534 //---------------------------------------------------------------------- |
| 535 |
| 536 UBool |
| 537 DateFormat::isCalendarLenient() const |
| 538 { |
| 508 if (fCalendar != NULL) { | 539 if (fCalendar != NULL) { |
| 509 return fCalendar->isLenient(); | 540 return fCalendar->isLenient(); |
| 510 } | 541 } |
| 511 // fCalendar is rarely null | 542 // fCalendar is rarely null |
| 512 return FALSE; | 543 return FALSE; |
| 513 } | 544 } |
| 514 | 545 |
| 546 |
| 515 //---------------------------------------------------------------------- | 547 //---------------------------------------------------------------------- |
| 516 | 548 |
| 549 |
| 550 void DateFormat::setContext(UDisplayContext value, UErrorCode& status) |
| 551 { |
| 552 if (U_FAILURE(status)) |
| 553 return; |
| 554 if ( (UDisplayContextType)((uint32_t)value >> 8) == UDISPCTX_TYPE_CAPITALIZA
TION ) { |
| 555 fCapitalizationContext = value; |
| 556 } else { |
| 557 status = U_ILLEGAL_ARGUMENT_ERROR; |
| 558 } |
| 559 } |
| 560 |
| 561 |
| 562 //---------------------------------------------------------------------- |
| 563 |
| 564 |
| 565 UDisplayContext DateFormat::getContext(UDisplayContextType type, UErrorCode& sta
tus) const |
| 566 { |
| 567 if (U_FAILURE(status)) |
| 568 return (UDisplayContext)0; |
| 569 if (type != UDISPCTX_TYPE_CAPITALIZATION) { |
| 570 status = U_ILLEGAL_ARGUMENT_ERROR; |
| 571 return (UDisplayContext)0; |
| 572 } |
| 573 return fCapitalizationContext; |
| 574 } |
| 575 |
| 576 |
| 577 //---------------------------------------------------------------------- |
| 578 |
| 579 |
| 517 DateFormat& | 580 DateFormat& |
| 518 DateFormat::setBooleanAttribute(UDateFormatBooleanAttribute attr, | 581 DateFormat::setBooleanAttribute(UDateFormatBooleanAttribute attr, |
| 519 UBool ne
wValue, | 582 UBool ne
wValue, |
| 520 UErrorCo
de &status) { | 583 UErrorCo
de &status) { |
| 521 if(!fBoolFlags.isValidValue(newValue)) { | 584 if(!fBoolFlags.isValidValue(newValue)) { |
| 522 status = U_ILLEGAL_ARGUMENT_ERROR; | 585 status = U_ILLEGAL_ARGUMENT_ERROR; |
| 523 } else { | 586 } else { |
| 524 fBoolFlags.set(attr, newValue); | 587 fBoolFlags.set(attr, newValue); |
| 525 } | 588 } |
| 526 | 589 |
| 527 return *this; | 590 return *this; |
| 528 } | 591 } |
| 529 | 592 |
| 530 //---------------------------------------------------------------------- | 593 //---------------------------------------------------------------------- |
| 531 | 594 |
| 532 UBool | 595 UBool |
| 533 DateFormat::getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &/*
status*/) const { | 596 DateFormat::getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &/*
status*/) const { |
| 534 | 597 |
| 535 return fBoolFlags.get(attr); | 598 return fBoolFlags.get(attr); |
| 536 } | 599 } |
| 537 | 600 |
| 538 U_NAMESPACE_END | 601 U_NAMESPACE_END |
| 539 | 602 |
| 540 #endif /* #if !UCONFIG_NO_FORMATTING */ | 603 #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 541 | 604 |
| 542 //eof | 605 //eof |
| OLD | NEW |