164339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert// Copyright (C) 2016 and later: Unicode, Inc. and others.
264339d36f8bd4db5025fe2988eda22b491a9219cFredrik Roubert// License & terms of use: http://www.unicode.org/copyright.html
3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*
4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ******************************************************************************
5f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius * Copyright (C) 2007-2014, International Business Machines Corporation
685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * and others. All Rights Reserved.
7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ******************************************************************************
8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * File CHNSECAL.CPP
10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *
1185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Modification History:
1285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *
1385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *   Date        Name        Description
1485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *   9/18/2007  ajmacher         ported from java ChineseCalendar
1585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *****************************************************************************
16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */
1785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "chnsecal.h"
19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
2085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#if !UCONFIG_NO_FORMATTING
2185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
2285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include "umutex.h"
2385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include <float.h>
2485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include "gregoimp.h" // Math
2585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include "astro.h" // CalendarAstronomer
268393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#include "unicode/simpletz.h"
2785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include "uhash.h"
2885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include "ucln_in.h"
2985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
3085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Debugging
3185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#ifdef U_DEBUG_CHNSECAL
3285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# include <stdio.h>
3385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# include <stdarg.h>
3485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic void debug_chnsecal_loc(const char *f, int32_t l)
3585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
3685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    fprintf(stderr, "%s:%d: ", f, l);
3785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
3885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
3985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic void debug_chnsecal_msg(const char *pat, ...)
4085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
4185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    va_list ap;
4285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    va_start(ap, pat);
4385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    vfprintf(stderr, pat, ap);
4485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    fflush(stderr);
4585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
4685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// must use double parens, i.e.:  U_DEBUG_CHNSECAL_MSG(("four is: %d",4));
4785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define U_DEBUG_CHNSECAL_MSG(x) {debug_chnsecal_loc(__FILE__,__LINE__);debug_chnsecal_msg x;}
4885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#else
4985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define U_DEBUG_CHNSECAL_MSG(x)
5085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif
5185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
5285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
5385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// --- The cache --
54f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusstatic UMutex astroLock = U_MUTEX_INITIALIZER;  // Protects access to gChineseCalendarAstro.
55103e9ffba2cba345d0078eb8b8db33249f81840aCraig Corneliusstatic icu::CalendarAstronomer *gChineseCalendarAstro = NULL;
56f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
57f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius// Lazy Creation & Access synchronized by class CalendarCache with a mutex.
58103e9ffba2cba345d0078eb8b8db33249f81840aCraig Corneliusstatic icu::CalendarCache *gChineseCalendarWinterSolsticeCache = NULL;
59103e9ffba2cba345d0078eb8b8db33249f81840aCraig Corneliusstatic icu::CalendarCache *gChineseCalendarNewYearCache = NULL;
60f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
618393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Corneliusstatic icu::TimeZone *gChineseCalendarZoneAstroCalc = NULL;
6259d709d503bab6e2b61931737e662dd293b40578ccorneliusstatic icu::UInitOnce gChineseCalendarZoneAstroCalcInitOnce = U_INITONCE_INITIALIZER;
6385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
6485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
6585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * The start year of the Chinese calendar, the 61st year of the reign
6685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * of Huang Di.  Some sources use the first year of his reign,
6785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * resulting in EXTENDED_YEAR values 60 years greater and ERA (cycle)
6885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * values one greater.
6985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
7085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic const int32_t CHINESE_EPOCH_YEAR = -2636; // Gregorian year
7185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
7285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
7385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * The offset from GMT in milliseconds at which we perform astronomical
7485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * computations.  Some sources use a different historically accurate
7585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * offset of GMT+7:45:40 for years before 1929; we do not do this.
7685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
778393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Corneliusstatic const int32_t CHINA_OFFSET = 8 * kOneHour;
7885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
7985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
8085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Value to be added or subtracted from the local days of a new moon to
8185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * get close to the next or prior new moon, but not cross it.  Must be
8285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * >= 1 and < CalendarAstronomer.SYNODIC_MONTH.
8385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
8485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic const int32_t SYNODIC_GAP = 25;
8585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
8685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
8785bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoU_CDECL_BEGIN
8885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic UBool calendar_chinese_cleanup(void) {
8985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (gChineseCalendarAstro) {
9085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        delete gChineseCalendarAstro;
9185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        gChineseCalendarAstro = NULL;
9285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
9385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (gChineseCalendarWinterSolsticeCache) {
9485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        delete gChineseCalendarWinterSolsticeCache;
9585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        gChineseCalendarWinterSolsticeCache = NULL;
9685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
9785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (gChineseCalendarNewYearCache) {
9885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        delete gChineseCalendarNewYearCache;
9985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        gChineseCalendarNewYearCache = NULL;
10085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
1018393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    if (gChineseCalendarZoneAstroCalc) {
1028393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        delete gChineseCalendarZoneAstroCalc;
1038393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        gChineseCalendarZoneAstroCalc = NULL;
1048393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    }
10559d709d503bab6e2b61931737e662dd293b40578ccornelius    gChineseCalendarZoneAstroCalcInitOnce.reset();
10685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return TRUE;
10785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
10885bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoU_CDECL_END
10985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
11085bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoU_NAMESPACE_BEGIN
11185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
11285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
11385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Implementation of the ChineseCalendar class
11485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
11585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
11685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//-------------------------------------------------------------------------
11785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Constructors...
11885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//-------------------------------------------------------------------------
11985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
12085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
12185bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoCalendar* ChineseCalendar::clone() const {
12285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return new ChineseCalendar(*this);
12385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
12485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
12585bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoChineseCalendar::ChineseCalendar(const Locale& aLocale, UErrorCode& success)
1268393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius:   Calendar(TimeZone::createDefault(), aLocale, success),
1278393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    isLeapYear(FALSE),
1288393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    fEpochYear(CHINESE_EPOCH_YEAR),
1298393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    fZoneAstroCalc(getChineseCalZoneAstroCalc())
1308393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius{
1318393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
1328393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius}
1338393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius
1348393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig CorneliusChineseCalendar::ChineseCalendar(const Locale& aLocale, int32_t epochYear,
1358393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius                                const TimeZone* zoneAstroCalc, UErrorCode &success)
1368393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius:   Calendar(TimeZone::createDefault(), aLocale, success),
1378393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    isLeapYear(FALSE),
1388393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    fEpochYear(epochYear),
1398393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    fZoneAstroCalc(zoneAstroCalc)
14085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
14185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
14285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
14385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
14485bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoChineseCalendar::ChineseCalendar(const ChineseCalendar& other) : Calendar(other) {
14585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    isLeapYear = other.isLeapYear;
1468393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    fEpochYear = other.fEpochYear;
1478393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    fZoneAstroCalc = other.fZoneAstroCalc;
14885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
14985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
15085bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoChineseCalendar::~ChineseCalendar()
15185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
15285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
15385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
15485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoconst char *ChineseCalendar::getType() const {
15585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return "chinese";
15685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
15785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
15859d709d503bab6e2b61931737e662dd293b40578ccorneliusstatic void U_CALLCONV initChineseCalZoneAstroCalc() {
15959d709d503bab6e2b61931737e662dd293b40578ccornelius    gChineseCalendarZoneAstroCalc = new SimpleTimeZone(CHINA_OFFSET, UNICODE_STRING_SIMPLE("CHINA_ZONE") );
16059d709d503bab6e2b61931737e662dd293b40578ccornelius    ucln_i18n_registerCleanup(UCLN_I18N_CHINESE_CALENDAR, calendar_chinese_cleanup);
16159d709d503bab6e2b61931737e662dd293b40578ccornelius}
16259d709d503bab6e2b61931737e662dd293b40578ccornelius
1638393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Corneliusconst TimeZone* ChineseCalendar::getChineseCalZoneAstroCalc(void) const {
16459d709d503bab6e2b61931737e662dd293b40578ccornelius    umtx_initOnce(gChineseCalendarZoneAstroCalcInitOnce, &initChineseCalZoneAstroCalc);
1658393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    return gChineseCalendarZoneAstroCalc;
1668393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius}
1678393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius
16885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//-------------------------------------------------------------------------
16985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Minimum / Maximum access functions
17085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//-------------------------------------------------------------------------
17185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
17285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
17385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic const int32_t LIMITS[UCAL_FIELD_COUNT][4] = {
17485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Minimum  Greatest     Least    Maximum
17585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //           Minimum   Maximum
17685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {        1,        1,    83333,    83333}, // ERA
17785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {        1,        1,       60,       60}, // YEAR
17885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {        0,        0,       11,       11}, // MONTH
17985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {        1,        1,       50,       55}, // WEEK_OF_YEAR
18085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
18185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {        1,        1,       29,       30}, // DAY_OF_MONTH
18285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {        1,        1,      353,      385}, // DAY_OF_YEAR
18385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
18485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {       -1,       -1,        5,        5}, // DAY_OF_WEEK_IN_MONTH
18585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM
18685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
18785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
18885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
18985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
19085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
19185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
19285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
19385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    { -5000000, -5000000,  5000000,  5000000}, // YEAR_WOY
19485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
19585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    { -5000000, -5000000,  5000000,  5000000}, // EXTENDED_YEAR
19685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
19785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
19885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {        0,        0,        1,        1}, // IS_LEAP_MONTH
19985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho};
20085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
20185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
20285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
20385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho* @draft ICU 2.4
20485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho*/
20585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoint32_t ChineseCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
20685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return LIMITS[field][limitType];
20785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
20885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
20985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
21085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//----------------------------------------------------------------------
21185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Calendar framework
21285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//----------------------------------------------------------------------
21385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
21485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
21585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Implement abstract Calendar method to return the extended year
21685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * defined by the current fields.  This will use either the ERA and
21785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * YEAR field as the cycle and year-of-cycle, or the EXTENDED_YEAR
21885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * field as the continuous year count, depending on which is newer.
21985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @stable ICU 2.8
22085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
22185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoint32_t ChineseCalendar::handleGetExtendedYear() {
22285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t year;
22385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (newestStamp(UCAL_ERA, UCAL_YEAR, kUnset) <= fStamp[UCAL_EXTENDED_YEAR]) {
22485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
22585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    } else {
22685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        int32_t cycle = internalGet(UCAL_ERA, 1) - 1; // 0-based cycle
2278393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        // adjust to the instance specific epoch
2288393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        year = cycle * 60 + internalGet(UCAL_YEAR, 1) - (fEpochYear - CHINESE_EPOCH_YEAR);
22985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
23085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return year;
23185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
23285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
23385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
23485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Override Calendar method to return the number of days in the given
23585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * extended year and month.
23685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *
23785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * <p>Note: This method also reads the IS_LEAP_MONTH field to determine
23885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * whether or not the given month is a leap month.
23985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @stable ICU 2.8
24085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
24185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoint32_t ChineseCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const {
24285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t thisStart = handleComputeMonthStart(extendedYear, month, TRUE) -
24385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        kEpochStartAsJulianDay + 1; // Julian day -> local days
24485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t nextStart = newMoonNear(thisStart + SYNODIC_GAP, TRUE);
24585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return nextStart - thisStart;
24685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
24785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
24885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
24985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Override Calendar to compute several fields specific to the Chinese
25085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * calendar system.  These are:
25185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *
25285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * <ul><li>ERA
25385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * <li>YEAR
25485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * <li>MONTH
25585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * <li>DAY_OF_MONTH
25685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * <li>DAY_OF_YEAR
25785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * <li>EXTENDED_YEAR</ul>
25885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *
25985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
26085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * method is called.  The getGregorianXxx() methods return Gregorian
26185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * calendar equivalents for the given Julian day.
26285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *
26385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * <p>Compute the ChineseCalendar-specific field IS_LEAP_MONTH.
26485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @stable ICU 2.8
26585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
26685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hovoid ChineseCalendar::handleComputeFields(int32_t julianDay, UErrorCode &/*status*/) {
26785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
26885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    computeChineseFields(julianDay - kEpochStartAsJulianDay, // local days
26985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                         getGregorianYear(), getGregorianMonth(),
27085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                         TRUE); // set all fields
27185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
27285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
27385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
27485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Field resolution table that incorporates IS_LEAP_MONTH.
27585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
27685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoconst UFieldResolutionTable ChineseCalendar::CHINESE_DATE_PRECEDENCE[] =
27785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
27885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {
27985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { UCAL_DAY_OF_MONTH, kResolveSTOP },
28085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { UCAL_WEEK_OF_YEAR, UCAL_DAY_OF_WEEK, kResolveSTOP },
28185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { UCAL_WEEK_OF_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP },
28285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP },
28385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { UCAL_WEEK_OF_YEAR, UCAL_DOW_LOCAL, kResolveSTOP },
28485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { UCAL_WEEK_OF_MONTH, UCAL_DOW_LOCAL, kResolveSTOP },
28585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DOW_LOCAL, kResolveSTOP },
28685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { UCAL_DAY_OF_YEAR, kResolveSTOP },
28785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { kResolveRemap | UCAL_DAY_OF_MONTH, UCAL_IS_LEAP_MONTH, kResolveSTOP },
28885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { kResolveSTOP }
28985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    },
29085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {
29185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { UCAL_WEEK_OF_YEAR, kResolveSTOP },
29285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { UCAL_WEEK_OF_MONTH, kResolveSTOP },
29385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { UCAL_DAY_OF_WEEK_IN_MONTH, kResolveSTOP },
29485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { kResolveRemap | UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP },
29585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { kResolveRemap | UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DOW_LOCAL, kResolveSTOP },
29685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        { kResolveSTOP }
29785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    },
29885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    {{kResolveSTOP}}
29985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho};
30085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
30185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
30285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Override Calendar to add IS_LEAP_MONTH to the field resolution
30385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * table.
30485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @stable ICU 2.8
30585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
30685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoconst UFieldResolutionTable* ChineseCalendar::getFieldResolutionTable() const {
30785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return CHINESE_DATE_PRECEDENCE;
30885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
30985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
31085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
31185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Return the Julian day number of day before the first day of the
31285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * given month in the given extended year.
31385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *
31485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * <p>Note: This method reads the IS_LEAP_MONTH field to determine
31585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * whether the given month is a leap month.
31685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param eyear the extended year
31785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param month the zero-based month.  The month is also determined
31885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * by reading the IS_LEAP_MONTH field.
31985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @return the Julian day number of the day before the first
32085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * day of the given month and year
32185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @stable ICU 2.8
32285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
32385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoint32_t ChineseCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool useMonth) const {
32485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
32585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    ChineseCalendar *nonConstThis = (ChineseCalendar*)this; // cast away const
32685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
32785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // If the month is out of range, adjust it into range, and
32885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // modify the extended year value accordingly.
32985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (month < 0 || month > 11) {
33085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        double m = month;
33185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        eyear += (int32_t)ClockMath::floorDivide(m, 12.0, m);
33285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        month = (int32_t)m;
33385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
33485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
3358393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    int32_t gyear = eyear + fEpochYear - 1; // Gregorian year
33685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t theNewYear = newYear(gyear);
33785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t newMoon = newMoonNear(theNewYear + month * 29, TRUE);
33885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
33985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t julianDay = newMoon + kEpochStartAsJulianDay;
34085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
34185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Save fields for later restoration
34285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t saveMonth = internalGet(UCAL_MONTH);
34385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t saveIsLeapMonth = internalGet(UCAL_IS_LEAP_MONTH);
34485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
34585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Ignore IS_LEAP_MONTH field if useMonth is false
34685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t isLeapMonth = useMonth ? saveIsLeapMonth : 0;
34785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
34885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UErrorCode status = U_ZERO_ERROR;
34985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    nonConstThis->computeGregorianFields(julianDay, status);
35085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (U_FAILURE(status))
35185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        return 0;
35285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
35385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // This will modify the MONTH and IS_LEAP_MONTH fields (only)
35485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    nonConstThis->computeChineseFields(newMoon, getGregorianYear(),
35585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                         getGregorianMonth(), FALSE);
35685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
35785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (month != internalGet(UCAL_MONTH) ||
35885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        isLeapMonth != internalGet(UCAL_IS_LEAP_MONTH)) {
35985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        newMoon = newMoonNear(newMoon + SYNODIC_GAP, TRUE);
36085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        julianDay = newMoon + kEpochStartAsJulianDay;
36185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
36285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
36385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    nonConstThis->internalSet(UCAL_MONTH, saveMonth);
36485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    nonConstThis->internalSet(UCAL_IS_LEAP_MONTH, saveIsLeapMonth);
36585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
36685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return julianDay - 1;
36785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
36885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
36985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
37085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
37185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Override Calendar to handle leap months properly.
37285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @stable ICU 2.8
37385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
37485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hovoid ChineseCalendar::add(UCalendarDateFields field, int32_t amount, UErrorCode& status) {
37585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    switch (field) {
37685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    case UCAL_MONTH:
37785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (amount != 0) {
37885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            int32_t dom = get(UCAL_DAY_OF_MONTH, status);
37985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if (U_FAILURE(status)) break;
38085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            int32_t day = get(UCAL_JULIAN_DAY, status) - kEpochStartAsJulianDay; // Get local day
38185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if (U_FAILURE(status)) break;
38285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            int32_t moon = day - dom + 1; // New moon
38385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            offsetMonth(moon, dom, amount);
38485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
38585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        break;
38685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    default:
38785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        Calendar::add(field, amount, status);
38885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        break;
38985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
39085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
39185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
39285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
39385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Override Calendar to handle leap months properly.
39485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @stable ICU 2.8
39585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
39685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hovoid ChineseCalendar::add(EDateFields field, int32_t amount, UErrorCode& status) {
39785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    add((UCalendarDateFields)field, amount, status);
39885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
39985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
40085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
40185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Override Calendar to handle leap months properly.
40285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @stable ICU 2.8
40385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
40485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hovoid ChineseCalendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) {
40585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    switch (field) {
40685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    case UCAL_MONTH:
40785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (amount != 0) {
40885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            int32_t dom = get(UCAL_DAY_OF_MONTH, status);
40985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if (U_FAILURE(status)) break;
41085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            int32_t day = get(UCAL_JULIAN_DAY, status) - kEpochStartAsJulianDay; // Get local day
41185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if (U_FAILURE(status)) break;
41285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            int32_t moon = day - dom + 1; // New moon (start of this month)
41385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
41485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            // Note throughout the following:  Months 12 and 1 are never
41585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            // followed by a leap month (D&R p. 185).
41685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
41785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            // Compute the adjusted month number m.  This is zero-based
41885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            // value from 0..11 in a non-leap year, and from 0..12 in a
41985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            // leap year.
42085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            int32_t m = get(UCAL_MONTH, status); // 0-based month
42185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if (U_FAILURE(status)) break;
42285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if (isLeapYear) { // (member variable)
42385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                if (get(UCAL_IS_LEAP_MONTH, status) == 1) {
42485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    ++m;
42585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                } else {
42685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    // Check for a prior leap month.  (In the
42785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    // following, month 0 is the first month of the
42885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    // year.)  Month 0 is never followed by a leap
42985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    // month, and we know month m is not a leap month.
43085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    // moon1 will be the start of month 0 if there is
43185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    // no leap month between month 0 and month m;
43285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    // otherwise it will be the start of month 1.
43385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    int moon1 = moon -
43485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                        (int) (CalendarAstronomer::SYNODIC_MONTH * (m - 0.5));
43585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    moon1 = newMoonNear(moon1, TRUE);
43685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    if (isLeapMonthBetween(moon1, moon)) {
43785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                        ++m;
43885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    }
43985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                }
44085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                if (U_FAILURE(status)) break;
44185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            }
44285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
44385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            // Now do the standard roll computation on m, with the
44485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            // allowed range of 0..n-1, where n is 12 or 13.
44585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            int32_t n = isLeapYear ? 13 : 12; // Months in this year
44685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            int32_t newM = (m + amount) % n;
44785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if (newM < 0) {
44885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                newM += n;
44985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            }
45085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
45185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if (newM != m) {
45285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                offsetMonth(moon, dom, newM - m);
45385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            }
45485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
45585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        break;
45685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    default:
45785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        Calendar::roll(field, amount, status);
45885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        break;
45985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
46085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
46185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
46285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hovoid ChineseCalendar::roll(EDateFields field, int32_t amount, UErrorCode& status) {
46385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    roll((UCalendarDateFields)field, amount, status);
46485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
46585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
46685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
46785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//------------------------------------------------------------------
46885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Support methods and constants
46985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//------------------------------------------------------------------
47085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
47185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
47285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Convert local days to UTC epoch milliseconds.
4738393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * This is not an accurate conversion in that getTimezoneOffset
4748393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * takes the milliseconds in GMT (not local time). In theory, more
4758393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * accurate algorithm can be implemented but practically we do not need
4768393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * to go through that complication as long as the historical timezone
4778393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * changes did not happen around the 'tricky' new moon (new moon around
4788393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * midnight).
4798393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius *
4808393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * @param days days after January 1, 1970 0:00 in the astronomical base zone
48185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @return milliseconds after January 1, 1970 0:00 GMT
48285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
4838393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Corneliusdouble ChineseCalendar::daysToMillis(double days) const {
4848393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    double millis = days * (double)kOneDay;
4858393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    if (fZoneAstroCalc != NULL) {
4868393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        int32_t rawOffset, dstOffset;
4878393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        UErrorCode status = U_ZERO_ERROR;
4888393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        fZoneAstroCalc->getOffset(millis, FALSE, rawOffset, dstOffset, status);
4898393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        if (U_SUCCESS(status)) {
4908393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        	return millis - (double)(rawOffset + dstOffset);
4918393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        }
4928393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    }
4938393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    return millis - (double)CHINA_OFFSET;
49485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
49585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
49685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
49785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Convert UTC epoch milliseconds to local days.
49885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param millis milliseconds after January 1, 1970 0:00 GMT
4998393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * @return days after January 1, 1970 0:00 in the astronomical base zone
50085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
5018393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Corneliusdouble ChineseCalendar::millisToDays(double millis) const {
5028393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    if (fZoneAstroCalc != NULL) {
5038393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        int32_t rawOffset, dstOffset;
5048393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        UErrorCode status = U_ZERO_ERROR;
5058393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        fZoneAstroCalc->getOffset(millis, FALSE, rawOffset, dstOffset, status);
5068393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        if (U_SUCCESS(status)) {
5078393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        	return ClockMath::floorDivide(millis + (double)(rawOffset + dstOffset), kOneDay);
5088393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        }
5098393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    }
5108393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    return ClockMath::floorDivide(millis + (double)CHINA_OFFSET, kOneDay);
51185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
51285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
51385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//------------------------------------------------------------------
51485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Astronomical computations
51585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//------------------------------------------------------------------
51685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
51785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
51885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
51985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Return the major solar term on or after December 15 of the given
52085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Gregorian year, that is, the winter solstice of the given year.
52185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Computations are relative to Asia/Shanghai time zone.
52285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param gyear a Gregorian year
52385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @return days after January 1, 1970 0:00 Asia/Shanghai of the
52485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * winter solstice of the given year
52585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
52685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoint32_t ChineseCalendar::winterSolstice(int32_t gyear) const {
52785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
52885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UErrorCode status = U_ZERO_ERROR;
52985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t cacheValue = CalendarCache::get(&gChineseCalendarWinterSolsticeCache, gyear, status);
53085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
53185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (cacheValue == 0) {
53285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // In books December 15 is used, but it fails for some years
53385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // using our algorithms, e.g.: 1298 1391 1492 1553 1560.  That
53485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // is, winterSolstice(1298) starts search at Dec 14 08:00:00
53585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // PST 1298 with a final result of Dec 14 10:31:59 PST 1299.
53685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        double ms = daysToMillis(Grego::fieldsToDay(gyear, UCAL_DECEMBER, 1));
53785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
53885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        umtx_lock(&astroLock);
53985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if(gChineseCalendarAstro == NULL) {
54085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            gChineseCalendarAstro = new CalendarAstronomer();
54185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            ucln_i18n_registerCleanup(UCLN_I18N_CHINESE_CALENDAR, calendar_chinese_cleanup);
54285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
54385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        gChineseCalendarAstro->setTime(ms);
54485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        UDate solarLong = gChineseCalendarAstro->getSunTime(CalendarAstronomer::WINTER_SOLSTICE(), TRUE);
54585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        umtx_unlock(&astroLock);
54685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
54785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // Winter solstice is 270 degrees solar longitude aka Dongzhi
54885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        cacheValue = (int32_t)millisToDays(solarLong);
54985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        CalendarCache::put(&gChineseCalendarWinterSolsticeCache, gyear, cacheValue, status);
55085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
55185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if(U_FAILURE(status)) {
55285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        cacheValue = 0;
55385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
55485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return cacheValue;
55585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
55685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
55785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
55885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Return the closest new moon to the given date, searching either
55985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * forward or backward in time.
56085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param days days after January 1, 1970 0:00 Asia/Shanghai
56185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param after if true, search for a new moon on or after the given
56285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * date; otherwise, search for a new moon before it
56385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @return days after January 1, 1970 0:00 Asia/Shanghai of the nearest
56485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * new moon after or before <code>days</code>
56585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
56685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoint32_t ChineseCalendar::newMoonNear(double days, UBool after) const {
56785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
56885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    umtx_lock(&astroLock);
56985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if(gChineseCalendarAstro == NULL) {
57085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        gChineseCalendarAstro = new CalendarAstronomer();
57185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        ucln_i18n_registerCleanup(UCLN_I18N_CHINESE_CALENDAR, calendar_chinese_cleanup);
57285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
57385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    gChineseCalendarAstro->setTime(daysToMillis(days));
57485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UDate newMoon = gChineseCalendarAstro->getMoonTime(CalendarAstronomer::NEW_MOON(), after);
57585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    umtx_unlock(&astroLock);
57685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
57785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return (int32_t) millisToDays(newMoon);
57885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
57985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
58085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
58185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Return the nearest integer number of synodic months between
58285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * two dates.
58385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param day1 days after January 1, 1970 0:00 Asia/Shanghai
58485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param day2 days after January 1, 1970 0:00 Asia/Shanghai
58585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @return the nearest integer number of months between day1 and day2
58685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
58785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoint32_t ChineseCalendar::synodicMonthsBetween(int32_t day1, int32_t day2) const {
58885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    double roundme = ((day2 - day1) / CalendarAstronomer::SYNODIC_MONTH);
58985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return (int32_t) (roundme + (roundme >= 0 ? .5 : -.5));
59085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
59185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
59285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
59385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Return the major solar term on or before a given date.  This
59485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * will be an integer from 1..12, with 1 corresponding to 330 degrees,
59585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * 2 to 0 degrees, 3 to 30 degrees,..., and 12 to 300 degrees.
59685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param days days after January 1, 1970 0:00 Asia/Shanghai
59785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
59885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoint32_t ChineseCalendar::majorSolarTerm(int32_t days) const {
59985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
60085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    umtx_lock(&astroLock);
60185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if(gChineseCalendarAstro == NULL) {
60285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        gChineseCalendarAstro = new CalendarAstronomer();
60385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        ucln_i18n_registerCleanup(UCLN_I18N_CHINESE_CALENDAR, calendar_chinese_cleanup);
60485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
60585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    gChineseCalendarAstro->setTime(daysToMillis(days));
60685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UDate solarLongitude = gChineseCalendarAstro->getSunLongitude();
60785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    umtx_unlock(&astroLock);
60885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
60985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Compute (floor(solarLongitude / (pi/6)) + 2) % 12
61085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t term = ( ((int32_t)(6 * solarLongitude / CalendarAstronomer::PI)) + 2 ) % 12;
61185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (term < 1) {
61285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        term += 12;
61385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
61485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return term;
61585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
61685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
61785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
61885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Return true if the given month lacks a major solar term.
61985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param newMoon days after January 1, 1970 0:00 Asia/Shanghai of a new
62085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * moon
62185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
62285bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoUBool ChineseCalendar::hasNoMajorSolarTerm(int32_t newMoon) const {
62385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return majorSolarTerm(newMoon) ==
62485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        majorSolarTerm(newMoonNear(newMoon + SYNODIC_GAP, TRUE));
62585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
62685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
62785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
62885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//------------------------------------------------------------------
62985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Time to fields
63085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//------------------------------------------------------------------
63185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
63285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
63385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Return true if there is a leap month on or after month newMoon1 and
63485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * at or before month newMoon2.
6358393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * @param newMoon1 days after January 1, 1970 0:00 astronomical base zone
6368393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * of a new moon
6378393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * @param newMoon2 days after January 1, 1970 0:00 astronomical base zone
6388393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * of a new moon
63985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
64085bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoUBool ChineseCalendar::isLeapMonthBetween(int32_t newMoon1, int32_t newMoon2) const {
64185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
64285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#ifdef U_DEBUG_CHNSECAL
64385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // This is only needed to debug the timeOfAngle divergence bug.
64485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Remove this later. Liu 11/9/00
64585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (synodicMonthsBetween(newMoon1, newMoon2) >= 50) {
64685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        U_DEBUG_CHNSECAL_MSG((
64785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            "isLeapMonthBetween(%d, %d): Invalid parameters", newMoon1, newMoon2
64885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            ));
64985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
65085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif
65185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
65285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return (newMoon2 >= newMoon1) &&
65385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        (isLeapMonthBetween(newMoon1, newMoonNear(newMoon2 - SYNODIC_GAP, FALSE)) ||
65485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho         hasNoMajorSolarTerm(newMoon2));
65585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
65685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
65785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
65885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Compute fields for the Chinese calendar system.  This method can
65985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * either set all relevant fields, as required by
66085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * <code>handleComputeFields()</code>, or it can just set the MONTH and
66185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * IS_LEAP_MONTH fields, as required by
66285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * <code>handleComputeMonthStart()</code>.
66385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho *
66485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * <p>As a side effect, this method sets {@link #isLeapYear}.
6658393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * @param days days after January 1, 1970 0:00 astronomical base zone
6668393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * of the date to compute fields for
66785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param gyear the Gregorian year of the given date
66885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param gmonth the Gregorian month of the given date
66985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param setAllFields if true, set the EXTENDED_YEAR, ERA, YEAR,
67085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * DAY_OF_MONTH, and DAY_OF_YEAR fields.  In either case set the MONTH
67185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * and IS_LEAP_MONTH fields.
67285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
67385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hovoid ChineseCalendar::computeChineseFields(int32_t days, int32_t gyear, int32_t gmonth,
67485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                                  UBool setAllFields) {
67585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
67685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Find the winter solstices before and after the target date.
67785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // These define the boundaries of this Chinese year, specifically,
67885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // the position of month 11, which always contains the solstice.
67985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // We want solsticeBefore <= date < solsticeAfter.
68085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t solsticeBefore;
68185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t solsticeAfter = winterSolstice(gyear);
68285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (days < solsticeAfter) {
68385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        solsticeBefore = winterSolstice(gyear - 1);
68485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    } else {
68585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        solsticeBefore = solsticeAfter;
68685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        solsticeAfter = winterSolstice(gyear + 1);
68785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
68885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
68985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Find the start of the month after month 11.  This will be either
69085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // the prior month 12 or leap month 11 (very rare).  Also find the
69185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // start of the following month 11.
69285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t firstMoon = newMoonNear(solsticeBefore + 1, TRUE);
69385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t lastMoon = newMoonNear(solsticeAfter + 1, FALSE);
69485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t thisMoon = newMoonNear(days + 1, FALSE); // Start of this month
69585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Note: isLeapYear is a member variable
69685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    isLeapYear = synodicMonthsBetween(firstMoon, lastMoon) == 12;
69785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
69885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t month = synodicMonthsBetween(firstMoon, thisMoon);
69985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (isLeapYear && isLeapMonthBetween(firstMoon, thisMoon)) {
70085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        month--;
70185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
70285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (month < 1) {
70385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        month += 12;
70485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
70585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
70685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UBool isLeapMonth = isLeapYear &&
70785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        hasNoMajorSolarTerm(thisMoon) &&
70885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        !isLeapMonthBetween(firstMoon, newMoonNear(thisMoon - SYNODIC_GAP, FALSE));
70985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
71085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    internalSet(UCAL_MONTH, month-1); // Convert from 1-based to 0-based
71185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    internalSet(UCAL_IS_LEAP_MONTH, isLeapMonth?1:0);
71285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
71385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (setAllFields) {
71485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
7158393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        // Extended year and cycle year is based on the epoch year
7168393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius
7178393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        int32_t extended_year = gyear - fEpochYear;
7188393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        int cycle_year = gyear - CHINESE_EPOCH_YEAR;
71985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (month < 11 ||
72085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            gmonth >= UCAL_JULY) {
7218393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius            extended_year++;
7228393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius            cycle_year++;
72385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
72485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        int32_t dayOfMonth = days - thisMoon + 1;
72585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
7268393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        internalSet(UCAL_EXTENDED_YEAR, extended_year);
72785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
72885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // 0->0,60  1->1,1  60->1,60  61->2,1  etc.
72985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        int32_t yearOfCycle;
7308393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        int32_t cycle = ClockMath::floorDivide(cycle_year - 1, 60, yearOfCycle);
73185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        internalSet(UCAL_ERA, cycle + 1);
73285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        internalSet(UCAL_YEAR, yearOfCycle + 1);
73385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
73485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        internalSet(UCAL_DAY_OF_MONTH, dayOfMonth);
73585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
73685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // Days will be before the first new year we compute if this
73785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // date is in month 11, leap 11, 12.  There is never a leap 12.
73885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // New year computations are cached so this should be cheap in
73985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // the long run.
74085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        int32_t theNewYear = newYear(gyear);
74185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (days < theNewYear) {
74285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            theNewYear = newYear(gyear-1);
74385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
74485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        internalSet(UCAL_DAY_OF_YEAR, days - theNewYear + 1);
74585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
74685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
74785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
74885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
74985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//------------------------------------------------------------------
75085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Fields to time
75185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//------------------------------------------------------------------
75285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
75385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
75485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Return the Chinese new year of the given Gregorian year.
75585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param gyear a Gregorian year
7568393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius * @return days after January 1, 1970 0:00 astronomical base zone of the
75785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Chinese new year of the given year (this will be a new moon)
75885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
75985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoint32_t ChineseCalendar::newYear(int32_t gyear) const {
76085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UErrorCode status = U_ZERO_ERROR;
76185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t cacheValue = CalendarCache::get(&gChineseCalendarNewYearCache, gyear, status);
76285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
76385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (cacheValue == 0) {
76485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
76585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        int32_t solsticeBefore= winterSolstice(gyear - 1);
76685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        int32_t solsticeAfter = winterSolstice(gyear);
76785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        int32_t newMoon1 = newMoonNear(solsticeBefore + 1, TRUE);
76885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        int32_t newMoon2 = newMoonNear(newMoon1 + SYNODIC_GAP, TRUE);
76985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        int32_t newMoon11 = newMoonNear(solsticeAfter + 1, FALSE);
77085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
77185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (synodicMonthsBetween(newMoon1, newMoon11) == 12 &&
77285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            (hasNoMajorSolarTerm(newMoon1) || hasNoMajorSolarTerm(newMoon2))) {
77385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            cacheValue = newMoonNear(newMoon2 + SYNODIC_GAP, TRUE);
77485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        } else {
77585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            cacheValue = newMoon2;
77685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
77785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
77885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        CalendarCache::put(&gChineseCalendarNewYearCache, gyear, cacheValue, status);
77985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
78085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if(U_FAILURE(status)) {
78185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        cacheValue = 0;
78285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
78385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return cacheValue;
78485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
78585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
78685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
78785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Adjust this calendar to be delta months before or after a given
78885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * start position, pinning the day of month if necessary.  The start
78985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * position is given as a local days number for the start of the month
79085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * and a day-of-month.  Used by add() and roll().
79185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param newMoon the local days of the first day of the month of the
79285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * start position (days after January 1, 1970 0:00 Asia/Shanghai)
79385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param dom the 1-based day-of-month of the start position
79485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * @param delta the number of months to move forward or backward from
79585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * the start position
79685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
79785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hovoid ChineseCalendar::offsetMonth(int32_t newMoon, int32_t dom, int32_t delta) {
79885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UErrorCode status = U_ZERO_ERROR;
79985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
80085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Move to the middle of the month before our target month.
80185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    newMoon += (int32_t) (CalendarAstronomer::SYNODIC_MONTH * (delta - 0.5));
80285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
80385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Search forward to the target month's new moon
80485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    newMoon = newMoonNear(newMoon, TRUE);
80585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
80685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Find the target dom
80785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t jd = newMoon + kEpochStartAsJulianDay - 1 + dom;
80885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
80985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Pin the dom.  In this calendar all months are 29 or 30 days
81085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // so pinning just means handling dom 30.
81185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (dom > 29) {
81285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        set(UCAL_JULIAN_DAY, jd-1);
81385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // TODO Fix this.  We really shouldn't ever have to
81485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // explicitly call complete().  This is either a bug in
81585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // this method, in ChineseCalendar, or in
81685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        // Calendar.getActualMaximum().  I suspect the last.
81785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        complete(status);
81885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (U_FAILURE(status)) return;
81985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (getActualMaximum(UCAL_DAY_OF_MONTH, status) >= dom) {
82085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if (U_FAILURE(status)) return;
82185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            set(UCAL_JULIAN_DAY, jd);
82285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
82385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    } else {
82485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        set(UCAL_JULIAN_DAY, jd);
82585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
82685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
82785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
82885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
82985bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoUBool
83085bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoChineseCalendar::inDaylightTime(UErrorCode& status) const
83185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
83285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // copied from GregorianCalendar
83385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (U_FAILURE(status) || !getTimeZone().useDaylightTime())
83485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        return FALSE;
83585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
83685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Force an update of the state of the Calendar.
83785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    ((ChineseCalendar*)this)->complete(status); // cast away const
83885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
83985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE);
84085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
84185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
84285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// default century
84385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
84459d709d503bab6e2b61931737e662dd293b40578ccorneliusstatic UDate     gSystemDefaultCenturyStart       = DBL_MIN;
84559d709d503bab6e2b61931737e662dd293b40578ccorneliusstatic int32_t   gSystemDefaultCenturyStartYear   = -1;
84659d709d503bab6e2b61931737e662dd293b40578ccorneliusstatic icu::UInitOnce gSystemDefaultCenturyInitOnce = U_INITONCE_INITIALIZER;
84785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
84885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
84985bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoUBool ChineseCalendar::haveDefaultCentury() const
85085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
85185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return TRUE;
85285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
85385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
85485bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoUDate ChineseCalendar::defaultCenturyStart() const
85585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
85685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return internalGetDefaultCenturyStart();
85785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
85885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
85985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoint32_t ChineseCalendar::defaultCenturyStartYear() const
86085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
86185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return internalGetDefaultCenturyStartYear();
86285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
86385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
86459d709d503bab6e2b61931737e662dd293b40578ccorneliusstatic void U_CALLCONV initializeSystemDefaultCentury()
86585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho{
86685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // initialize systemDefaultCentury and systemDefaultCenturyYear based
86785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // on the current time.  They'll be set to 80 years before
86885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // the current time.
86985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UErrorCode status = U_ZERO_ERROR;
87085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    ChineseCalendar calendar(Locale("@calendar=chinese"),status);
87159d709d503bab6e2b61931737e662dd293b40578ccornelius    if (U_SUCCESS(status)) {
87285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        calendar.setTime(Calendar::getNow(), status);
87385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        calendar.add(UCAL_YEAR, -80, status);
87459d709d503bab6e2b61931737e662dd293b40578ccornelius        gSystemDefaultCenturyStart     = calendar.getTime(status);
87559d709d503bab6e2b61931737e662dd293b40578ccornelius        gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
87685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
87785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // We have no recourse upon failure unless we want to propagate the failure
87885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // out.
87985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
88085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
88159d709d503bab6e2b61931737e662dd293b40578ccorneliusUDate
88259d709d503bab6e2b61931737e662dd293b40578ccorneliusChineseCalendar::internalGetDefaultCenturyStart() const
88359d709d503bab6e2b61931737e662dd293b40578ccornelius{
88459d709d503bab6e2b61931737e662dd293b40578ccornelius    // lazy-evaluate systemDefaultCenturyStart
88559d709d503bab6e2b61931737e662dd293b40578ccornelius    umtx_initOnce(gSystemDefaultCenturyInitOnce, &initializeSystemDefaultCentury);
88659d709d503bab6e2b61931737e662dd293b40578ccornelius    return gSystemDefaultCenturyStart;
88759d709d503bab6e2b61931737e662dd293b40578ccornelius}
88859d709d503bab6e2b61931737e662dd293b40578ccornelius
88959d709d503bab6e2b61931737e662dd293b40578ccorneliusint32_t
89059d709d503bab6e2b61931737e662dd293b40578ccorneliusChineseCalendar::internalGetDefaultCenturyStartYear() const
89159d709d503bab6e2b61931737e662dd293b40578ccornelius{
89259d709d503bab6e2b61931737e662dd293b40578ccornelius    // lazy-evaluate systemDefaultCenturyStartYear
89359d709d503bab6e2b61931737e662dd293b40578ccornelius    umtx_initOnce(gSystemDefaultCenturyInitOnce, &initializeSystemDefaultCentury);
89459d709d503bab6e2b61931737e662dd293b40578ccornelius    return    gSystemDefaultCenturyStartYear;
89559d709d503bab6e2b61931737e662dd293b40578ccornelius}
89659d709d503bab6e2b61931737e662dd293b40578ccornelius
89785bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoUOBJECT_DEFINE_RTTI_IMPLEMENTATION(ChineseCalendar)
89885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
89985bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoU_NAMESPACE_END
90085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
90185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif
902ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru
903