1/*
2 *******************************************************************************
3 * Copyright (C) 2003-2013, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
6 *
7 * File TAIWNCAL.CPP
8 *
9 * Modification History:
10 *  05/13/2003    srl     copied from gregocal.cpp
11 *  06/29/2007    srl     copied from buddhcal.cpp
12 *  05/12/2008    jce     modified to use calendar=roc per CLDR
13 *
14 */
15
16#include "unicode/utypes.h"
17
18#if !UCONFIG_NO_FORMATTING
19
20#include "taiwncal.h"
21#include "unicode/gregocal.h"
22#include "umutex.h"
23#include <float.h>
24
25U_NAMESPACE_BEGIN
26
27UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TaiwanCalendar)
28
29static const int32_t kTaiwanEraStart = 1911;  // 1911 (Gregorian)
30
31static const int32_t kGregorianEpoch = 1970;
32
33TaiwanCalendar::TaiwanCalendar(const Locale& aLocale, UErrorCode& success)
34:   GregorianCalendar(aLocale, success)
35{
36    setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
37}
38
39TaiwanCalendar::~TaiwanCalendar()
40{
41}
42
43TaiwanCalendar::TaiwanCalendar(const TaiwanCalendar& source)
44: GregorianCalendar(source)
45{
46}
47
48TaiwanCalendar& TaiwanCalendar::operator= ( const TaiwanCalendar& right)
49{
50    GregorianCalendar::operator=(right);
51    return *this;
52}
53
54Calendar* TaiwanCalendar::clone(void) const
55{
56    return new TaiwanCalendar(*this);
57}
58
59const char *TaiwanCalendar::getType() const
60{
61    return "roc";
62}
63
64int32_t TaiwanCalendar::handleGetExtendedYear()
65{
66    // EXTENDED_YEAR in TaiwanCalendar is a Gregorian year
67    // The default value of EXTENDED_YEAR is 1970 (Minguo 59)
68    int32_t year = kGregorianEpoch;
69
70    if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR
71        && newerField(UCAL_EXTENDED_YEAR, UCAL_ERA) == UCAL_EXTENDED_YEAR) {
72        year = internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch);
73    } else {
74        int32_t era = internalGet(UCAL_ERA, MINGUO);
75        if(era == MINGUO) {
76            year =     internalGet(UCAL_YEAR, 1) + kTaiwanEraStart;
77        } else if(era == BEFORE_MINGUO) {
78            year = 1 - internalGet(UCAL_YEAR, 1) + kTaiwanEraStart;
79        }
80    }
81    return year;
82}
83
84void TaiwanCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)
85{
86    GregorianCalendar::handleComputeFields(julianDay, status);
87    int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kTaiwanEraStart;
88    if(y>0) {
89        internalSet(UCAL_ERA, MINGUO);
90        internalSet(UCAL_YEAR, y);
91    } else {
92        internalSet(UCAL_ERA, BEFORE_MINGUO);
93        internalSet(UCAL_YEAR, 1-y);
94    }
95}
96
97int32_t TaiwanCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
98{
99    if(field == UCAL_ERA) {
100        if(limitType == UCAL_LIMIT_MINIMUM || limitType == UCAL_LIMIT_GREATEST_MINIMUM) {
101            return BEFORE_MINGUO;
102        } else {
103            return MINGUO;
104        }
105    } else {
106        return GregorianCalendar::handleGetLimit(field,limitType);
107    }
108}
109
110#if 0
111void TaiwanCalendar::timeToFields(UDate theTime, UBool quick, UErrorCode& status)
112{
113    //Calendar::timeToFields(theTime, quick, status);
114
115    int32_t era = internalGet(UCAL_ERA);
116    int32_t year = internalGet(UCAL_YEAR);
117
118    if(era == GregorianCalendar::BC) {
119        year = 1-year;
120        era = TaiwanCalendar::MINGUO;
121    } else if(era == GregorianCalendar::AD) {
122        era = TaiwanCalendar::MINGUO;
123    } else {
124        status = U_INTERNAL_PROGRAM_ERROR;
125    }
126
127    year = year - kTaiwanEraStart;
128
129    internalSet(UCAL_ERA, era);
130    internalSet(UCAL_YEAR, year);
131}
132#endif
133
134/**
135 * The system maintains a static default century start date and Year.  They are
136 * initialized the first time they are used.  Once the system default century date
137 * and year are set, they do not change.
138 */
139static UDate           gSystemDefaultCenturyStart       = DBL_MIN;
140static int32_t         gSystemDefaultCenturyStartYear   = -1;
141static icu::UInitOnce  gSystemDefaultCenturyInit        = U_INITONCE_INITIALIZER;
142
143UBool TaiwanCalendar::haveDefaultCentury() const
144{
145    return TRUE;
146}
147
148static void U_CALLCONV initializeSystemDefaultCentury()
149{
150    // initialize systemDefaultCentury and systemDefaultCenturyYear based
151    // on the current time.  They'll be set to 80 years before
152    // the current time.
153    UErrorCode status = U_ZERO_ERROR;
154    TaiwanCalendar calendar(Locale("@calendar=roc"),status);
155    if (U_SUCCESS(status))
156    {
157        calendar.setTime(Calendar::getNow(), status);
158        calendar.add(UCAL_YEAR, -80, status);
159
160        gSystemDefaultCenturyStart = calendar.getTime(status);
161        gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
162    }
163    // We have no recourse upon failure unless we want to propagate the failure
164    // out.
165}
166
167UDate TaiwanCalendar::defaultCenturyStart() const {
168    // lazy-evaluate systemDefaultCenturyStart
169    umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
170    return gSystemDefaultCenturyStart;
171}
172
173int32_t TaiwanCalendar::defaultCenturyStartYear() const {
174    // lazy-evaluate systemDefaultCenturyStartYear
175    umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
176    return gSystemDefaultCenturyStartYear;
177}
178
179U_NAMESPACE_END
180
181#endif
182