2003-11-24 20:02:11 +00:00
|
|
|
/*
|
2006-09-08 04:02:23 +00:00
|
|
|
******************************************************************************
|
2011-05-04 12:23:42 +00:00
|
|
|
* Copyright (C) 2003-2011, International Business Machines Corporation
|
2006-09-08 04:02:23 +00:00
|
|
|
* and others. All Rights Reserved.
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* File ISLAMCAL.H
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 10/14/2003 srl ported from java IslamicCalendar
|
|
|
|
*****************************************************************************
|
|
|
|
*/
|
2003-11-24 20:02:11 +00:00
|
|
|
|
|
|
|
#include "islamcal.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
2006-09-08 04:02:23 +00:00
|
|
|
#include "umutex.h"
|
2003-11-24 20:02:11 +00:00
|
|
|
#include <float.h>
|
|
|
|
#include "gregoimp.h" // Math
|
|
|
|
#include "astro.h" // CalendarAstronomer
|
|
|
|
#include "uhash.h"
|
2003-12-03 22:46:40 +00:00
|
|
|
#include "ucln_in.h"
|
2003-11-24 20:02:11 +00:00
|
|
|
|
|
|
|
static const UDate HIJRA_MILLIS = -42521587200000.0; // 7/16/622 AD 00:00
|
|
|
|
|
|
|
|
// Debugging
|
|
|
|
#ifdef U_DEBUG_ISLAMCAL
|
|
|
|
# include <stdio.h>
|
|
|
|
# include <stdarg.h>
|
|
|
|
static void debug_islamcal_loc(const char *f, int32_t l)
|
|
|
|
{
|
2006-09-08 04:02:23 +00:00
|
|
|
fprintf(stderr, "%s:%d: ", f, l);
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void debug_islamcal_msg(const char *pat, ...)
|
|
|
|
{
|
2006-09-08 04:02:23 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, pat);
|
|
|
|
vfprintf(stderr, pat, ap);
|
|
|
|
fflush(stderr);
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
// must use double parens, i.e.: U_DEBUG_ISLAMCAL_MSG(("four is: %d",4));
|
|
|
|
#define U_DEBUG_ISLAMCAL_MSG(x) {debug_islamcal_loc(__FILE__,__LINE__);debug_islamcal_msg x;}
|
|
|
|
#else
|
|
|
|
#define U_DEBUG_ISLAMCAL_MSG(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// --- The cache --
|
|
|
|
// cache of months
|
|
|
|
static UMTX astroLock = 0; // pod bay door lock
|
2011-07-06 04:03:35 +00:00
|
|
|
static icu::CalendarCache *gMonthCache = NULL;
|
|
|
|
static icu::CalendarAstronomer *gIslamicCalendarAstro = NULL;
|
2003-11-24 20:02:11 +00:00
|
|
|
|
2004-09-12 23:07:29 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
static UBool calendar_islamic_cleanup(void) {
|
|
|
|
if (gMonthCache) {
|
|
|
|
delete gMonthCache;
|
|
|
|
gMonthCache = NULL;
|
|
|
|
}
|
|
|
|
if (gIslamicCalendarAstro) {
|
|
|
|
delete gIslamicCalendarAstro;
|
|
|
|
gIslamicCalendarAstro = NULL;
|
|
|
|
}
|
|
|
|
umtx_destroy(&astroLock);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
U_CDECL_END
|
|
|
|
|
2003-11-24 20:02:11 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
// Implementation of the IslamicCalendar class
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Constructors...
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const char *IslamicCalendar::getType() const {
|
2006-09-08 04:02:23 +00:00
|
|
|
if(civil==CIVIL) {
|
|
|
|
return "islamic-civil";
|
|
|
|
} else {
|
|
|
|
return "islamic";
|
|
|
|
}
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Calendar* IslamicCalendar::clone() const {
|
2006-09-08 04:02:23 +00:00
|
|
|
return new IslamicCalendar(*this);
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IslamicCalendar::IslamicCalendar(const Locale& aLocale, UErrorCode& success, ECivil beCivil)
|
2006-09-08 04:02:23 +00:00
|
|
|
: Calendar(TimeZone::createDefault(), aLocale, success),
|
|
|
|
civil(beCivil)
|
2003-11-24 20:02:11 +00:00
|
|
|
{
|
2008-02-23 19:15:18 +00:00
|
|
|
setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IslamicCalendar::IslamicCalendar(const IslamicCalendar& other) : Calendar(other), civil(other.civil) {
|
|
|
|
}
|
|
|
|
|
|
|
|
IslamicCalendar::~IslamicCalendar()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-09-08 04:02:23 +00:00
|
|
|
* Determines whether this object uses the fixed-cycle Islamic civil calendar
|
|
|
|
* or an approximation of the religious, astronomical calendar.
|
|
|
|
*
|
|
|
|
* @param beCivil <code>true</code> to use the civil calendar,
|
|
|
|
* <code>false</code> to use the astronomical calendar.
|
|
|
|
* @draft ICU 2.4
|
|
|
|
*/
|
2003-11-24 20:02:11 +00:00
|
|
|
void IslamicCalendar::setCivil(ECivil beCivil, UErrorCode &status)
|
|
|
|
{
|
2006-09-08 04:02:23 +00:00
|
|
|
if (civil != beCivil) {
|
|
|
|
// The fields of the calendar will become invalid, because the calendar
|
|
|
|
// rules are different
|
|
|
|
UDate m = getTimeInMillis(status);
|
|
|
|
civil = beCivil;
|
|
|
|
clear();
|
|
|
|
setTimeInMillis(m, status);
|
|
|
|
}
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
2006-09-08 04:02:23 +00:00
|
|
|
|
2003-11-24 20:02:11 +00:00
|
|
|
/**
|
2006-09-08 04:02:23 +00:00
|
|
|
* Returns <code>true</code> if this object is using the fixed-cycle civil
|
|
|
|
* calendar, or <code>false</code> if using the religious, astronomical
|
|
|
|
* calendar.
|
|
|
|
* @draft ICU 2.4
|
|
|
|
*/
|
2003-11-24 20:02:11 +00:00
|
|
|
UBool IslamicCalendar::isCivil() {
|
2006-09-08 04:02:23 +00:00
|
|
|
return (civil == CIVIL);
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
2006-09-08 04:02:23 +00:00
|
|
|
|
2003-11-24 20:02:11 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Minimum / Maximum access functions
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2008-04-09 18:26:42 +00:00
|
|
|
// Note: Current IslamicCalendar implementation does not work
|
|
|
|
// well with negative years.
|
|
|
|
|
2011-05-04 12:23:42 +00:00
|
|
|
// TODO: In some cases the current ICU Islamic calendar implementation shows
|
|
|
|
// a month as having 31 days. Since date parsing now uses range checks based
|
|
|
|
// on the table below, we need to change the range for last day of month to
|
|
|
|
// include 31 as a workaround until the implementation is fixed.
|
2003-11-24 20:02:11 +00:00
|
|
|
static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = {
|
2006-09-08 04:02:23 +00:00
|
|
|
// Minimum Greatest Least Maximum
|
|
|
|
// Minimum Maximum
|
2008-04-09 18:26:42 +00:00
|
|
|
{ 0, 0, 0, 0}, // ERA
|
|
|
|
{ 1, 1, 5000000, 5000000}, // YEAR
|
|
|
|
{ 0, 0, 11, 11}, // MONTH
|
|
|
|
{ 1, 1, 50, 51}, // WEEK_OF_YEAR
|
|
|
|
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
|
2011-05-04 12:23:42 +00:00
|
|
|
{ 1, 1, 29, 31}, // DAY_OF_MONTH - 31 to workaround for cal implementation bug, should be 30
|
2008-04-09 18:26:42 +00:00
|
|
|
{ 1, 1, 354, 355}, // DAY_OF_YEAR
|
2006-09-08 04:02:23 +00:00
|
|
|
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
|
2008-04-09 18:26:42 +00:00
|
|
|
{ -1, -1, 5, 5}, // DAY_OF_WEEK_IN_MONTH
|
2006-09-08 04:02:23 +00:00
|
|
|
{/*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
|
2008-04-09 18:26:42 +00:00
|
|
|
{ 1, 1, 5000000, 5000000}, // YEAR_WOY
|
2006-09-08 04:02:23 +00:00
|
|
|
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
|
2008-04-09 18:26:42 +00:00
|
|
|
{ 1, 1, 5000000, 5000000}, // EXTENDED_YEAR
|
2006-09-08 04:02:23 +00:00
|
|
|
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
|
2007-10-25 20:26:34 +00:00
|
|
|
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
|
|
|
|
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH
|
2003-11-24 20:02:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2006-09-08 04:02:23 +00:00
|
|
|
* @draft ICU 2.4
|
|
|
|
*/
|
2003-11-24 20:02:11 +00:00
|
|
|
int32_t IslamicCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
|
2006-09-08 04:02:23 +00:00
|
|
|
return LIMITS[field][limitType];
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Assorted calculation utilities
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
2006-09-08 04:02:23 +00:00
|
|
|
* Determine whether a year is a leap year in the Islamic civil calendar
|
|
|
|
*/
|
2003-11-24 20:02:11 +00:00
|
|
|
UBool IslamicCalendar::civilLeapYear(int32_t year)
|
|
|
|
{
|
2006-09-08 04:02:23 +00:00
|
|
|
return (14 + 11 * year) % 30 < 11;
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
2006-09-08 04:02:23 +00:00
|
|
|
|
2003-11-24 20:02:11 +00:00
|
|
|
/**
|
2006-09-08 04:02:23 +00:00
|
|
|
* Return the day # on which the given year starts. Days are counted
|
|
|
|
* from the Hijri epoch, origin 0.
|
|
|
|
*/
|
2003-11-24 20:02:11 +00:00
|
|
|
int32_t IslamicCalendar::yearStart(int32_t year) {
|
2006-09-08 04:02:23 +00:00
|
|
|
if (civil == CIVIL) {
|
2008-09-25 20:24:14 +00:00
|
|
|
return (year-1)*354 + ClockMath::floorDivide((3+11*year),30);
|
2006-09-08 04:02:23 +00:00
|
|
|
} else {
|
|
|
|
return trueMonthStart(12*(year-1));
|
|
|
|
}
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
2006-09-08 04:02:23 +00:00
|
|
|
|
2003-11-24 20:02:11 +00:00
|
|
|
/**
|
2006-09-08 04:02:23 +00:00
|
|
|
* Return the day # on which the given month starts. Days are counted
|
|
|
|
* from the Hijri epoch, origin 0.
|
|
|
|
*
|
|
|
|
* @param year The hijri year
|
|
|
|
* @param year The hijri month, 0-based
|
|
|
|
*/
|
2003-11-24 20:02:11 +00:00
|
|
|
int32_t IslamicCalendar::monthStart(int32_t year, int32_t month) const {
|
2006-09-08 04:02:23 +00:00
|
|
|
if (civil == CIVIL) {
|
|
|
|
return (int32_t)uprv_ceil(29.5*month)
|
2008-09-25 20:24:14 +00:00
|
|
|
+ (year-1)*354 + (int32_t)ClockMath::floorDivide((3+11*year),30);
|
2006-09-08 04:02:23 +00:00
|
|
|
} else {
|
|
|
|
return trueMonthStart(12*(year-1) + month);
|
|
|
|
}
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
2006-09-08 04:02:23 +00:00
|
|
|
|
2003-11-24 20:02:11 +00:00
|
|
|
/**
|
2006-09-08 04:02:23 +00:00
|
|
|
* Find the day number on which a particular month of the true/lunar
|
|
|
|
* Islamic calendar starts.
|
|
|
|
*
|
|
|
|
* @param month The month in question, origin 0 from the Hijri epoch
|
|
|
|
*
|
|
|
|
* @return The day number on which the given month starts.
|
|
|
|
*/
|
2003-11-24 20:02:11 +00:00
|
|
|
int32_t IslamicCalendar::trueMonthStart(int32_t month) const
|
|
|
|
{
|
2006-09-08 04:02:23 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
int32_t start = CalendarCache::get(&gMonthCache, month, status);
|
|
|
|
|
|
|
|
if (start==0) {
|
|
|
|
// Make a guess at when the month started, using the average length
|
|
|
|
UDate origin = HIJRA_MILLIS
|
2010-02-25 00:03:49 +00:00
|
|
|
+ uprv_floor(month * CalendarAstronomer::SYNODIC_MONTH) * kOneDay;
|
2006-09-08 04:02:23 +00:00
|
|
|
|
2008-01-11 20:32:46 +00:00
|
|
|
// moonAge will fail due to memory allocation error
|
|
|
|
double age = moonAge(origin, status);
|
|
|
|
if (U_FAILURE(status)) {
|
2008-02-23 19:15:18 +00:00
|
|
|
goto trueMonthStartEnd;
|
2008-01-11 20:32:46 +00:00
|
|
|
}
|
2006-09-08 04:02:23 +00:00
|
|
|
|
2008-01-11 20:32:46 +00:00
|
|
|
if (age >= 0) {
|
2006-09-08 04:02:23 +00:00
|
|
|
// The month has already started
|
|
|
|
do {
|
|
|
|
origin -= kOneDay;
|
2008-01-11 20:32:46 +00:00
|
|
|
age = moonAge(origin, status);
|
|
|
|
if (U_FAILURE(status)) {
|
2008-02-23 19:15:18 +00:00
|
|
|
goto trueMonthStartEnd;
|
2008-01-11 20:32:46 +00:00
|
|
|
}
|
2006-09-08 04:02:23 +00:00
|
|
|
} while (age >= 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Preceding month has not ended yet.
|
|
|
|
do {
|
|
|
|
origin += kOneDay;
|
2008-01-11 20:32:46 +00:00
|
|
|
age = moonAge(origin, status);
|
|
|
|
if (U_FAILURE(status)) {
|
2008-02-23 19:15:18 +00:00
|
|
|
goto trueMonthStartEnd;
|
2008-01-11 20:32:46 +00:00
|
|
|
}
|
2006-09-08 04:02:23 +00:00
|
|
|
} while (age < 0);
|
|
|
|
}
|
2008-09-25 20:24:14 +00:00
|
|
|
start = (int32_t)ClockMath::floorDivide((origin - HIJRA_MILLIS), (double)kOneDay) + 1;
|
2006-09-08 04:02:23 +00:00
|
|
|
CalendarCache::put(&gMonthCache, month, start, status);
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
2008-01-11 20:32:46 +00:00
|
|
|
trueMonthStartEnd :
|
2006-09-08 04:02:23 +00:00
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
start = 0;
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
2006-09-08 04:02:23 +00:00
|
|
|
return start;
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-09-08 04:02:23 +00:00
|
|
|
* Return the "age" of the moon at the given time; this is the difference
|
|
|
|
* in ecliptic latitude between the moon and the sun. This method simply
|
|
|
|
* calls CalendarAstronomer.moonAge, converts to degrees,
|
|
|
|
* and adjusts the result to be in the range [-180, 180].
|
|
|
|
*
|
|
|
|
* @param time The time at which the moon's age is desired,
|
|
|
|
* in millis since 1/1/1970.
|
|
|
|
*/
|
2008-01-11 20:32:46 +00:00
|
|
|
double IslamicCalendar::moonAge(UDate time, UErrorCode &status)
|
2003-11-24 20:02:11 +00:00
|
|
|
{
|
2006-09-08 04:02:23 +00:00
|
|
|
double age = 0;
|
|
|
|
|
|
|
|
umtx_lock(&astroLock);
|
|
|
|
if(gIslamicCalendarAstro == NULL) {
|
|
|
|
gIslamicCalendarAstro = new CalendarAstronomer();
|
2008-01-11 20:32:46 +00:00
|
|
|
if (gIslamicCalendarAstro == NULL) {
|
2008-02-23 19:15:18 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return age;
|
2008-01-11 20:32:46 +00:00
|
|
|
}
|
2007-10-25 20:26:34 +00:00
|
|
|
ucln_i18n_registerCleanup(UCLN_I18N_ISLAMIC_CALENDAR, calendar_islamic_cleanup);
|
2006-09-08 04:02:23 +00:00
|
|
|
}
|
|
|
|
gIslamicCalendarAstro->setTime(time);
|
|
|
|
age = gIslamicCalendarAstro->getMoonAge();
|
|
|
|
umtx_unlock(&astroLock);
|
|
|
|
|
|
|
|
// Convert to degrees and normalize...
|
|
|
|
age = age * 180 / CalendarAstronomer::PI;
|
|
|
|
if (age > 180) {
|
|
|
|
age = age - 360;
|
|
|
|
}
|
|
|
|
|
|
|
|
return age;
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Calendar framework
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2006-09-08 04:02:23 +00:00
|
|
|
* Return the length (in days) of the given month.
|
|
|
|
*
|
|
|
|
* @param year The hijri year
|
|
|
|
* @param year The hijri month, 0-based
|
|
|
|
* @draft ICU 2.4
|
|
|
|
*/
|
2003-11-24 20:02:11 +00:00
|
|
|
int32_t IslamicCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const {
|
|
|
|
|
2006-09-08 04:02:23 +00:00
|
|
|
int32_t length = 0;
|
|
|
|
|
|
|
|
if (civil == CIVIL) {
|
|
|
|
length = 29 + (month+1) % 2;
|
|
|
|
if (month == DHU_AL_HIJJAH && civilLeapYear(extendedYear)) {
|
|
|
|
length++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
month = 12*(extendedYear-1) + month;
|
|
|
|
length = trueMonthStart(month+1) - trueMonthStart(month) ;
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
2006-09-08 04:02:23 +00:00
|
|
|
return length;
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-09-08 04:02:23 +00:00
|
|
|
* Return the number of days in the given Islamic year
|
|
|
|
* @draft ICU 2.4
|
|
|
|
*/
|
2003-11-24 20:02:11 +00:00
|
|
|
int32_t IslamicCalendar::handleGetYearLength(int32_t extendedYear) const {
|
2006-09-08 04:02:23 +00:00
|
|
|
if (civil == CIVIL) {
|
|
|
|
return 354 + (civilLeapYear(extendedYear) ? 1 : 0);
|
|
|
|
} else {
|
|
|
|
int32_t month = 12*(extendedYear-1);
|
|
|
|
return (trueMonthStart(month + 12) - trueMonthStart(month));
|
|
|
|
}
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
2006-09-08 04:02:23 +00:00
|
|
|
|
2003-11-24 20:02:11 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Functions for converting from field values to milliseconds....
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Return JD of start of given month/year
|
|
|
|
/**
|
2006-09-08 04:02:23 +00:00
|
|
|
* @draft ICU 2.4
|
|
|
|
*/
|
2003-11-24 22:50:45 +00:00
|
|
|
int32_t IslamicCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /* useMonth */) const {
|
2006-09-08 04:02:23 +00:00
|
|
|
return monthStart(eyear, month) + 1948439;
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Functions for converting from milliseconds to field values
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2006-09-08 04:02:23 +00:00
|
|
|
/**
|
|
|
|
* @draft ICU 2.4
|
|
|
|
*/
|
2003-11-24 20:02:11 +00:00
|
|
|
int32_t IslamicCalendar::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-09-08 04:02:23 +00:00
|
|
|
* Override Calendar to compute several fields specific to the Islamic
|
|
|
|
* calendar system. These are:
|
|
|
|
*
|
|
|
|
* <ul><li>ERA
|
|
|
|
* <li>YEAR
|
|
|
|
* <li>MONTH
|
|
|
|
* <li>DAY_OF_MONTH
|
|
|
|
* <li>DAY_OF_YEAR
|
|
|
|
* <li>EXTENDED_YEAR</ul>
|
|
|
|
*
|
|
|
|
* The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
|
|
|
|
* method is called. The getGregorianXxx() methods return Gregorian
|
|
|
|
* calendar equivalents for the given Julian day.
|
|
|
|
* @draft ICU 2.4
|
|
|
|
*/
|
2008-01-11 20:32:46 +00:00
|
|
|
void IslamicCalendar::handleComputeFields(int32_t julianDay, UErrorCode &status) {
|
2003-11-24 20:02:11 +00:00
|
|
|
int32_t year, month, dayOfMonth, dayOfYear;
|
|
|
|
UDate startDate;
|
|
|
|
int32_t days = julianDay - 1948440;
|
2006-09-08 04:02:23 +00:00
|
|
|
|
2003-11-24 20:02:11 +00:00
|
|
|
if (civil == CIVIL) {
|
|
|
|
// Use the civil calendar approximation, which is just arithmetic
|
2008-09-25 20:24:14 +00:00
|
|
|
year = (int)ClockMath::floorDivide( (double)(30 * days + 10646) , 10631.0 );
|
2003-11-24 20:02:11 +00:00
|
|
|
month = (int32_t)uprv_ceil((days - 29 - yearStart(year)) / 29.5 );
|
|
|
|
month = month<11?month:11;
|
|
|
|
startDate = monthStart(year, month);
|
|
|
|
} else {
|
|
|
|
// Guess at the number of elapsed full months since the epoch
|
|
|
|
int32_t months = (int32_t)uprv_floor((double)days / CalendarAstronomer::SYNODIC_MONTH);
|
|
|
|
|
2010-02-25 00:03:49 +00:00
|
|
|
startDate = uprv_floor(months * CalendarAstronomer::SYNODIC_MONTH);
|
2003-11-24 20:02:11 +00:00
|
|
|
|
2008-01-11 20:32:46 +00:00
|
|
|
double age = moonAge(internalGetTime(), status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2010-02-25 00:03:49 +00:00
|
|
|
if ( days - startDate >= 25 && age > 0) {
|
2003-11-24 20:02:11 +00:00
|
|
|
// If we're near the end of the month, assume next month and search backwards
|
|
|
|
months++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find out the last time that the new moon was actually visible at this longitude
|
|
|
|
// This returns midnight the night that the moon was visible at sunset.
|
|
|
|
while ((startDate = trueMonthStart(months)) > days) {
|
|
|
|
// If it was after the date in question, back up a month and try again
|
|
|
|
months--;
|
|
|
|
}
|
2006-09-08 04:02:23 +00:00
|
|
|
|
2003-11-24 20:02:11 +00:00
|
|
|
year = months / 12 + 1;
|
|
|
|
month = months % 12;
|
|
|
|
}
|
2006-09-08 04:02:23 +00:00
|
|
|
|
2003-11-24 20:02:11 +00:00
|
|
|
dayOfMonth = (days - monthStart(year, month)) + 1;
|
|
|
|
|
|
|
|
// Now figure out the day of the year.
|
|
|
|
dayOfYear = (days - monthStart(year, 0) + 1);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
IslamicCalendar::inDaylightTime(UErrorCode& status) const
|
|
|
|
{
|
2006-09-08 04:02:23 +00:00
|
|
|
// copied from GregorianCalendar
|
2008-01-11 20:32:46 +00:00
|
|
|
if (U_FAILURE(status) || (&(getTimeZone()) == NULL && !getTimeZone().useDaylightTime()))
|
2006-09-08 04:02:23 +00:00
|
|
|
return FALSE;
|
2003-11-24 20:02:11 +00:00
|
|
|
|
|
|
|
// Force an update of the state of the Calendar.
|
2006-09-08 04:02:23 +00:00
|
|
|
((IslamicCalendar*)this)->complete(status); // cast away const
|
2003-11-24 20:02:11 +00:00
|
|
|
|
2006-09-08 04:02:23 +00:00
|
|
|
return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE);
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// default century
|
|
|
|
const UDate IslamicCalendar::fgSystemDefaultCentury = DBL_MIN;
|
|
|
|
const int32_t IslamicCalendar::fgSystemDefaultCenturyYear = -1;
|
|
|
|
|
|
|
|
UDate IslamicCalendar::fgSystemDefaultCenturyStart = DBL_MIN;
|
|
|
|
int32_t IslamicCalendar::fgSystemDefaultCenturyStartYear = -1;
|
|
|
|
|
|
|
|
|
|
|
|
UBool IslamicCalendar::haveDefaultCentury() const
|
|
|
|
{
|
2006-09-08 04:02:23 +00:00
|
|
|
return TRUE;
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UDate IslamicCalendar::defaultCenturyStart() const
|
|
|
|
{
|
2006-09-08 04:02:23 +00:00
|
|
|
return internalGetDefaultCenturyStart();
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t IslamicCalendar::defaultCenturyStartYear() const
|
|
|
|
{
|
2006-09-08 04:02:23 +00:00
|
|
|
return internalGetDefaultCenturyStartYear();
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UDate
|
|
|
|
IslamicCalendar::internalGetDefaultCenturyStart() const
|
|
|
|
{
|
2006-09-08 04:02:23 +00:00
|
|
|
// lazy-evaluate systemDefaultCenturyStart
|
|
|
|
UBool needsUpdate;
|
|
|
|
UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate);
|
|
|
|
|
|
|
|
if (needsUpdate) {
|
|
|
|
initializeSystemDefaultCentury();
|
|
|
|
}
|
|
|
|
|
|
|
|
// use defaultCenturyStart unless it's the flag value;
|
|
|
|
// then use systemDefaultCenturyStart
|
|
|
|
|
|
|
|
return fgSystemDefaultCenturyStart;
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
IslamicCalendar::internalGetDefaultCenturyStartYear() const
|
|
|
|
{
|
2006-09-08 04:02:23 +00:00
|
|
|
// lazy-evaluate systemDefaultCenturyStartYear
|
|
|
|
UBool needsUpdate;
|
|
|
|
UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate);
|
|
|
|
|
|
|
|
if (needsUpdate) {
|
|
|
|
initializeSystemDefaultCentury();
|
|
|
|
}
|
|
|
|
|
|
|
|
// use defaultCenturyStart unless it's the flag value;
|
|
|
|
// then use systemDefaultCenturyStartYear
|
|
|
|
|
|
|
|
return fgSystemDefaultCenturyStartYear;
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IslamicCalendar::initializeSystemDefaultCentury()
|
|
|
|
{
|
2006-09-08 04:02:23 +00:00
|
|
|
// initialize systemDefaultCentury and systemDefaultCenturyYear based
|
|
|
|
// on the current time. They'll be set to 80 years before
|
|
|
|
// the current time.
|
2008-08-05 22:19:05 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
IslamicCalendar calendar(Locale("@calendar=islamic-civil"),status);
|
|
|
|
if (U_SUCCESS(status))
|
2003-11-24 20:02:11 +00:00
|
|
|
{
|
2008-08-05 22:19:05 +00:00
|
|
|
calendar.setTime(Calendar::getNow(), status);
|
|
|
|
calendar.add(UCAL_YEAR, -80, status);
|
|
|
|
UDate newStart = calendar.getTime(status);
|
|
|
|
int32_t newYear = calendar.get(UCAL_YEAR, status);
|
|
|
|
umtx_lock(NULL);
|
|
|
|
if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury)
|
2006-09-08 04:02:23 +00:00
|
|
|
{
|
2008-08-05 22:19:05 +00:00
|
|
|
fgSystemDefaultCenturyStartYear = newYear;
|
|
|
|
fgSystemDefaultCenturyStart = newStart;
|
2006-09-08 04:02:23 +00:00
|
|
|
}
|
2008-08-05 22:19:05 +00:00
|
|
|
umtx_unlock(NULL);
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
2008-08-05 22:19:05 +00:00
|
|
|
// We have no recourse upon failure unless we want to propagate the failure
|
|
|
|
// out.
|
2003-11-24 20:02:11 +00:00
|
|
|
}
|
|
|
|
|
2003-11-24 22:43:31 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IslamicCalendar)
|
|
|
|
|
2003-11-24 20:02:11 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|