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