mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-13 08:53:20 +00:00
ICU-2424 hebrew checked in, placeholder for chinese
X-SVN-Rev: 14060
This commit is contained in:
parent
48f3512cdc
commit
7fb178d7d0
11 changed files with 1388 additions and 56 deletions
|
@ -62,7 +62,7 @@ ucurr.o digitlst.o \
|
|||
choicfmt.o datefmt.o smpdtfmt.o dtfmtsym.o udat.o \
|
||||
nfrs.o nfrule.o nfsubs.o rbnf.o \
|
||||
ucal.o calendar.o gregocal.o timezone.o simpletz.o olsontz.o \
|
||||
astro.o buddhcal.o islamcal.o japancal.o gregoimp.o \
|
||||
astro.o buddhcal.o islamcal.o japancal.o gregoimp.o hebrwcal.o chnsecal.o \
|
||||
sortkey.o bocsu.o coleitr.o coll.o ucoleitr.o \
|
||||
ucol.o ucol_bld.o ucol_cnt.o ucol_elm.o ucol_tok.o ucol_wgt.o tblcoll.o \
|
||||
strmatch.o usearch.o search.o stsearch.o \
|
||||
|
|
|
@ -13,6 +13,9 @@
|
|||
#include "math.h"
|
||||
#include <float.h>
|
||||
#include "unicode/putil.h"
|
||||
#include "uhash.h"
|
||||
#include "umutex.h"
|
||||
#include "ucln_in.h"
|
||||
#include <stdio.h> // for toString()
|
||||
|
||||
#ifdef U_DEBUG_ASTRO
|
||||
|
@ -55,6 +58,8 @@ static inline UBool isINVALID(double d) {
|
|||
return(uprv_isNaN(d));
|
||||
}
|
||||
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
/**
|
||||
* The number of standard hours in one sidereal day.
|
||||
* Approximately 24.93.
|
||||
|
@ -1457,6 +1462,77 @@ UnicodeString CalendarAstronomer::Horizon::toString() const
|
|||
// return Integer.toString(deg) + "\u00b0" + min + "'" + sec + "\"";
|
||||
// }
|
||||
|
||||
// =============== Calendar Cache ================
|
||||
UMTX ccLock = NULL;
|
||||
|
||||
void CalendarCache::createCache(CalendarCache** cache, UErrorCode& status) {
|
||||
ucln_i18n_registerCleanup();
|
||||
*cache = new CalendarCache(32, status);
|
||||
if(cache == NULL) {
|
||||
status = U_MEMORY_ALLOCATION_ERROR;
|
||||
}
|
||||
if(U_FAILURE(status)) {
|
||||
delete *cache;
|
||||
*cache = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t CalendarCache::get(CalendarCache** cache, int32_t key, UErrorCode &status) {
|
||||
int32_t res;
|
||||
|
||||
if(U_FAILURE(status)) {
|
||||
return 0;
|
||||
}
|
||||
umtx_lock(&ccLock);
|
||||
|
||||
if(*cache == NULL) {
|
||||
createCache(cache, status);
|
||||
if(U_FAILURE(status)) {
|
||||
umtx_unlock(&ccLock);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
res = uhash_geti((*cache)->fTable, (void*)key);
|
||||
|
||||
umtx_unlock(&ccLock);
|
||||
return res;
|
||||
}
|
||||
|
||||
void CalendarCache::put(CalendarCache** cache, int32_t key, int32_t value, UErrorCode &status) {
|
||||
|
||||
if(U_FAILURE(status)) {
|
||||
return;
|
||||
}
|
||||
umtx_lock(&ccLock);
|
||||
|
||||
if(*cache == NULL) {
|
||||
createCache(cache, status);
|
||||
if(U_FAILURE(status)) {
|
||||
umtx_unlock(&ccLock);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
uhash_puti((*cache)->fTable, (void*)key, value, &status);
|
||||
|
||||
umtx_unlock(&ccLock);
|
||||
}
|
||||
|
||||
CalendarCache::CalendarCache(int32_t size, UErrorCode &status) {
|
||||
fTable = uhash_openSize(uhash_hashLong, uhash_compareLong, 32, &status);
|
||||
}
|
||||
|
||||
CalendarCache::~CalendarCache() {
|
||||
if(fTable != NULL) {
|
||||
uhash_close(fTable);
|
||||
}
|
||||
}
|
||||
|
||||
U_NAMESPACE_END
|
||||
|
||||
U_CFUNC UBool calendar_astro_cleanup(void) {
|
||||
umtx_destroy(&ccLock);
|
||||
}
|
||||
|
||||
#endif // !UCONFIG_NO_FORMATTING
|
||||
|
|
|
@ -757,5 +757,27 @@ private:
|
|||
*/
|
||||
UDate local(UDate localMillis);
|
||||
};
|
||||
|
||||
struct UHashtable;
|
||||
|
||||
/**
|
||||
* Cache of month -> julian day
|
||||
* @internal
|
||||
*/
|
||||
class U_I18N_API CalendarCache : public UMemory {
|
||||
public:
|
||||
static int32_t get(CalendarCache** cache, int32_t key, UErrorCode &status);
|
||||
static void put(CalendarCache** cache, int32_t key, int32_t value, UErrorCode &status);
|
||||
virtual ~CalendarCache();
|
||||
private:
|
||||
CalendarCache(int32_t size, UErrorCode& status);
|
||||
static void createCache(CalendarCache** cache, UErrorCode& status);
|
||||
/**
|
||||
* not implemented
|
||||
*/
|
||||
CalendarCache();
|
||||
UHashtable *fTable;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
@ -34,6 +34,8 @@
|
|||
#include "buddhcal.h"
|
||||
#include "japancal.h"
|
||||
#include "islamcal.h"
|
||||
#include "hebrwcal.h"
|
||||
#include "chnsecal.h"
|
||||
#include "unicode/calendar.h"
|
||||
#include "cpputils.h"
|
||||
#include "iculserv.h"
|
||||
|
@ -167,6 +169,10 @@ protected:
|
|||
return new IslamicCalendar(canLoc, status, IslamicCalendar::CIVIL);
|
||||
} else if(!uprv_strcmp(fType, "@calendar=islamic")) {
|
||||
return new IslamicCalendar(canLoc, status, IslamicCalendar::ASTRONOMICAL);
|
||||
} else if(!uprv_strcmp(fType, "@calendar=hebrew")) {
|
||||
return new HebrewCalendar(canLoc, status);
|
||||
//} else if(!uprv_strcmp(fType, "@calendar=chinese")) {
|
||||
//return new ChineseCalendar(canLoc, status);
|
||||
} else {
|
||||
status = U_UNSUPPORTED_ERROR;
|
||||
return NULL;
|
||||
|
@ -335,9 +341,11 @@ getService(void)
|
|||
// Register all basic instances.
|
||||
newservice->registerFactory(new BasicCalendarFactory("@calendar=japanese"),status);
|
||||
newservice->registerFactory(new BasicCalendarFactory("@calendar=buddhist"),status);
|
||||
newservice->registerFactory(new BasicCalendarFactory("@calendar=gregorian"),status);
|
||||
newservice->registerFactory(new BasicCalendarFactory("@calendar=hebrew"),status);
|
||||
newservice->registerFactory(new BasicCalendarFactory("@calendar=islamic"),status);
|
||||
// newservice->registerFactory(new BasicCalendarFactory("@calendar=chinese"),status);
|
||||
newservice->registerFactory(new BasicCalendarFactory("@calendar=islamic-civil"),status);
|
||||
newservice->registerFactory(new BasicCalendarFactory("@calendar=gregorian"),status);
|
||||
|
||||
#ifdef U_DEBUG_CALSVC
|
||||
fprintf(stderr, "Done..\n");
|
||||
|
@ -2982,9 +2990,13 @@ U_NAMESPACE_END
|
|||
// INTERNAL - for cleanup
|
||||
// clean up the astronomical data & cache
|
||||
U_CFUNC UBool calendar_islamic_cleanup(void);
|
||||
U_CFUNC UBool calendar_hebrew_cleanup(void);
|
||||
U_CFUNC UBool calendar_astro_cleanup(void);
|
||||
|
||||
U_CFUNC UBool calendar_cleanup(void) {
|
||||
calendar_islamic_cleanup();
|
||||
calendar_hebrew_cleanup();
|
||||
calendar_astro_cleanup();
|
||||
if (gService) {
|
||||
delete gService;
|
||||
gService = NULL;
|
||||
|
|
14
icu4c/source/i18n/chnsecal.cpp
Normal file
14
icu4c/source/i18n/chnsecal.cpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
// Place holder
|
||||
#include "chnsecal.h"
|
||||
void placeHolder() {
|
||||
;
|
||||
}
|
||||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (C) 1996-2003, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
*******************************************************************************
|
||||
*
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
11
icu4c/source/i18n/chnsecal.h
Normal file
11
icu4c/source/i18n/chnsecal.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
/*
|
||||
******************************************************************************
|
||||
* Copyright (C) 1996-2003, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
******************************************************************************
|
||||
*
|
||||
*
|
||||
**************************************************************************
|
||||
*/
|
||||
|
||||
// placeholder
|
733
icu4c/source/i18n/hebrwcal.cpp
Normal file
733
icu4c/source/i18n/hebrwcal.cpp
Normal file
|
@ -0,0 +1,733 @@
|
|||
/*
|
||||
* Copyright (C) 2003, International Business Machines Corporation
|
||||
* and others. All Rights Reserved.
|
||||
******************************************************************************
|
||||
*
|
||||
* File HEBRWCAL.H
|
||||
*
|
||||
* Modification History:
|
||||
*
|
||||
* Date Name Description
|
||||
* 12/03/2003 srl ported from java HebrewCalendar
|
||||
*****************************************************************************
|
||||
*/
|
||||
|
||||
#include "hebrwcal.h"
|
||||
|
||||
#if !UCONFIG_NO_FORMATTING
|
||||
|
||||
#include "mutex.h"
|
||||
#include <float.h>
|
||||
#include "gregoimp.h" // Math
|
||||
#include "astro.h" // CalendarAstronomer
|
||||
#include "uhash.h"
|
||||
#include "ucln_in.h"
|
||||
|
||||
U_NAMESPACE_BEGIN
|
||||
// Hebrew Calendar implementation
|
||||
|
||||
/**
|
||||
* The absolute date, in milliseconds since 1/1/1970 AD, Gregorian,
|
||||
* of the start of the Hebrew calendar. In order to keep this calendar's
|
||||
* time of day in sync with that of the Gregorian calendar, we use
|
||||
* midnight, rather than sunset the day before.
|
||||
*/
|
||||
static const double EPOCH_MILLIS = -180799862400000.; // 1/1/1 HY
|
||||
|
||||
static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = {
|
||||
// Minimum Greatest Least Maximum
|
||||
// Minimum Maximum
|
||||
{ 0, 0, 0, 0 }, // ERA
|
||||
{ 1, 1, 5000000, 5000000 }, // YEAR
|
||||
{ 0, 0, 12, 12 }, // MONTH
|
||||
{ 1, 1, 51, 56 }, // WEEK_OF_YEAR
|
||||
{ 0, 0, 5, 6 }, // WEEK_OF_MONTH
|
||||
{ 1, 1, 29, 30 }, // DAY_OF_MONTH
|
||||
{ 1, 1, 353, 385 }, // DAY_OF_YEAR
|
||||
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
|
||||
{ -1, -1, 4, 6 }, // DAY_OF_WEEK_IN_MONTH
|
||||
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1/* */}, // AM_PM
|
||||
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
|
||||
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
|
||||
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
|
||||
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
|
||||
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
|
||||
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
|
||||
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
|
||||
{ -5000001, -5000001, 5000001, 5000001 }, // YEAR_WOY
|
||||
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
|
||||
{ -5000000, -5000000, 5000000, 5000000 }, // EXTENDED_YEAR
|
||||
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
|
||||
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
|
||||
};
|
||||
|
||||
/**
|
||||
* The lengths of the Hebrew months. This is complicated, because there
|
||||
* are three different types of years, or six if you count leap years.
|
||||
* Due to the rules for postponing the start of the year to avoid having
|
||||
* certain holidays fall on the sabbath, the year can end up being three
|
||||
* different lengths, called "deficient", "normal", and "complete".
|
||||
*/
|
||||
static const int32_t MONTH_LENGTH[][3] = {
|
||||
// Deficient Normal Complete
|
||||
{ 30, 30, 30 }, //Tishri
|
||||
{ 29, 29, 30 }, //Heshvan
|
||||
{ 29, 30, 30 }, //Kislev
|
||||
{ 29, 29, 29 }, //Tevet
|
||||
{ 30, 30, 30 }, //Shevat
|
||||
{ 30, 30, 30 }, //Adar I (leap years only)
|
||||
{ 29, 29, 29 }, //Adar
|
||||
{ 30, 30, 30 }, //Nisan
|
||||
{ 29, 29, 29 }, //Iyar
|
||||
{ 30, 30, 30 }, //Sivan
|
||||
{ 29, 29, 29 }, //Tammuz
|
||||
{ 30, 30, 30 }, //Av
|
||||
{ 29, 29, 29 }, //Elul
|
||||
};
|
||||
|
||||
/**
|
||||
* The cumulative # of days to the end of each month in a non-leap year
|
||||
* Although this can be calculated from the MONTH_LENGTH table,
|
||||
* keeping it around separately makes some calculations a lot faster
|
||||
*/
|
||||
|
||||
static const int32_t MONTH_START[][3] = {
|
||||
// Deficient Normal Complete
|
||||
{ 0, 0, 0 }, // (placeholder)
|
||||
{ 30, 30, 30 }, // Tishri
|
||||
{ 59, 59, 60 }, // Heshvan
|
||||
{ 88, 89, 90 }, // Kislev
|
||||
{ 117, 118, 119 }, // Tevet
|
||||
{ 147, 148, 149 }, // Shevat
|
||||
{ 147, 148, 149 }, // (Adar I placeholder)
|
||||
{ 176, 177, 178 }, // Adar
|
||||
{ 206, 207, 208 }, // Nisan
|
||||
{ 235, 236, 237 }, // Iyar
|
||||
{ 265, 266, 267 }, // Sivan
|
||||
{ 294, 295, 296 }, // Tammuz
|
||||
{ 324, 325, 326 }, // Av
|
||||
{ 353, 354, 355 }, // Elul
|
||||
};
|
||||
|
||||
/**
|
||||
* The cumulative # of days to the end of each month in a leap year
|
||||
*/
|
||||
static const int32_t LEAP_MONTH_START[][3] = {
|
||||
// Deficient Normal Complete
|
||||
{ 0, 0, 0 }, // (placeholder)
|
||||
{ 30, 30, 30 }, // Tishri
|
||||
{ 59, 59, 60 }, // Heshvan
|
||||
{ 88, 89, 90 }, // Kislev
|
||||
{ 117, 118, 119 }, // Tevet
|
||||
{ 147, 148, 149 }, // Shevat
|
||||
{ 177, 178, 179 }, // Adar I
|
||||
{ 206, 207, 208 }, // Adar II
|
||||
{ 236, 237, 238 }, // Nisan
|
||||
{ 265, 266, 267 }, // Iyar
|
||||
{ 295, 296, 297 }, // Sivan
|
||||
{ 324, 325, 326 }, // Tammuz
|
||||
{ 354, 355, 356 }, // Av
|
||||
{ 383, 384, 385 }, // Elul
|
||||
};
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Data Members...
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
CalendarCache *gCache = NULL;
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Constructors...
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs a default <code>HebrewCalendar</code> using the current time
|
||||
* in the default time zone with the default locale.
|
||||
* @internal
|
||||
*/
|
||||
HebrewCalendar::HebrewCalendar(const Locale& aLocale, UErrorCode& success)
|
||||
: Calendar(TimeZone::createDefault(), aLocale, success)
|
||||
|
||||
{
|
||||
setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
|
||||
}
|
||||
|
||||
|
||||
HebrewCalendar::~HebrewCalendar() {
|
||||
}
|
||||
|
||||
const char *HebrewCalendar::getType() const {
|
||||
return "hebrew";
|
||||
}
|
||||
|
||||
Calendar* HebrewCalendar::clone() const {
|
||||
return new HebrewCalendar(*this);
|
||||
}
|
||||
|
||||
HebrewCalendar::HebrewCalendar(const HebrewCalendar& other) : Calendar(other) {
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Rolling and adding functions overridden from Calendar
|
||||
//
|
||||
// These methods call through to the default implementation in IBMCalendar
|
||||
// for most of the fields and only handle the unusual ones themselves.
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Add a signed amount to a specified field, using this calendar's rules.
|
||||
* For example, to add three days to the current date, you can call
|
||||
* <code>add(Calendar.DATE, 3)</code>.
|
||||
* <p>
|
||||
* When adding to certain fields, the values of other fields may conflict and
|
||||
* need to be changed. For example, when adding one to the {@link #MONTH MONTH} field
|
||||
* for the date "30 Av 5758", the {@link #DAY_OF_MONTH DAY_OF_MONTH} field
|
||||
* must be adjusted so that the result is "29 Elul 5758" rather than the invalid
|
||||
* "30 Elul 5758".
|
||||
* <p>
|
||||
* This method is able to add to
|
||||
* all fields except for {@link #ERA ERA}, {@link #DST_OFFSET DST_OFFSET},
|
||||
* and {@link #ZONE_OFFSET ZONE_OFFSET}.
|
||||
* <p>
|
||||
* <b>Note:</b> You should always use {@link #roll roll} and add rather
|
||||
* than attempting to perform arithmetic operations directly on the fields
|
||||
* of a <tt>HebrewCalendar</tt>. Since the {@link #MONTH MONTH} field behaves
|
||||
* discontinuously in non-leap years, simple arithmetic can give invalid results.
|
||||
* <p>
|
||||
* @param field the time field.
|
||||
* @param amount the amount to add to the field.
|
||||
*
|
||||
* @exception IllegalArgumentException if the field is invalid or refers
|
||||
* to a field that cannot be handled by this method.
|
||||
* @internal
|
||||
*/
|
||||
void HebrewCalendar::add(UCalendarDateFields field, int32_t amount, UErrorCode& status)
|
||||
{
|
||||
if(U_FAILURE(status)) {
|
||||
return;
|
||||
}
|
||||
switch (field) {
|
||||
case UCAL_MONTH:
|
||||
{
|
||||
// We can't just do a set(MONTH, get(MONTH) + amount). The
|
||||
// reason is ADAR_1. Suppose amount is +2 and we land in
|
||||
// ADAR_1 -- then we have to bump to ADAR_2 aka ADAR. But
|
||||
// if amount is -2 and we land in ADAR_1, then we have to
|
||||
// bump the other way -- down to SHEVAT. - Alan 11/00
|
||||
int32_t month = get(UCAL_MONTH, status);
|
||||
int32_t year = get(UCAL_YEAR, status);
|
||||
UBool acrossAdar1;
|
||||
if (amount > 0) {
|
||||
acrossAdar1 = (month < ADAR_1); // started before ADAR_1?
|
||||
month += amount;
|
||||
for (;;) {
|
||||
if (acrossAdar1 && month>=ADAR_1 && !isLeapYear(year)) {
|
||||
++month;
|
||||
}
|
||||
if (month <= ELUL) {
|
||||
break;
|
||||
}
|
||||
month -= ELUL+1;
|
||||
++year;
|
||||
acrossAdar1 = true;
|
||||
}
|
||||
} else {
|
||||
acrossAdar1 = (month > ADAR_1); // started after ADAR_1?
|
||||
month += amount;
|
||||
for (;;) {
|
||||
if (acrossAdar1 && month<=ADAR_1 && !isLeapYear(year)) {
|
||||
--month;
|
||||
}
|
||||
if (month >= 0) {
|
||||
break;
|
||||
}
|
||||
month += ELUL+1;
|
||||
--year;
|
||||
acrossAdar1 = true;
|
||||
}
|
||||
}
|
||||
set(UCAL_MONTH, month);
|
||||
set(UCAL_YEAR, year);
|
||||
pinField(UCAL_DAY_OF_MONTH, status);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
Calendar::add(field, amount, status);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rolls (up/down) a specified amount time on the given field. For
|
||||
* example, to roll the current date up by three days, you can call
|
||||
* <code>roll(Calendar.DATE, 3)</code>. If the
|
||||
* field is rolled past its maximum allowable value, it will "wrap" back
|
||||
* to its minimum and continue rolling.
|
||||
* For example, calling <code>roll(Calendar.DATE, 10)</code>
|
||||
* on a Hebrew calendar set to "25 Av 5758" will result in the date "5 Av 5758".
|
||||
* <p>
|
||||
* When rolling certain fields, the values of other fields may conflict and
|
||||
* need to be changed. For example, when rolling the {@link #MONTH MONTH} field
|
||||
* upward by one for the date "30 Av 5758", the {@link #DAY_OF_MONTH DAY_OF_MONTH} field
|
||||
* must be adjusted so that the result is "29 Elul 5758" rather than the invalid
|
||||
* "30 Elul".
|
||||
* <p>
|
||||
* This method is able to roll
|
||||
* all fields except for {@link #ERA ERA}, {@link #DST_OFFSET DST_OFFSET},
|
||||
* and {@link #ZONE_OFFSET ZONE_OFFSET}. Subclasses may, of course, add support for
|
||||
* additional fields in their overrides of <code>roll</code>.
|
||||
* <p>
|
||||
* <b>Note:</b> You should always use roll and {@link #add add} rather
|
||||
* than attempting to perform arithmetic operations directly on the fields
|
||||
* of a <tt>HebrewCalendar</tt>. Since the {@link #MONTH MONTH} field behaves
|
||||
* discontinuously in non-leap years, simple arithmetic can give invalid results.
|
||||
* <p>
|
||||
* @param field the time field.
|
||||
* @param amount the amount by which the field should be rolled.
|
||||
*
|
||||
* @exception IllegalArgumentException if the field is invalid or refers
|
||||
* to a field that cannot be handled by this method.
|
||||
* @internal
|
||||
*/
|
||||
void HebrewCalendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status)
|
||||
{
|
||||
if(U_FAILURE(status)) {
|
||||
return;
|
||||
}
|
||||
switch (field) {
|
||||
case UCAL_MONTH:
|
||||
{
|
||||
int32_t month = get(UCAL_MONTH, status);
|
||||
int32_t year = get(UCAL_YEAR, status);
|
||||
|
||||
UBool leapYear = isLeapYear(year);
|
||||
int32_t yearLength = monthsInYear(year);
|
||||
int32_t newMonth = month + (amount % yearLength);
|
||||
//
|
||||
// If it's not a leap year and we're rolling past the missing month
|
||||
// of ADAR_1, we need to roll an extra month to make up for it.
|
||||
//
|
||||
if (!leapYear) {
|
||||
if (amount > 0 && month < ADAR_1 && newMonth >= ADAR_1) {
|
||||
newMonth++;
|
||||
} else if (amount < 0 && month > ADAR_1 && newMonth <= ADAR_1) {
|
||||
newMonth--;
|
||||
}
|
||||
}
|
||||
set(UCAL_MONTH, (newMonth + 13) % 13);
|
||||
pinField(UCAL_DAY_OF_MONTH, status);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
Calendar::roll(field, amount, status);
|
||||
}
|
||||
}
|
||||
|
||||
void HebrewCalendar::roll(EDateFields field, int32_t amount, UErrorCode& status) {
|
||||
return roll((UCalendarDateFields)field, amount, status);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Support methods
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// Hebrew date calculations are performed in terms of days, hours, and
|
||||
// "parts" (or halakim), which are 1/1080 of an hour, or 3 1/3 seconds.
|
||||
static const int32_t HOUR_PARTS = 1080;
|
||||
static const int32_t DAY_PARTS = 24*HOUR_PARTS;
|
||||
|
||||
// An approximate value for the length of a lunar month.
|
||||
// It is used to calculate the approximate year and month of a given
|
||||
// absolute date.
|
||||
static const int32_t MONTH_DAYS = 29;
|
||||
static const int32_t MONTH_FRACT = 12*HOUR_PARTS + 793;
|
||||
static const int32_t MONTH_PARTS = MONTH_DAYS*DAY_PARTS + MONTH_FRACT;
|
||||
|
||||
// The time of the new moon (in parts) on 1 Tishri, year 1 (the epoch)
|
||||
// counting from noon on the day before. BAHARAD is an abbreviation of
|
||||
// Bet (Monday), Hey (5 hours from sunset), Resh-Daled (204).
|
||||
static const int32_t BAHARAD = 11*HOUR_PARTS + 204;
|
||||
|
||||
/**
|
||||
* Finds the day # of the first day in the given Hebrew year.
|
||||
* To do this, we want to calculate the time of the Tishri 1 new moon
|
||||
* in that year.
|
||||
* <p>
|
||||
* The algorithm here is similar to ones described in a number of
|
||||
* references, including:
|
||||
* <ul>
|
||||
* <li>"Calendrical Calculations", by Nachum Dershowitz & Edward Reingold,
|
||||
* Cambridge University Press, 1997, pages 85-91.
|
||||
*
|
||||
* <li>Hebrew Calendar Science and Myths,
|
||||
* <a href="http://www.geocities.com/Athens/1584/">
|
||||
* http://www.geocities.com/Athens/1584/</a>
|
||||
*
|
||||
* <li>The Calendar FAQ,
|
||||
* <a href="http://www.faqs.org/faqs/calendars/faq/">
|
||||
* http://www.faqs.org/faqs/calendars/faq/</a>
|
||||
* </ul>
|
||||
*/
|
||||
int32_t HebrewCalendar::startOfYear(int32_t year, UErrorCode &status)
|
||||
{
|
||||
int32_t day = CalendarCache::get(&gCache, year, status);
|
||||
|
||||
if (day == 0) {
|
||||
int32_t months = (235 * year - 234) / 19; // # of months before year
|
||||
|
||||
int32_t frac = months * MONTH_FRACT + BAHARAD; // Fractional part of day #
|
||||
day = months * 29 + (frac / DAY_PARTS); // Whole # part of calculation
|
||||
frac = frac % DAY_PARTS; // Time of day
|
||||
|
||||
int32_t wd = (day % 7); // Day of week (0 == Monday)
|
||||
|
||||
if (wd == 2 || wd == 4 || wd == 6) {
|
||||
// If the 1st is on Sun, Wed, or Fri, postpone to the next day
|
||||
day += 1;
|
||||
wd = (day % 7);
|
||||
}
|
||||
if (wd == 1 && frac > 15*HOUR_PARTS+204 && !isLeapYear(year) ) {
|
||||
// If the new moon falls after 3:11:20am (15h204p from the previous noon)
|
||||
// on a Tuesday and it is not a leap year, postpone by 2 days.
|
||||
// This prevents 356-day years.
|
||||
day += 2;
|
||||
}
|
||||
else if (wd == 0 && frac > 21*HOUR_PARTS+589 && isLeapYear(year-1) ) {
|
||||
// If the new moon falls after 9:32:43 1/3am (21h589p from yesterday noon)
|
||||
// on a Monday and *last* year was a leap year, postpone by 1 day.
|
||||
// Prevents 382-day years.
|
||||
day += 1;
|
||||
}
|
||||
CalendarCache::put(&gCache, year, day, status);
|
||||
}
|
||||
return day;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the day of the week for a given day
|
||||
*
|
||||
* @param day The # of days since the start of the Hebrew calendar,
|
||||
* 1-based (i.e. 1/1/1 AM is day 1).
|
||||
*/
|
||||
int32_t HebrewCalendar::absoluteDayToDayOfWeek(int32_t day)
|
||||
{
|
||||
// We know that 1/1/1 AM is a Monday, which makes the math easy...
|
||||
return (day % 7) + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the the type of a given year.
|
||||
* 0 "Deficient" year with 353 or 383 days
|
||||
* 1 "Normal" year with 354 or 384 days
|
||||
* 2 "Complete" year with 355 or 385 days
|
||||
*/
|
||||
int32_t HebrewCalendar::yearType(int32_t year) const
|
||||
{
|
||||
int32_t yearLength = handleGetYearLength(year);
|
||||
|
||||
if (yearLength > 380) {
|
||||
yearLength -= 30; // Subtract length of leap month.
|
||||
}
|
||||
|
||||
int type = 0;
|
||||
|
||||
switch (yearLength) {
|
||||
case 353:
|
||||
type = 0; break;
|
||||
case 354:
|
||||
type = 1; break;
|
||||
case 355:
|
||||
type = 2; break;
|
||||
default:
|
||||
//throw new RuntimeException("Illegal year length " + yearLength + " in year " + year);
|
||||
type = 1;
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a given Hebrew year is a leap year
|
||||
*
|
||||
* The rule here is that if (year % 19) == 0, 3, 6, 8, 11, 14, or 17.
|
||||
* The formula below performs the same test, believe it or not.
|
||||
*/
|
||||
UBool HebrewCalendar::isLeapYear(int32_t year) {
|
||||
//return (year * 12 + 17) % 19 >= 12;
|
||||
int32_t x = (year*12 + 17) % 19;
|
||||
return x >= ((x < 0) ? -7 : 12);
|
||||
}
|
||||
|
||||
int32_t HebrewCalendar::monthsInYear(int32_t year) {
|
||||
return isLeapYear(year) ? 13 : 12;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Calendar framework
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
int32_t HebrewCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
|
||||
return LIMITS[field][limitType];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of the given month in the given year
|
||||
* @internal
|
||||
*/
|
||||
int32_t HebrewCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const {
|
||||
switch (month) {
|
||||
case HESHVAN:
|
||||
case KISLEV:
|
||||
// These two month lengths can vary
|
||||
return MONTH_LENGTH[month][yearType(extendedYear)];
|
||||
|
||||
default:
|
||||
// The rest are a fixed length
|
||||
return MONTH_LENGTH[month][0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of days in the given Hebrew year
|
||||
* @internal
|
||||
*/
|
||||
int32_t HebrewCalendar::handleGetYearLength(int32_t eyear) const {
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
return startOfYear(eyear+1, status) - startOfYear(eyear, status);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from milliseconds to field values
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Subclasses may override this method to compute several fields
|
||||
* specific to each calendar system. These are:
|
||||
*
|
||||
* <ul><li>ERA
|
||||
* <li>YEAR
|
||||
* <li>MONTH
|
||||
* <li>DAY_OF_MONTH
|
||||
* <li>DAY_OF_YEAR
|
||||
* <li>EXTENDED_YEAR</ul>
|
||||
*
|
||||
* Subclasses can refer to the DAY_OF_WEEK and DOW_LOCAL fields,
|
||||
* which will be set when this method is called. Subclasses can
|
||||
* also call the getGregorianXxx() methods to obtain Gregorian
|
||||
* calendar equivalents for the given Julian day.
|
||||
*
|
||||
* <p>In addition, subclasses should compute any subclass-specific
|
||||
* fields, that is, fields from BASE_FIELD_COUNT to
|
||||
* getFieldCount() - 1.
|
||||
* @internal
|
||||
*/
|
||||
void HebrewCalendar::handleComputeFields(int32_t julianDay, UErrorCode &status) {
|
||||
int32_t d = julianDay - 347997;
|
||||
double m = ((d * (double)DAY_PARTS)/ (double) MONTH_PARTS); // Months (approx)
|
||||
int32_t year = (int32_t)( ((19. * m + 234.) / 235.) + 1.); // Years (approx)
|
||||
int32_t ys = startOfYear(year, status); // 1st day of year
|
||||
int32_t dayOfYear = (d - ys);
|
||||
|
||||
// Because of the postponement rules, it's possible to guess wrong. Fix it.
|
||||
while (dayOfYear < 1) {
|
||||
year--;
|
||||
ys = startOfYear(year, status);
|
||||
dayOfYear = (d - ys);
|
||||
}
|
||||
|
||||
// Now figure out which month we're in, and the date within that month
|
||||
int32_t type = yearType(year);
|
||||
UBool isLeap = isLeapYear(year);
|
||||
|
||||
int32_t month = 0;
|
||||
while (dayOfYear > ( isLeap ? LEAP_MONTH_START[month][type] : MONTH_START[month][type] ) ) {
|
||||
month++;
|
||||
}
|
||||
month--;
|
||||
int dayOfMonth = dayOfYear - (isLeap ? LEAP_MONTH_START[month][type] : MONTH_START[month][type]);
|
||||
|
||||
internalSet(UCAL_ERA, 0);
|
||||
internalSet(UCAL_YEAR, year);
|
||||
internalSet(UCAL_EXTENDED_YEAR, year);
|
||||
internalSet(UCAL_MONTH, month);
|
||||
internalSet(UCAL_DAY_OF_MONTH, dayOfMonth);
|
||||
internalSet(UCAL_DAY_OF_YEAR, dayOfYear);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from field values to milliseconds
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
int32_t HebrewCalendar::handleGetExtendedYear() {
|
||||
int32_t year;
|
||||
if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
|
||||
year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
|
||||
} else {
|
||||
year = internalGet(UCAL_YEAR, 1); // Default to year 1
|
||||
}
|
||||
return year;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return JD of start of given month/year.
|
||||
* @internal
|
||||
*/
|
||||
int32_t HebrewCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool useMonth) const {
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
// Resolve out-of-range months. This is necessary in order to
|
||||
// obtain the correct year. We correct to
|
||||
// a 12- or 13-month year (add/subtract 12 or 13, depending
|
||||
// on the year) but since we _always_ number from 0..12, and
|
||||
// the leap year determines whether or not month 5 (Adar 1)
|
||||
// is present, we allow 0..12 in any given year.
|
||||
while (month < 0) {
|
||||
month += monthsInYear(--eyear);
|
||||
}
|
||||
// Careful: allow 0..12 in all years
|
||||
while (month > 12) {
|
||||
month -= monthsInYear(eyear++);
|
||||
}
|
||||
|
||||
int32_t day = startOfYear(eyear, status);
|
||||
|
||||
if(U_FAILURE(status)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (month != 0) {
|
||||
if (isLeapYear(eyear)) {
|
||||
day += LEAP_MONTH_START[month][yearType(eyear)];
|
||||
} else {
|
||||
day += MONTH_START[month][yearType(eyear)];
|
||||
}
|
||||
}
|
||||
|
||||
return (int) (day + 347997);
|
||||
}
|
||||
|
||||
UBool
|
||||
HebrewCalendar::inDaylightTime(UErrorCode& status) const
|
||||
{
|
||||
// copied from GregorianCalendar
|
||||
if (U_FAILURE(status) || !getTimeZone().useDaylightTime())
|
||||
return FALSE;
|
||||
|
||||
// Force an update of the state of the Calendar.
|
||||
((HebrewCalendar*)this)->complete(status); // cast away const
|
||||
|
||||
return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE);
|
||||
}
|
||||
|
||||
// default century
|
||||
const UDate HebrewCalendar::fgSystemDefaultCentury = DBL_MIN;
|
||||
const int32_t HebrewCalendar::fgSystemDefaultCenturyYear = -1;
|
||||
|
||||
UDate HebrewCalendar::fgSystemDefaultCenturyStart = DBL_MIN;
|
||||
int32_t HebrewCalendar::fgSystemDefaultCenturyStartYear = -1;
|
||||
|
||||
|
||||
UBool HebrewCalendar::haveDefaultCentury() const
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
UDate HebrewCalendar::defaultCenturyStart() const
|
||||
{
|
||||
return internalGetDefaultCenturyStart();
|
||||
}
|
||||
|
||||
int32_t HebrewCalendar::defaultCenturyStartYear() const
|
||||
{
|
||||
return internalGetDefaultCenturyStartYear();
|
||||
}
|
||||
|
||||
UDate
|
||||
HebrewCalendar::internalGetDefaultCenturyStart() const
|
||||
{
|
||||
// lazy-evaluate systemDefaultCenturyStart
|
||||
UBool needsUpdate;
|
||||
{
|
||||
Mutex m;
|
||||
needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury);
|
||||
}
|
||||
|
||||
if (needsUpdate) {
|
||||
initializeSystemDefaultCentury();
|
||||
}
|
||||
|
||||
// use defaultCenturyStart unless it's the flag value;
|
||||
// then use systemDefaultCenturyStart
|
||||
|
||||
return fgSystemDefaultCenturyStart;
|
||||
}
|
||||
|
||||
int32_t
|
||||
HebrewCalendar::internalGetDefaultCenturyStartYear() const
|
||||
{
|
||||
// lazy-evaluate systemDefaultCenturyStartYear
|
||||
UBool needsUpdate;
|
||||
{
|
||||
Mutex m;
|
||||
needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury);
|
||||
}
|
||||
|
||||
if (needsUpdate) {
|
||||
initializeSystemDefaultCentury();
|
||||
}
|
||||
|
||||
// use defaultCenturyStart unless it's the flag value;
|
||||
// then use systemDefaultCenturyStartYear
|
||||
|
||||
return fgSystemDefaultCenturyStartYear;
|
||||
}
|
||||
|
||||
void
|
||||
HebrewCalendar::initializeSystemDefaultCentury()
|
||||
{
|
||||
// initialize systemDefaultCentury and systemDefaultCenturyYear based
|
||||
// on the current time. They'll be set to 80 years before
|
||||
// the current time.
|
||||
// No point in locking as it should be idempotent.
|
||||
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
|
||||
{
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
Calendar *calendar = new HebrewCalendar(Locale("he@calendar=hebrew"),status);
|
||||
if (calendar != NULL && U_SUCCESS(status))
|
||||
{
|
||||
calendar->setTime(Calendar::getNow(), status);
|
||||
calendar->add(UCAL_YEAR, -80, status);
|
||||
UDate newStart = calendar->getTime(status);
|
||||
int32_t newYear = calendar->get(UCAL_YEAR, status);
|
||||
{
|
||||
Mutex m;
|
||||
fgSystemDefaultCenturyStart = newStart;
|
||||
fgSystemDefaultCenturyStartYear = newYear;
|
||||
}
|
||||
delete calendar;
|
||||
}
|
||||
// We have no recourse upon failure unless we want to propagate the failure
|
||||
// out.
|
||||
}
|
||||
}
|
||||
|
||||
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(HebrewCalendar);
|
||||
|
||||
|
||||
U_NAMESPACE_END
|
||||
|
||||
U_CFUNC UBool calendar_hebrew_cleanup(void) {
|
||||
delete gCache;
|
||||
gCache = NULL;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
#endif // UCONFIG_NO_FORMATTING
|
||||
|
484
icu4c/source/i18n/hebrwcal.h
Normal file
484
icu4c/source/i18n/hebrwcal.h
Normal file
|
@ -0,0 +1,484 @@
|
|||
/*
|
||||
* Copyright (C) 2003, International Business Machines Corporation and others. All Rights Reserved.
|
||||
********************************************************************************
|
||||
*
|
||||
* File HEBRWCAL.H
|
||||
*
|
||||
* Modification History:
|
||||
*
|
||||
* Date Name Description
|
||||
* 05/13/2003 srl copied from gregocal.h
|
||||
* 11/26/2003 srl copied from buddhcal.h
|
||||
********************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef HEBRWCAL_H
|
||||
#define HEBRWCAL_H
|
||||
|
||||
#include "unicode/utypes.h"
|
||||
|
||||
#if !UCONFIG_NO_FORMATTING
|
||||
|
||||
#include "unicode/calendar.h"
|
||||
#include "unicode/gregocal.h"
|
||||
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
/**
|
||||
* <code>HebrewCalendar</code> is a subclass of <code>Calendar</code>
|
||||
* that that implements the traditional Hebrew calendar.
|
||||
* This is the civil calendar in Israel and the liturgical calendar
|
||||
* of the Jewish faith worldwide.
|
||||
* <p>
|
||||
* The Hebrew calendar is lunisolar and thus has a number of interesting
|
||||
* properties that distinguish it from the Gregorian. Months start
|
||||
* on the day of (an arithmetic approximation of) each new moon. Since the
|
||||
* solar year (approximately 365.24 days) is not an even multiple of
|
||||
* the lunar month (approximately 29.53 days) an extra "leap month" is
|
||||
* inserted in 7 out of every 19 years. To make matters even more
|
||||
* interesting, the start of a year can be delayed by up to three days
|
||||
* in order to prevent certain holidays from falling on the Sabbath and
|
||||
* to prevent certain illegal year lengths. Finally, the lengths of certain
|
||||
* months can vary depending on the number of days in the year.
|
||||
* <p>
|
||||
* The leap month is known as "Adar 1" and is inserted between the
|
||||
* months of Shevat and Adar in leap years. Since the leap month does
|
||||
* not come at the end of the year, calculations involving
|
||||
* month numbers are particularly complex. Users of this class should
|
||||
* make sure to use the {@link #roll roll} and {@link #add add} methods
|
||||
* rather than attempting to perform date arithmetic by manipulating
|
||||
* the fields directly.
|
||||
* <p>
|
||||
* <b>Note:</b> In the traditional Hebrew calendar, days start at sunset.
|
||||
* However, in order to keep the time fields in this class
|
||||
* synchronized with those of the other calendars and with local clock time,
|
||||
* we treat days and months as beginning at midnight,
|
||||
* roughly 6 hours after the corresponding sunset.
|
||||
* <p>
|
||||
* If you are interested in more information on the rules behind the Hebrew
|
||||
* calendar, see one of the following references:
|
||||
* <ul>
|
||||
* <li>"<a href="http://www.amazon.com/exec/obidos/ASIN/0521564743">Calendrical Calculations</a>",
|
||||
* by Nachum Dershowitz & Edward Reingold, Cambridge University Press, 1997, pages 85-91.
|
||||
*
|
||||
* <li>Hebrew Calendar Science and Myths,
|
||||
* <a href="http://www.geocities.com/Athens/1584/">
|
||||
* http://www.geocities.com/Athens/1584/</a>
|
||||
*
|
||||
* <li>The Calendar FAQ,
|
||||
* <a href="http://www.faqs.org/faqs/calendars/faq/">
|
||||
* http://www.faqs.org/faqs/calendars/faq/</a>
|
||||
* </ul>
|
||||
* <p>
|
||||
* @see com.ibm.icu.util.GregorianCalendar
|
||||
*
|
||||
* @author Laura Werner
|
||||
* @author Alan Liu
|
||||
* @author Steven R. Loomis
|
||||
* <p>
|
||||
* @internal
|
||||
*/
|
||||
class U_I18N_API HebrewCalendar : public Calendar {
|
||||
public:
|
||||
/**
|
||||
* Useful constants for HebrewCalendar.
|
||||
* @internal
|
||||
*/
|
||||
enum EEras {
|
||||
/**
|
||||
* Constant for Tishri, the 1st month of the Hebrew year.
|
||||
*/
|
||||
TISHRI,
|
||||
/**
|
||||
* Constant for Heshvan, the 2nd month of the Hebrew year.
|
||||
*/
|
||||
HESHVAN,
|
||||
/**
|
||||
* Constant for Kislev, the 3rd month of the Hebrew year.
|
||||
*/
|
||||
KISLEV,
|
||||
|
||||
/**
|
||||
* Constant for Tevet, the 4th month of the Hebrew year.
|
||||
*/
|
||||
TEVET,
|
||||
|
||||
/**
|
||||
* Constant for Shevat, the 5th month of the Hebrew year.
|
||||
*/
|
||||
SHEVAT,
|
||||
|
||||
/**
|
||||
* Constant for Adar I, the 6th month of the Hebrew year
|
||||
* (present in leap years only). In non-leap years, the calendar
|
||||
* jumps from Shevat (5th month) to Adar (7th month).
|
||||
*/
|
||||
ADAR_1,
|
||||
|
||||
/**
|
||||
* Constant for the Adar, the 7th month of the Hebrew year.
|
||||
*/
|
||||
ADAR,
|
||||
|
||||
/**
|
||||
* Constant for Nisan, the 8th month of the Hebrew year.
|
||||
*/
|
||||
NISAN,
|
||||
|
||||
/**
|
||||
* Constant for Iyar, the 9th month of the Hebrew year.
|
||||
*/
|
||||
IYAR,
|
||||
|
||||
/**
|
||||
* Constant for Sivan, the 10th month of the Hebrew year.
|
||||
*/
|
||||
SIVAN,
|
||||
|
||||
/**
|
||||
* Constant for Tammuz, the 11th month of the Hebrew year.
|
||||
*/
|
||||
TAMUZ,
|
||||
|
||||
/**
|
||||
* Constant for Av, the 12th month of the Hebrew year.
|
||||
*/
|
||||
AV,
|
||||
|
||||
/**
|
||||
* Constant for Elul, the 13th month of the Hebrew year.
|
||||
*/
|
||||
ELUL
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructs a HebrewCalendar based on the current time in the default time zone
|
||||
* with the given locale.
|
||||
*
|
||||
* @param aLocale The given locale.
|
||||
* @param success Indicates the status of HebrewCalendar object construction.
|
||||
* Returns U_ZERO_ERROR if constructed successfully.
|
||||
* @internal
|
||||
*/
|
||||
HebrewCalendar(const Locale& aLocale, UErrorCode& success);
|
||||
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
* @internal
|
||||
*/
|
||||
virtual ~HebrewCalendar();
|
||||
|
||||
/**
|
||||
* Copy constructor
|
||||
* @param source the object to be copied.
|
||||
* @internal
|
||||
*/
|
||||
HebrewCalendar(const HebrewCalendar& source);
|
||||
|
||||
/**
|
||||
* Default assignment operator
|
||||
* @param right the object to be copied.
|
||||
* @internal
|
||||
*/
|
||||
HebrewCalendar& operator=(const HebrewCalendar& right);
|
||||
|
||||
/**
|
||||
* Create and return a polymorphic copy of this calendar.
|
||||
* @return return a polymorphic copy of this calendar.
|
||||
* @internal
|
||||
*/
|
||||
virtual Calendar* clone(void) const;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Override Calendar Returns a unique class ID POLYMORPHICALLY. Pure virtual
|
||||
* override. This method is to implement a simple version of RTTI, since not all C++
|
||||
* compilers support genuine RTTI. Polymorphic operator==() and clone() methods call
|
||||
* this method.
|
||||
*
|
||||
* @return The class ID for this object. All objects of a given class have the
|
||||
* same class ID. Objects of other classes have different class IDs.
|
||||
* @internal
|
||||
*/
|
||||
virtual UClassID getDynamicClassID(void) const;
|
||||
|
||||
/**
|
||||
* Return the class ID for this class. This is useful only for comparing to a return
|
||||
* value from getDynamicClassID(). For example:
|
||||
*
|
||||
* Base* polymorphic_pointer = createPolymorphicObject();
|
||||
* if (polymorphic_pointer->getDynamicClassID() ==
|
||||
* Derived::getStaticClassID()) ...
|
||||
*
|
||||
* @return The class ID for all objects of this class.
|
||||
* @internal
|
||||
*/
|
||||
static UClassID getStaticClassID(void);
|
||||
|
||||
/**
|
||||
* return the calendar type, "hebrew".
|
||||
*
|
||||
* @return calendar type
|
||||
* @internal
|
||||
*/
|
||||
virtual const char * getType() const;
|
||||
|
||||
|
||||
// Calendar API
|
||||
public:
|
||||
/**
|
||||
* (Overrides Calendar) UDate Arithmetic function. Adds the specified (signed) amount
|
||||
* of time to the given time field, based on the calendar's rules. For more
|
||||
* information, see the documentation for Calendar::add().
|
||||
*
|
||||
* @param field The time field.
|
||||
* @param amount The amount of date or time to be added to the field.
|
||||
* @param status Output param set to success/failure code on exit. If any value
|
||||
* previously set in the time field is invalid, this will be set to
|
||||
* an error status.
|
||||
*/
|
||||
virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode& status);
|
||||
/**
|
||||
* @deprecated ICU 2.6 use UCalendarDateFields instead of EDateFields
|
||||
*/
|
||||
inline virtual void add(EDateFields field, int32_t amount, UErrorCode& status) { add((UCalendarDateFields)field, amount, status); }
|
||||
|
||||
|
||||
/**
|
||||
* (Overrides Calendar) Rolls up or down by the given amount in the specified field.
|
||||
* For more information, see the documentation for Calendar::roll().
|
||||
*
|
||||
* @param field The time field.
|
||||
* @param amount Indicates amount to roll.
|
||||
* @param status Output param set to success/failure code on exit. If any value
|
||||
* previously set in the time field is invalid, this will be set to
|
||||
* an error status.
|
||||
* @internal
|
||||
*/
|
||||
virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode& status);
|
||||
|
||||
/**
|
||||
* (Overrides Calendar) Rolls up or down by the given amount in the specified field.
|
||||
* For more information, see the documentation for Calendar::roll().
|
||||
*
|
||||
* @param field The time field.
|
||||
* @param amount Indicates amount to roll.
|
||||
* @param status Output param set to success/failure code on exit. If any value
|
||||
* previously set in the time field is invalid, this will be set to
|
||||
* an error status.
|
||||
* @deprecated ICU 2.6. Use roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) instead.
|
||||
` */
|
||||
virtual void roll(EDateFields field, int32_t amount, UErrorCode& status);
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* Subclass API for defining limits of different types.
|
||||
* Subclasses must implement this method to return limits for the
|
||||
* following fields:
|
||||
*
|
||||
* <pre>UCAL_ERA
|
||||
* UCAL_YEAR
|
||||
* UCAL_MONTH
|
||||
* UCAL_WEEK_OF_YEAR
|
||||
* UCAL_WEEK_OF_MONTH
|
||||
* UCAL_DATE (DAY_OF_MONTH on Java)
|
||||
* UCAL_DAY_OF_YEAR
|
||||
* UCAL_DAY_OF_WEEK_IN_MONTH
|
||||
* UCAL_YEAR_WOY
|
||||
* UCAL_EXTENDED_YEAR</pre>
|
||||
*
|
||||
* @param field one of the above field numbers
|
||||
* @param limitType one of <code>MINIMUM</code>, <code>GREATEST_MINIMUM</code>,
|
||||
* <code>LEAST_MAXIMUM</code>, or <code>MAXIMUM</code>
|
||||
* @internal
|
||||
*/
|
||||
virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const;
|
||||
|
||||
/**
|
||||
* Return the number of days in the given month of the given extended
|
||||
* year of this calendar system. Subclasses should override this
|
||||
* method if they can provide a more correct or more efficient
|
||||
* implementation than the default implementation in Calendar.
|
||||
* @internal
|
||||
*/
|
||||
virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const;
|
||||
|
||||
/**
|
||||
* Return the number of days in the given extended year of this
|
||||
* calendar system. Subclasses should override this method if they can
|
||||
* provide a more correct or more efficient implementation than the
|
||||
* default implementation in Calendar.
|
||||
* @stable ICU 2.0
|
||||
*/
|
||||
virtual int32_t handleGetYearLength(int32_t eyear) const;
|
||||
/**
|
||||
* Subclasses may override this method to compute several fields
|
||||
* specific to each calendar system. These are:
|
||||
*
|
||||
* <ul><li>ERA
|
||||
* <li>YEAR
|
||||
* <li>MONTH
|
||||
* <li>DAY_OF_MONTH
|
||||
* <li>DAY_OF_YEAR
|
||||
* <li>EXTENDED_YEAR</ul>
|
||||
*
|
||||
* <p>The GregorianCalendar implementation implements
|
||||
* a calendar with the specified Julian/Gregorian cutover date.
|
||||
* @internal
|
||||
*/
|
||||
virtual void handleComputeFields(int32_t julianDay, UErrorCode &status);
|
||||
/**
|
||||
* Return the extended year defined by the current fields. This will
|
||||
* use the UCAL_EXTENDED_YEAR field or the UCAL_YEAR and supra-year fields (such
|
||||
* as UCAL_ERA) specific to the calendar system, depending on which set of
|
||||
* fields is newer.
|
||||
* @return the extended year
|
||||
* @internal
|
||||
*/
|
||||
virtual int32_t handleGetExtendedYear();
|
||||
/**
|
||||
* Return the Julian day number of day before the first day of the
|
||||
* given month in the given extended year. Subclasses should override
|
||||
* this method to implement their calendar system.
|
||||
* @param eyear the extended year
|
||||
* @param month the zero-based month, or 0 if useMonth is false
|
||||
* @param useMonth if false, compute the day before the first day of
|
||||
* the given year, otherwise, compute the day before the first day of
|
||||
* the given month
|
||||
* @param return the Julian day number of the day before the first
|
||||
* day of the given month and year
|
||||
* @internal
|
||||
*/
|
||||
virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month,
|
||||
UBool useMonth) const;
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* (Overrides Calendar) Return true if the current date for this Calendar is in
|
||||
* Daylight Savings Time. Recognizes DST_OFFSET, if it is set.
|
||||
*
|
||||
* @param status Fill-in parameter which receives the status of this operation.
|
||||
* @return True if the current date for this Calendar is in Daylight Savings Time,
|
||||
* false, otherwise.
|
||||
* @internal
|
||||
*/
|
||||
virtual UBool inDaylightTime(UErrorCode& status) const;
|
||||
|
||||
/**
|
||||
* Returns TRUE because the Hebrew Calendar does have a default century
|
||||
* @internal
|
||||
*/
|
||||
virtual UBool haveDefaultCentury() const;
|
||||
|
||||
/**
|
||||
* Returns the date of the start of the default century
|
||||
* @return start of century - in milliseconds since epoch, 1970
|
||||
* @internal
|
||||
*/
|
||||
virtual UDate defaultCenturyStart() const;
|
||||
|
||||
/**
|
||||
* Returns the year in which the default century begins
|
||||
* @internal
|
||||
*/
|
||||
virtual int32_t defaultCenturyStartYear() const;
|
||||
|
||||
private: // default century stuff.
|
||||
/**
|
||||
* The system maintains a static default century start date. This is initialized
|
||||
* the first time it is used. Before then, it is set to SYSTEM_DEFAULT_CENTURY to
|
||||
* indicate an uninitialized state. Once the system default century date and year
|
||||
* are set, they do not change.
|
||||
*/
|
||||
static UDate fgSystemDefaultCenturyStart;
|
||||
|
||||
/**
|
||||
* See documentation for systemDefaultCenturyStart.
|
||||
*/
|
||||
static int32_t fgSystemDefaultCenturyStartYear;
|
||||
|
||||
/**
|
||||
* Default value that indicates the defaultCenturyStartYear is unitialized
|
||||
*/
|
||||
static const int32_t fgSystemDefaultCenturyYear;
|
||||
|
||||
/**
|
||||
* start of default century, as a date
|
||||
*/
|
||||
static const UDate fgSystemDefaultCentury;
|
||||
|
||||
/**
|
||||
* Returns the beginning date of the 100-year window that dates
|
||||
* with 2-digit years are considered to fall within.
|
||||
*/
|
||||
UDate internalGetDefaultCenturyStart(void) const;
|
||||
|
||||
/**
|
||||
* Returns the first year of the 100-year window that dates with
|
||||
* 2-digit years are considered to fall within.
|
||||
*/
|
||||
int32_t internalGetDefaultCenturyStartYear(void) const;
|
||||
|
||||
/**
|
||||
* Initializes the 100-year window that dates with 2-digit years
|
||||
* are considered to fall within so that its start date is 80 years
|
||||
* before the current time.
|
||||
*/
|
||||
static void initializeSystemDefaultCentury(void);
|
||||
|
||||
private: // Calendar-specific implementation
|
||||
/**
|
||||
* Finds the day # of the first day in the given Hebrew year.
|
||||
* To do this, we want to calculate the time of the Tishri 1 new moon
|
||||
* in that year.
|
||||
* <p>
|
||||
* The algorithm here is similar to ones described in a number of
|
||||
* references, including:
|
||||
* <ul>
|
||||
* <li>"Calendrical Calculations", by Nachum Dershowitz & Edward Reingold,
|
||||
* Cambridge University Press, 1997, pages 85-91.
|
||||
*
|
||||
* <li>Hebrew Calendar Science and Myths,
|
||||
* <a href="http://www.geocities.com/Athens/1584/">
|
||||
* http://www.geocities.com/Athens/1584/</a>
|
||||
*
|
||||
* <li>The Calendar FAQ,
|
||||
* <a href="http://www.faqs.org/faqs/calendars/faq/">
|
||||
* http://www.faqs.org/faqs/calendars/faq/</a>
|
||||
* </ul>
|
||||
* @param year extended year
|
||||
* @return day number (JD)
|
||||
* @internal
|
||||
*/
|
||||
static int32_t startOfYear(int32_t year, UErrorCode& status);
|
||||
|
||||
static int32_t absoluteDayToDayOfWeek(int32_t day) ;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
int32_t yearType(int32_t year) const;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
static UBool isLeapYear(int year) ;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
static int monthsInYear(int year) ;
|
||||
};
|
||||
|
||||
U_NAMESPACE_END
|
||||
|
||||
#endif /* #if !UCONFIG_NO_FORMATTING */
|
||||
|
||||
#endif // _GREGOCAL
|
||||
//eof
|
||||
|
|
@ -698,6 +698,22 @@ SOURCE=.\calendar.cpp
|
|||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\chnsecal.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\chnsecal.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\hebrwcal.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\hebrwcal.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\unicode\calendar.h
|
||||
|
||||
!IF "$(CFG)" == "i18n - Win32 Release"
|
||||
|
|
|
@ -584,12 +584,24 @@
|
|||
Outputs="..\..\include\unicode\$(InputFileName)"/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\chnsecal.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\chnsecal.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\gregoimp.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\gregoimp.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\hebrwcal.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\hebrwcal.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\islamcal.cpp">
|
||||
</File>
|
||||
|
|
|
@ -51,55 +51,7 @@ static void debug_islamcal_msg(const char *pat, ...)
|
|||
// --- The cache --
|
||||
// cache of months
|
||||
static UMTX astroLock = 0; // pod bay door lock
|
||||
static UHashtable *monthCache = NULL;
|
||||
|
||||
static void createCache(UErrorCode &success) {
|
||||
if(monthCache == NULL) {
|
||||
monthCache = uhash_openSize(uhash_hashLong, uhash_compareLong, 32, &success);
|
||||
}
|
||||
ucln_i18n_registerCleanup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the cache, clean up.
|
||||
* called by calendar_islamic_cleanup()
|
||||
*/
|
||||
static void deleteCache() {
|
||||
if(monthCache != NULL) {
|
||||
uhash_close(monthCache);
|
||||
monthCache = NULL;
|
||||
}
|
||||
umtx_destroy(&astroLock);
|
||||
astroLock = NULL;
|
||||
}
|
||||
|
||||
static void putCache(int32_t month, int32_t start, UErrorCode &status) {
|
||||
umtx_lock(&astroLock);
|
||||
uhash_puti(monthCache, (void*)month, start, &status);
|
||||
U_DEBUG_ISLAMCAL_MSG(("Add: m%d, st%d\n", month, start));
|
||||
umtx_unlock(&astroLock);
|
||||
}
|
||||
|
||||
static int32_t getCache(int32_t month, UBool& found, UErrorCode &success) {
|
||||
int32_t res = 0;
|
||||
|
||||
umtx_lock(&astroLock);
|
||||
if(monthCache == NULL) {
|
||||
createCache(success);
|
||||
}
|
||||
|
||||
res = uhash_geti(monthCache, (void*)month);
|
||||
|
||||
umtx_unlock(&astroLock);
|
||||
if(res!=0) {
|
||||
found=TRUE;
|
||||
} else {
|
||||
found=FALSE;
|
||||
}
|
||||
|
||||
U_DEBUG_ISLAMCAL_MSG(("Get: m%d, res%d, found%s\n", month, res, found?"Y":"N"));
|
||||
return res;
|
||||
}
|
||||
static CalendarCache *monthCache = NULL;
|
||||
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
|
@ -253,11 +205,10 @@ int32_t IslamicCalendar::monthStart(int32_t year, int32_t month) const {
|
|||
*/
|
||||
int32_t IslamicCalendar::trueMonthStart(int32_t month) const
|
||||
{
|
||||
UBool found;
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
int32_t start = getCache(month, found, status);
|
||||
int32_t start = CalendarCache::get(&monthCache, month, status);
|
||||
|
||||
if (found==FALSE) {
|
||||
if (start==0) {
|
||||
// Make a guess at when the month started, using the average length
|
||||
UDate origin = HIJRA_MILLIS
|
||||
+ uprv_floor(month * CalendarAstronomer::SYNODIC_MONTH - 1) * kOneDay;
|
||||
|
@ -279,7 +230,7 @@ int32_t IslamicCalendar::trueMonthStart(int32_t month) const
|
|||
} while (age < 0);
|
||||
}
|
||||
start = (int32_t)Math::floorDivide((origin - HIJRA_MILLIS), (double)kOneDay) + 1;
|
||||
putCache(month, start, status);
|
||||
CalendarCache::put(&monthCache, month, start, status);
|
||||
}
|
||||
if(U_FAILURE(status)) {
|
||||
start = 0;
|
||||
|
@ -560,7 +511,8 @@ UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IslamicCalendar)
|
|||
U_NAMESPACE_END
|
||||
|
||||
U_CFUNC UBool calendar_islamic_cleanup(void) {
|
||||
deleteCache();
|
||||
delete monthCache;
|
||||
monthCache = NULL;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue