dangical.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. * Copyright (C) 2013, International Business Machines Corporation
  6. * and others. All Rights Reserved.
  7. ******************************************************************************
  8. *
  9. * File DANGICAL.CPP
  10. *****************************************************************************
  11. */
  12. #include "chnsecal.h"
  13. #include "dangical.h"
  14. #if !UCONFIG_NO_FORMATTING
  15. #include "gregoimp.h" // Math
  16. #include "uassert.h"
  17. #include "ucln_in.h"
  18. #include "umutex.h"
  19. #include "unicode/rbtz.h"
  20. #include "unicode/tzrule.h"
  21. // --- The cache --
  22. static icu::TimeZone *gDangiCalendarZoneAstroCalc = nullptr;
  23. static icu::UInitOnce gDangiCalendarInitOnce {};
  24. /**
  25. * The start year of the Korean traditional calendar (Dan-gi) is the inaugural
  26. * year of Dan-gun (BC 2333).
  27. */
  28. static const int32_t DANGI_EPOCH_YEAR = -2332; // Gregorian year
  29. U_CDECL_BEGIN
  30. static UBool calendar_dangi_cleanup() {
  31. if (gDangiCalendarZoneAstroCalc) {
  32. delete gDangiCalendarZoneAstroCalc;
  33. gDangiCalendarZoneAstroCalc = nullptr;
  34. }
  35. gDangiCalendarInitOnce.reset();
  36. return true;
  37. }
  38. U_CDECL_END
  39. U_NAMESPACE_BEGIN
  40. // Implementation of the DangiCalendar class
  41. //-------------------------------------------------------------------------
  42. // Constructors...
  43. //-------------------------------------------------------------------------
  44. DangiCalendar::DangiCalendar(const Locale& aLocale, UErrorCode& success)
  45. : ChineseCalendar(aLocale, DANGI_EPOCH_YEAR, getDangiCalZoneAstroCalc(success), success)
  46. {
  47. }
  48. DangiCalendar::DangiCalendar (const DangiCalendar& other)
  49. : ChineseCalendar(other)
  50. {
  51. }
  52. DangiCalendar::~DangiCalendar()
  53. {
  54. }
  55. DangiCalendar*
  56. DangiCalendar::clone() const
  57. {
  58. return new DangiCalendar(*this);
  59. }
  60. const char *DangiCalendar::getType() const {
  61. return "dangi";
  62. }
  63. /**
  64. * The time zone used for performing astronomical computations for
  65. * Dangi calendar. In Korea various timezones have been used historically
  66. * (cf. http://www.math.snu.ac.kr/~kye/others/lunar.html):
  67. *
  68. * - 1908/04/01: GMT+8
  69. * 1908/04/01 - 1911/12/31: GMT+8.5
  70. * 1912/01/01 - 1954/03/20: GMT+9
  71. * 1954/03/21 - 1961/08/09: GMT+8.5
  72. * 1961/08/10 - : GMT+9
  73. *
  74. * Note that, in 1908-1911, the government did not apply the timezone change
  75. * but used GMT+8. In addition, 1954-1961's timezone change does not affect
  76. * the lunar date calculation. Therefore, the following simpler rule works:
  77. *
  78. * -1911: GMT+8
  79. * 1912-: GMT+9
  80. *
  81. * Unfortunately, our astronomer's approximation doesn't agree with the
  82. * references (http://www.math.snu.ac.kr/~kye/others/lunar.html and
  83. * http://astro.kasi.re.kr/Life/ConvertSolarLunarForm.aspx?MenuID=115)
  84. * in 1897/7/30. So the following ad hoc fix is used here:
  85. *
  86. * -1896: GMT+8
  87. * 1897: GMT+7
  88. * 1898-1911: GMT+8
  89. * 1912- : GMT+9
  90. */
  91. static void U_CALLCONV initDangiCalZoneAstroCalc(UErrorCode &status) {
  92. U_ASSERT(gDangiCalendarZoneAstroCalc == nullptr);
  93. const UDate millis1897[] = { (UDate)((1897 - 1970) * 365 * kOneDay) }; // some days of error is not a problem here
  94. const UDate millis1898[] = { (UDate)((1898 - 1970) * 365 * kOneDay) }; // some days of error is not a problem here
  95. const UDate millis1912[] = { (UDate)((1912 - 1970) * 365 * kOneDay) }; // this doesn't create an issue for 1911/12/20
  96. LocalPointer<InitialTimeZoneRule> initialTimeZone(new InitialTimeZoneRule(
  97. UnicodeString(u"GMT+8"), 8*kOneHour, 0), status);
  98. LocalPointer<TimeZoneRule> rule1897(new TimeArrayTimeZoneRule(
  99. UnicodeString(u"Korean 1897"), 7*kOneHour, 0, millis1897, 1, DateTimeRule::STANDARD_TIME), status);
  100. LocalPointer<TimeZoneRule> rule1898to1911(new TimeArrayTimeZoneRule(
  101. UnicodeString(u"Korean 1898-1911"), 8*kOneHour, 0, millis1898, 1, DateTimeRule::STANDARD_TIME), status);
  102. LocalPointer<TimeZoneRule> ruleFrom1912(new TimeArrayTimeZoneRule(
  103. UnicodeString(u"Korean 1912-"), 9*kOneHour, 0, millis1912, 1, DateTimeRule::STANDARD_TIME), status);
  104. LocalPointer<RuleBasedTimeZone> dangiCalZoneAstroCalc(new RuleBasedTimeZone(
  105. UnicodeString(u"KOREA_ZONE"), initialTimeZone.orphan()), status); // adopts initialTimeZone
  106. if (U_FAILURE(status)) {
  107. return;
  108. }
  109. dangiCalZoneAstroCalc->addTransitionRule(rule1897.orphan(), status); // adopts rule1897
  110. dangiCalZoneAstroCalc->addTransitionRule(rule1898to1911.orphan(), status);
  111. dangiCalZoneAstroCalc->addTransitionRule(ruleFrom1912.orphan(), status);
  112. dangiCalZoneAstroCalc->complete(status);
  113. if (U_SUCCESS(status)) {
  114. gDangiCalendarZoneAstroCalc = dangiCalZoneAstroCalc.orphan();
  115. }
  116. ucln_i18n_registerCleanup(UCLN_I18N_DANGI_CALENDAR, calendar_dangi_cleanup);
  117. }
  118. const TimeZone* DangiCalendar::getDangiCalZoneAstroCalc(UErrorCode &status) const {
  119. umtx_initOnce(gDangiCalendarInitOnce, &initDangiCalZoneAstroCalc, status);
  120. return gDangiCalendarZoneAstroCalc;
  121. }
  122. constexpr uint32_t kDangiRelatedYearDiff = -2333;
  123. int32_t DangiCalendar::getRelatedYear(UErrorCode &status) const
  124. {
  125. int32_t year = get(UCAL_EXTENDED_YEAR, status);
  126. if (U_FAILURE(status)) {
  127. return 0;
  128. }
  129. return year + kDangiRelatedYearDiff;
  130. }
  131. void DangiCalendar::setRelatedYear(int32_t year)
  132. {
  133. // set extended year
  134. set(UCAL_EXTENDED_YEAR, year - kDangiRelatedYearDiff);
  135. }
  136. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DangiCalendar)
  137. U_NAMESPACE_END
  138. #endif