2003-05-16 22:05:41 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2004-05-19 20:42:44 +00:00
|
|
|
* Copyright (C) 2003-2004, International Business Machines Corporation and *
|
2003-05-16 22:05:41 +00:00
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* File BUDDHCAL.CPP
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
* 05/13/2003 srl copied from gregocal.cpp
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
#include "buddhcal.h"
|
|
|
|
#include "unicode/gregocal.h"
|
2003-05-29 23:01:03 +00:00
|
|
|
#include "mutex.h"
|
2003-05-28 05:40:04 +00:00
|
|
|
#include <float.h>
|
2003-05-16 22:05:41 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(BuddhistCalendar)
|
2003-05-16 22:05:41 +00:00
|
|
|
|
|
|
|
static const int32_t kMaxEra = 0; // only 1 era
|
|
|
|
|
|
|
|
static const int32_t kBuddhistEraStart = -543; // 544 BC (Gregorian)
|
|
|
|
|
2003-05-25 07:12:19 +00:00
|
|
|
static const int32_t kGregorianEpoch = 1970;
|
|
|
|
|
2003-05-16 22:05:41 +00:00
|
|
|
BuddhistCalendar::BuddhistCalendar(const Locale& aLocale, UErrorCode& success)
|
|
|
|
: GregorianCalendar(aLocale, success)
|
|
|
|
{
|
2003-06-10 03:58:41 +00:00
|
|
|
setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
|
2003-05-16 22:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BuddhistCalendar::~BuddhistCalendar()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BuddhistCalendar::BuddhistCalendar(const BuddhistCalendar& source)
|
|
|
|
: GregorianCalendar(source)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BuddhistCalendar& BuddhistCalendar::operator= ( const BuddhistCalendar& right)
|
|
|
|
{
|
|
|
|
GregorianCalendar::operator=(right);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Calendar* BuddhistCalendar::clone(void) const
|
|
|
|
{
|
|
|
|
return new BuddhistCalendar(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *BuddhistCalendar::getType() const
|
|
|
|
{
|
|
|
|
return "buddhist";
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
BuddhistCalendar::getMaximum(UCalendarDateFields field) const
|
|
|
|
{
|
|
|
|
if(field == UCAL_ERA) {
|
|
|
|
return kMaxEra;
|
|
|
|
} else {
|
|
|
|
return GregorianCalendar::getMaximum(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
BuddhistCalendar::getLeastMaximum(UCalendarDateFields field) const
|
|
|
|
{
|
|
|
|
if(field == UCAL_ERA) {
|
|
|
|
return kMaxEra;
|
|
|
|
} else {
|
|
|
|
return GregorianCalendar::getLeastMaximum(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
BuddhistCalendar::monthLength(int32_t month, int32_t year) const
|
|
|
|
{
|
|
|
|
return GregorianCalendar::monthLength(month,year);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
BuddhistCalendar::monthLength(int32_t month) const
|
|
|
|
{
|
2003-06-02 18:46:41 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2003-05-16 22:05:41 +00:00
|
|
|
// ignore era
|
2003-06-02 18:46:41 +00:00
|
|
|
return GregorianCalendar::monthLength(month, getGregorianYear(status));
|
2003-05-16 22:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t BuddhistCalendar::internalGetEra() const
|
|
|
|
{
|
2003-10-26 10:24:58 +00:00
|
|
|
return internalGet(UCAL_ERA, BE);
|
2003-05-16 22:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
2003-05-25 07:12:19 +00:00
|
|
|
BuddhistCalendar::getGregorianYear(UErrorCode &status) const
|
2003-05-16 22:05:41 +00:00
|
|
|
{
|
2003-05-25 07:12:19 +00:00
|
|
|
int32_t year = (fStamp[UCAL_YEAR] != kUnset) ? internalGet(UCAL_YEAR) : kGregorianEpoch+kBuddhistEraStart;
|
2003-05-16 22:05:41 +00:00
|
|
|
int32_t era = BE;
|
|
|
|
if (fStamp[UCAL_ERA] != kUnset) {
|
|
|
|
era = internalGet(UCAL_ERA);
|
|
|
|
if (era != BE) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2003-05-25 07:12:19 +00:00
|
|
|
return kGregorianEpoch + kBuddhistEraStart;
|
2003-05-16 22:05:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return year + kBuddhistEraStart;
|
|
|
|
}
|
|
|
|
|
2003-10-26 10:24:58 +00:00
|
|
|
int32_t BuddhistCalendar::handleGetExtendedYear()
|
|
|
|
{
|
|
|
|
int32_t year;
|
|
|
|
if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
|
|
|
|
year = internalGet(UCAL_EXTENDED_YEAR, 1);
|
|
|
|
} else {
|
|
|
|
// Ignore the era, as there is only one
|
|
|
|
year = internalGet(UCAL_YEAR, 1);
|
|
|
|
}
|
|
|
|
return year;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t BuddhistCalendar::handleComputeMonthStart(int32_t eyear, int32_t month,
|
|
|
|
|
|
|
|
UBool useMonth) const
|
|
|
|
{
|
|
|
|
return GregorianCalendar::handleComputeMonthStart(eyear+kBuddhistEraStart, month, useMonth);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BuddhistCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)
|
|
|
|
{
|
|
|
|
GregorianCalendar::handleComputeFields(julianDay, status);
|
|
|
|
int32_t y = internalGet(UCAL_EXTENDED_YEAR) - kBuddhistEraStart;
|
|
|
|
internalSet(UCAL_EXTENDED_YEAR, y);
|
|
|
|
internalSet(UCAL_ERA, 0);
|
|
|
|
internalSet(UCAL_YEAR, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t BuddhistCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
|
|
|
|
{
|
|
|
|
if(field == UCAL_ERA) {
|
|
|
|
return BE;
|
|
|
|
} else {
|
|
|
|
return GregorianCalendar::handleGetLimit(field,limitType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2003-05-16 22:05:41 +00:00
|
|
|
void BuddhistCalendar::timeToFields(UDate theTime, UBool quick, UErrorCode& status)
|
|
|
|
{
|
2003-10-26 10:24:58 +00:00
|
|
|
//Calendar::timeToFields(theTime, quick, status);
|
2003-05-16 22:05:41 +00:00
|
|
|
|
|
|
|
int32_t era = internalGet(UCAL_ERA);
|
|
|
|
int32_t year = internalGet(UCAL_YEAR);
|
|
|
|
|
|
|
|
if(era == GregorianCalendar::BC) {
|
|
|
|
year = 1-year;
|
|
|
|
era = BuddhistCalendar::BE;
|
|
|
|
} else if(era == GregorianCalendar::AD) {
|
|
|
|
era = BuddhistCalendar::BE;
|
|
|
|
} else {
|
|
|
|
status = U_INTERNAL_PROGRAM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
year = year - kBuddhistEraStart;
|
|
|
|
|
|
|
|
internalSet(UCAL_ERA, era);
|
|
|
|
internalSet(UCAL_YEAR, year);
|
|
|
|
}
|
2003-10-26 10:24:58 +00:00
|
|
|
#endif
|
2003-05-16 22:05:41 +00:00
|
|
|
|
2003-05-28 05:40:04 +00:00
|
|
|
void BuddhistCalendar::add(UCalendarDateFields field, int32_t amount, UErrorCode& status)
|
|
|
|
{
|
|
|
|
if (U_FAILURE(status))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (amount == 0)
|
|
|
|
return; // Do nothing!
|
|
|
|
|
|
|
|
if(field == UCAL_YEAR /* || field == UCAL_YEAR_WOY */) {
|
2004-05-27 20:36:52 +00:00
|
|
|
int32_t year = get(field, status); // not internalGet -- force completion
|
2003-05-28 05:40:04 +00:00
|
|
|
|
|
|
|
year += amount;
|
|
|
|
|
|
|
|
set(field,year);
|
|
|
|
pinDayOfMonth();
|
|
|
|
} else {
|
|
|
|
GregorianCalendar::add(field,amount,status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// default century
|
|
|
|
const UDate BuddhistCalendar::fgSystemDefaultCentury = DBL_MIN;
|
|
|
|
const int32_t BuddhistCalendar::fgSystemDefaultCenturyYear = -1;
|
|
|
|
|
|
|
|
UDate BuddhistCalendar::fgSystemDefaultCenturyStart = DBL_MIN;
|
|
|
|
int32_t BuddhistCalendar::fgSystemDefaultCenturyStartYear = -1;
|
|
|
|
|
|
|
|
|
2003-05-25 07:12:19 +00:00
|
|
|
UBool BuddhistCalendar::haveDefaultCentury() const
|
|
|
|
{
|
2003-05-28 05:40:04 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UDate BuddhistCalendar::defaultCenturyStart() const
|
|
|
|
{
|
|
|
|
return internalGetDefaultCenturyStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t BuddhistCalendar::defaultCenturyStartYear() const
|
|
|
|
{
|
|
|
|
return internalGetDefaultCenturyStartYear();
|
|
|
|
}
|
|
|
|
|
|
|
|
UDate
|
|
|
|
BuddhistCalendar::internalGetDefaultCenturyStart() const
|
|
|
|
{
|
|
|
|
// lazy-evaluate systemDefaultCenturyStart
|
2003-05-29 23:01:03 +00:00
|
|
|
UBool needsUpdate;
|
|
|
|
{
|
|
|
|
Mutex m;
|
|
|
|
needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needsUpdate) {
|
2003-05-28 05:40:04 +00:00
|
|
|
initializeSystemDefaultCentury();
|
2003-05-29 23:01:03 +00:00
|
|
|
}
|
|
|
|
|
2003-05-28 05:40:04 +00:00
|
|
|
// use defaultCenturyStart unless it's the flag value;
|
|
|
|
// then use systemDefaultCenturyStart
|
|
|
|
|
|
|
|
return fgSystemDefaultCenturyStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
BuddhistCalendar::internalGetDefaultCenturyStartYear() const
|
|
|
|
{
|
2003-05-29 23:01:03 +00:00
|
|
|
// lazy-evaluate systemDefaultCenturyStartYear
|
|
|
|
UBool needsUpdate;
|
|
|
|
{
|
|
|
|
Mutex m;
|
|
|
|
needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury);
|
|
|
|
}
|
2003-05-28 05:40:04 +00:00
|
|
|
|
2003-05-29 23:01:03 +00:00
|
|
|
if (needsUpdate) {
|
|
|
|
initializeSystemDefaultCentury();
|
|
|
|
}
|
2003-05-28 05:40:04 +00:00
|
|
|
|
2003-05-29 23:01:03 +00:00
|
|
|
// use defaultCenturyStart unless it's the flag value;
|
|
|
|
// then use systemDefaultCenturyStartYear
|
|
|
|
|
|
|
|
return fgSystemDefaultCenturyStartYear;
|
2003-05-28 05:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BuddhistCalendar::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;
|
2004-09-10 04:57:10 +00:00
|
|
|
BuddhistCalendar calendar(Locale("@calendar=buddhist"),status);
|
2004-04-14 17:47:13 +00:00
|
|
|
if (U_SUCCESS(status))
|
2003-05-28 05:40:04 +00:00
|
|
|
{
|
2004-04-14 17:47:13 +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);
|
2003-05-29 23:01:03 +00:00
|
|
|
{
|
|
|
|
Mutex m;
|
|
|
|
fgSystemDefaultCenturyStart = newStart;
|
|
|
|
fgSystemDefaultCenturyStartYear = newYear;
|
|
|
|
}
|
2003-05-28 05:40:04 +00:00
|
|
|
}
|
|
|
|
// We have no recourse upon failure unless we want to propagate the failure
|
|
|
|
// out.
|
|
|
|
}
|
2003-05-25 07:12:19 +00:00
|
|
|
}
|
2003-05-16 22:05:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|