| 1 |
// © 2016 and later: Unicode, Inc. and others. |
| 2 |
// License & terms of use: http://www.unicode.org/copyright.html |
| 3 |
/* |
| 4 |
******************************************************************************* |
| 5 |
* Copyright (C) 2003-2013, International Business Machines Corporation and |
| 6 |
* others. All Rights Reserved. |
| 7 |
******************************************************************************* |
| 8 |
* |
| 9 |
* File TAIWNCAL.CPP |
| 10 |
* |
| 11 |
* Modification History: |
| 12 |
* 05/13/2003 srl copied from gregocal.cpp |
| 13 |
* 06/29/2007 srl copied from buddhcal.cpp |
| 14 |
* 05/12/2008 jce modified to use calendar=roc per CLDR |
| 15 |
* |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "unicode/utypes.h" |
| 19 |
|
| 20 |
#if !UCONFIG_NO_FORMATTING |
| 21 |
|
| 22 |
#include "taiwncal.h" |
| 23 |
#include "gregoimp.h" |
| 24 |
#include "unicode/gregocal.h" |
| 25 |
#include "umutex.h" |
| 26 |
#include <float.h> |
| 27 |
|
| 28 |
U_NAMESPACE_BEGIN |
| 29 |
|
| 30 |
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TaiwanCalendar) |
| 31 |
|
| 32 |
static const int32_t kTaiwanEraStart = 1911; // 1911 (Gregorian) |
| 33 |
|
| 34 |
static const int32_t kGregorianEpoch = 1970; |
| 35 |
|
| 36 |
TaiwanCalendar::TaiwanCalendar(const Locale& aLocale, UErrorCode& success) |
| 37 |
: GregorianCalendar(aLocale, success) |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
TaiwanCalendar::~TaiwanCalendar() |
| 42 |
{ |
| 43 |
} |
| 44 |
|
| 45 |
TaiwanCalendar::TaiwanCalendar(const TaiwanCalendar& source) |
| 46 |
: GregorianCalendar(source) |
| 47 |
{ |
| 48 |
} |
| 49 |
|
| 50 |
TaiwanCalendar* TaiwanCalendar::clone() const |
| 51 |
{ |
| 52 |
return new TaiwanCalendar(*this); |
| 53 |
} |
| 54 |
|
| 55 |
const char *TaiwanCalendar::getType() const |
| 56 |
{ |
| 57 |
return "roc"; |
| 58 |
} |
| 59 |
|
| 60 |
int32_t TaiwanCalendar::handleGetExtendedYear(UErrorCode& status) |
| 61 |
{ |
| 62 |
if (U_FAILURE(status)) { |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
// EXTENDED_YEAR in TaiwanCalendar is a Gregorian year |
| 67 |
// The default value of EXTENDED_YEAR is 1970 (Minguo 59) |
| 68 |
if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR |
| 69 |
&& newerField(UCAL_EXTENDED_YEAR, UCAL_ERA) == UCAL_EXTENDED_YEAR) { |
| 70 |
return internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch); |
| 71 |
} |
| 72 |
int32_t era = internalGet(UCAL_ERA, MINGUO); |
| 73 |
int32_t year = internalGet(UCAL_YEAR, 1); |
| 74 |
switch (era) { |
| 75 |
case MINGUO: |
| 76 |
if (uprv_add32_overflow(year, kTaiwanEraStart, &year)) { |
| 77 |
status = U_ILLEGAL_ARGUMENT_ERROR; |
| 78 |
return 0; |
| 79 |
} |
| 80 |
return year; |
| 81 |
case BEFORE_MINGUO: |
| 82 |
if (uprv_add32_overflow(1 + kTaiwanEraStart, -year, &year)) { |
| 83 |
status = U_ILLEGAL_ARGUMENT_ERROR; |
| 84 |
return 0; |
| 85 |
} |
| 86 |
return year; |
| 87 |
default: |
| 88 |
status = U_ILLEGAL_ARGUMENT_ERROR; |
| 89 |
return 0; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void TaiwanCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) |
| 94 |
{ |
| 95 |
GregorianCalendar::handleComputeFields(julianDay, status); |
| 96 |
int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kTaiwanEraStart; |
| 97 |
if(y>0) { |
| 98 |
internalSet(UCAL_ERA, MINGUO); |
| 99 |
internalSet(UCAL_YEAR, y); |
| 100 |
} else { |
| 101 |
internalSet(UCAL_ERA, BEFORE_MINGUO); |
| 102 |
internalSet(UCAL_YEAR, 1-y); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
int32_t TaiwanCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const |
| 107 |
{ |
| 108 |
if(field != UCAL_ERA) { |
| 109 |
return GregorianCalendar::handleGetLimit(field,limitType); |
| 110 |
} |
| 111 |
if (limitType == UCAL_LIMIT_MINIMUM || limitType == UCAL_LIMIT_GREATEST_MINIMUM) { |
| 112 |
return BEFORE_MINGUO; |
| 113 |
} |
| 114 |
return MINGUO; |
| 115 |
} |
| 116 |
|
| 117 |
IMPL_SYSTEM_DEFAULT_CENTURY(TaiwanCalendar, "@calendar=roc") |
| 118 |
|
| 119 |
U_NAMESPACE_END |
| 120 |
|
| 121 |
#endif |