taiwncal.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "unicode/utypes.h"
  18. #if !UCONFIG_NO_FORMATTING
  19. #include "taiwncal.h"
  20. #include "unicode/gregocal.h"
  21. #include "umutex.h"
  22. #include <float.h>
  23. U_NAMESPACE_BEGIN
  24. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TaiwanCalendar)
  25. static const int32_t kTaiwanEraStart = 1911; // 1911 (Gregorian)
  26. static const int32_t kGregorianEpoch = 1970;
  27. TaiwanCalendar::TaiwanCalendar(const Locale& aLocale, UErrorCode& success)
  28. : GregorianCalendar(aLocale, success)
  29. {
  30. setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
  31. }
  32. TaiwanCalendar::~TaiwanCalendar()
  33. {
  34. }
  35. TaiwanCalendar::TaiwanCalendar(const TaiwanCalendar& source)
  36. : GregorianCalendar(source)
  37. {
  38. }
  39. TaiwanCalendar& TaiwanCalendar::operator= ( const TaiwanCalendar& right)
  40. {
  41. GregorianCalendar::operator=(right);
  42. return *this;
  43. }
  44. TaiwanCalendar* TaiwanCalendar::clone() const
  45. {
  46. return new TaiwanCalendar(*this);
  47. }
  48. const char *TaiwanCalendar::getType() const
  49. {
  50. return "roc";
  51. }
  52. int32_t TaiwanCalendar::handleGetExtendedYear()
  53. {
  54. // EXTENDED_YEAR in TaiwanCalendar is a Gregorian year
  55. // The default value of EXTENDED_YEAR is 1970 (Minguo 59)
  56. int32_t year = kGregorianEpoch;
  57. if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR
  58. && newerField(UCAL_EXTENDED_YEAR, UCAL_ERA) == UCAL_EXTENDED_YEAR) {
  59. year = internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch);
  60. } else {
  61. int32_t era = internalGet(UCAL_ERA, MINGUO);
  62. if(era == MINGUO) {
  63. year = internalGet(UCAL_YEAR, 1) + kTaiwanEraStart;
  64. } else if(era == BEFORE_MINGUO) {
  65. year = 1 - internalGet(UCAL_YEAR, 1) + kTaiwanEraStart;
  66. }
  67. }
  68. return year;
  69. }
  70. void TaiwanCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)
  71. {
  72. GregorianCalendar::handleComputeFields(julianDay, status);
  73. int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kTaiwanEraStart;
  74. if(y>0) {
  75. internalSet(UCAL_ERA, MINGUO);
  76. internalSet(UCAL_YEAR, y);
  77. } else {
  78. internalSet(UCAL_ERA, BEFORE_MINGUO);
  79. internalSet(UCAL_YEAR, 1-y);
  80. }
  81. }
  82. int32_t TaiwanCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
  83. {
  84. if(field == UCAL_ERA) {
  85. if(limitType == UCAL_LIMIT_MINIMUM || limitType == UCAL_LIMIT_GREATEST_MINIMUM) {
  86. return BEFORE_MINGUO;
  87. } else {
  88. return MINGUO;
  89. }
  90. } else {
  91. return GregorianCalendar::handleGetLimit(field,limitType);
  92. }
  93. }
  94. #if 0
  95. void TaiwanCalendar::timeToFields(UDate theTime, UBool quick, UErrorCode& status)
  96. {
  97. //Calendar::timeToFields(theTime, quick, status);
  98. int32_t era = internalGet(UCAL_ERA);
  99. int32_t year = internalGet(UCAL_YEAR);
  100. if(era == GregorianCalendar::BC) {
  101. year = 1-year;
  102. era = TaiwanCalendar::MINGUO;
  103. } else if(era == GregorianCalendar::AD) {
  104. era = TaiwanCalendar::MINGUO;
  105. } else {
  106. status = U_INTERNAL_PROGRAM_ERROR;
  107. }
  108. year = year - kTaiwanEraStart;
  109. internalSet(UCAL_ERA, era);
  110. internalSet(UCAL_YEAR, year);
  111. }
  112. #endif
  113. /**
  114. * The system maintains a static default century start date and Year. They are
  115. * initialized the first time they are used. Once the system default century date
  116. * and year are set, they do not change.
  117. */
  118. static UDate gSystemDefaultCenturyStart = DBL_MIN;
  119. static int32_t gSystemDefaultCenturyStartYear = -1;
  120. static icu::UInitOnce gSystemDefaultCenturyInit {};
  121. UBool TaiwanCalendar::haveDefaultCentury() const
  122. {
  123. return true;
  124. }
  125. static void U_CALLCONV initializeSystemDefaultCentury()
  126. {
  127. // initialize systemDefaultCentury and systemDefaultCenturyYear based
  128. // on the current time. They'll be set to 80 years before
  129. // the current time.
  130. UErrorCode status = U_ZERO_ERROR;
  131. TaiwanCalendar calendar(Locale("@calendar=roc"),status);
  132. if (U_SUCCESS(status))
  133. {
  134. calendar.setTime(Calendar::getNow(), status);
  135. calendar.add(UCAL_YEAR, -80, status);
  136. gSystemDefaultCenturyStart = calendar.getTime(status);
  137. gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
  138. }
  139. // We have no recourse upon failure unless we want to propagate the failure
  140. // out.
  141. }
  142. UDate TaiwanCalendar::defaultCenturyStart() const {
  143. // lazy-evaluate systemDefaultCenturyStart
  144. umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
  145. return gSystemDefaultCenturyStart;
  146. }
  147. int32_t TaiwanCalendar::defaultCenturyStartYear() const {
  148. // lazy-evaluate systemDefaultCenturyStartYear
  149. umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
  150. return gSystemDefaultCenturyStartYear;
  151. }
  152. U_NAMESPACE_END
  153. #endif