1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
******************************************************************************
* Copyright (C) 2003-2013, International Business Machines Corporation
* and others. All Rights Reserved.
******************************************************************************
*
* File PERSNCAL.CPP
*
* Modification History:
*
* Date Name Description
* 9/23/2003 mehran posted to icu-design
* 10/1/2012 roozbeh Fixed algorithm and heavily refactored and rewrote
* based on the implementation of Gregorian
*****************************************************************************
*/
#include "persncal.h"
#if !UCONFIG_NO_FORMATTING
#include "uassert.h"
#include "umutex.h"
#include "gregoimp.h" // Math
#include <float.h>
#include "cmemory.h"
#include "ucln_in.h"
#include "unicode/uniset.h"
static const int16_t kPersianNumDays[]
= {0,31,62,93,124,155,186,216,246,276,306,336}; // 0-based, for day-in-year
static const int8_t kPersianMonthLength[]
= {31,31,31,31,31,31,30,30,30,30,30,29}; // 0-based
static const int8_t kPersianLeapMonthLength[]
= {31,31,31,31,31,31,30,30,30,30,30,30}; // 0-based
static const int32_t kPersianCalendarLimits[UCAL_FIELD_COUNT][4] = {
// Minimum Greatest Least Maximum
// Minimum Maximum
{ 0, 0, 0, 0}, // ERA
{ -5000000, -5000000, 5000000, 5000000}, // YEAR
{ 0, 0, 11, 11}, // MONTH
{ 1, 1, 52, 53}, // WEEK_OF_YEAR
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
{ 1, 1, 29, 31}, // DAY_OF_MONTH
{ 1, 1, 365, 366}, // DAY_OF_YEAR
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
{ 1, 1, 5, 5}, // 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
{ -5000000, -5000000, 5000000, 5000000}, // 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
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH
{ 0, 0, 11, 11}, // ORDINAL_MONTH
};
namespace { // anonymous
static const icu::UnicodeSet *gLeapCorrection = nullptr;
static icu::UInitOnce gCorrectionInitOnce {};
static int32_t gMinCorrection;
} // namespace
U_CDECL_BEGIN
static UBool calendar_persian_cleanup() {
if (gLeapCorrection) {
delete gLeapCorrection;
gLeapCorrection = nullptr;
}
gCorrectionInitOnce.reset();
return true;
}
U_CDECL_END
namespace { // anonymous
static void U_CALLCONV initLeapCorrection() {
static int16_t nonLeapYears[] = {
1502, 1601, 1634, 1667, 1700, 1733, 1766, 1799, 1832, 1865, 1898, 1931, 1964, 1997, 2030, 2059,
2063, 2096, 2129, 2158, 2162, 2191, 2195, 2224, 2228, 2257, 2261, 2290, 2294, 2323, 2327, 2356,
2360, 2389, 2393, 2422, 2426, 2455, 2459, 2488, 2492, 2521, 2525, 2554, 2558, 2587, 2591, 2620,
2624, 2653, 2657, 2686, 2690, 2719, 2723, 2748, 2752, 2756, 2781, 2785, 2789, 2818, 2822, 2847,
2851, 2855, 2880, 2884, 2888, 2913, 2917, 2921, 2946, 2950, 2954, 2979, 2983, 2987,
};
gMinCorrection = nonLeapYears[0];
icu::UnicodeSet prefab;
for (auto year : nonLeapYears) {
prefab.add(year);
}
gLeapCorrection = prefab.cloneAsThawed()->freeze();
ucln_i18n_registerCleanup(UCLN_I18N_PERSIAN_CALENDAR, calendar_persian_cleanup);
}
const icu::UnicodeSet* getLeapCorrection() {
umtx_initOnce(gCorrectionInitOnce, &initLeapCorrection);
return gLeapCorrection;
}
} // namespace anonymous
U_NAMESPACE_BEGIN
static const int32_t PERSIAN_EPOCH = 1948320;
// Implementation of the PersianCalendar class
//-------------------------------------------------------------------------
// Constructors...
//-------------------------------------------------------------------------
const char *PersianCalendar::getType() const {
return "persian";
}
PersianCalendar* PersianCalendar::clone() const {
return new PersianCalendar(*this);
}
PersianCalendar::PersianCalendar(const Locale& aLocale, UErrorCode& success)
: Calendar(TimeZone::forLocaleOrDefault(aLocale), aLocale, success)
{
}
PersianCalendar::PersianCalendar(const PersianCalendar& other) : Calendar(other) {
}
PersianCalendar::~PersianCalendar()
{
}
//-------------------------------------------------------------------------
// Minimum / Maximum access functions
//-------------------------------------------------------------------------
int32_t PersianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
return kPersianCalendarLimits[field][limitType];
}
//-------------------------------------------------------------------------
// Assorted calculation utilities
//
/**
* Determine whether a year is a leap year in the Persian calendar
*/
UBool PersianCalendar::isLeapYear(int32_t year)
{
if (year >= gMinCorrection && getLeapCorrection()->contains(year)) {
return false;
}
if (year > gMinCorrection && getLeapCorrection()->contains(year-1)) {
return true;
}
int64_t y = static_cast<int64_t>(year) * 25LL + 11LL;
bool res = (y % 33L < 8);
return res;
}
/**
* Return the day # on which the given year starts. Days are counted
* from the Persian epoch, origin 0.
*/
int32_t PersianCalendar::yearStart(int32_t year, UErrorCode& status) {
return handleComputeMonthStart(year,0,false, status);
}
/**
* Return the day # on which the given month starts. Days are counted
* from the Persian epoch, origin 0.
*
* @param year The Persian year
* @param year The Persian month, 0-based
*/
int32_t PersianCalendar::monthStart(int32_t year, int32_t month, UErrorCode& status) const {
return handleComputeMonthStart(year,month,true, status);
}
//----------------------------------------------------------------------
// Calendar framework
//----------------------------------------------------------------------
/**
* Return the length (in days) of the given month.
*
* @param year The Persian year
* @param year The Persian month, 0-based
*/
int32_t PersianCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month, UErrorCode& /*status*/) const {
// If the month is out of range, adjust it into range, and
// modify the extended year value accordingly.
if (month < 0 || month > 11) {
extendedYear += ClockMath::floorDivide(month, 12, &month);
}
return isLeapYear(extendedYear) ? kPersianLeapMonthLength[month] : kPersianMonthLength[month];
}
/**
* Return the number of days in the given Persian year
*/
int32_t PersianCalendar::handleGetYearLength(int32_t extendedYear, UErrorCode& status) const {
if (U_FAILURE(status)) return 0;
return isLeapYear(extendedYear) ? 366 : 365;
}
//-------------------------------------------------------------------------
// Functions for converting from field values to milliseconds....
//-------------------------------------------------------------------------
static int64_t firstJulianOfYear(int64_t year) {
int64_t julianDay = 365LL * (year - 1LL) + ClockMath::floorDivide(8LL * year + 21, 33);
if (year > gMinCorrection && getLeapCorrection()->contains(year-1)) {
julianDay--;
}
return julianDay;
}
// Return JD of start of given month/year
int64_t PersianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /*useMonth*/, UErrorCode& status) const {
if (U_FAILURE(status)) {
return 0;
}
// If the month is out of range, adjust it into range, and
// modify the extended year value accordingly.
if (month < 0 || month > 11) {
if (uprv_add32_overflow(eyear, ClockMath::floorDivide(month, 12, &month), &eyear)) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
}
int64_t julianDay = PERSIAN_EPOCH - 1LL + firstJulianOfYear(eyear);
if (month != 0) {
julianDay += kPersianNumDays[month];
}
return julianDay;
}
//-------------------------------------------------------------------------
// Functions for converting from milliseconds to field values
//-------------------------------------------------------------------------
int32_t PersianCalendar::handleGetExtendedYear(UErrorCode& status) {
if (U_FAILURE(status)) {
return 0;
}
if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
return internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
}
return internalGet(UCAL_YEAR, 1); // Default to year 1
}
/**
* Override Calendar to compute several fields specific to the Persian
* 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.
*/
void PersianCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) {
int64_t daysSinceEpoch = julianDay;
daysSinceEpoch -= PERSIAN_EPOCH;
int64_t year = ClockMath::floorDivideInt64(
33LL * daysSinceEpoch + 3LL, 12053LL) + 1LL;
if (year > INT32_MAX || year < INT32_MIN) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
int64_t farvardin1 = firstJulianOfYear(year);
int32_t dayOfYear = daysSinceEpoch - farvardin1; // 0-based
U_ASSERT(dayOfYear >= 0);
U_ASSERT(dayOfYear < 366);
if (dayOfYear == 365 && year >= gMinCorrection && getLeapCorrection()->contains(year)) {
year++;
dayOfYear = 0;
}
int32_t month;
if (dayOfYear < 216) { // Compute 0-based month
month = dayOfYear / 31;
} else {
month = (dayOfYear - 6) / 30;
}
U_ASSERT(month >= 0);
U_ASSERT(month < 12);
++dayOfYear; // Make it 1-based now
int32_t dayOfMonth = dayOfYear - kPersianNumDays[month];
U_ASSERT(dayOfMonth > 0);
U_ASSERT(dayOfMonth <= 31);
internalSet(UCAL_ERA, 0);
internalSet(UCAL_YEAR, year);
internalSet(UCAL_EXTENDED_YEAR, year);
internalSet(UCAL_MONTH, month);
internalSet(UCAL_ORDINAL_MONTH, month);
internalSet(UCAL_DAY_OF_MONTH, dayOfMonth);
internalSet(UCAL_DAY_OF_YEAR, dayOfYear);
}
IMPL_SYSTEM_DEFAULT_CENTURY(PersianCalendar, "@calendar=persian")
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(PersianCalendar)
int32_t
PersianCalendar::getRelatedYearDifference() const {
constexpr int32_t kPersianCalendarRelatedYearDifference = 622;
return kPersianCalendarRelatedYearDifference;
}
U_NAMESPACE_END
#endif
|