| 1 |
// © 2016 and later: Unicode, Inc. and others. |
| 2 |
// License & terms of use: http://www.unicode.org/copyright.html |
| 3 |
/* |
| 4 |
******************************************************************************* |
| 5 |
* Copyright (C) 1997-2016, International Business Machines Corporation and |
| 6 |
* others. All Rights Reserved. |
| 7 |
******************************************************************************* |
| 8 |
* |
| 9 |
* File GREGOCAL.CPP |
| 10 |
* |
| 11 |
* Modification History: |
| 12 |
* |
| 13 |
* Date Name Description |
| 14 |
* 02/05/97 clhuang Creation. |
| 15 |
* 03/28/97 aliu Made highly questionable fix to computeFields to |
| 16 |
* handle DST correctly. |
| 17 |
* 04/22/97 aliu Cleaned up code drastically. Added monthLength(). |
| 18 |
* Finished unimplemented parts of computeTime() for |
| 19 |
* week-based date determination. Removed quetionable |
| 20 |
* fix and wrote correct fix for computeFields() and |
| 21 |
* daylight time handling. Rewrote inDaylightTime() |
| 22 |
* and computeFields() to handle sensitive Daylight to |
| 23 |
* Standard time transitions correctly. |
| 24 |
* 05/08/97 aliu Added code review changes. Fixed isLeapYear() to |
| 25 |
* not cutover. |
| 26 |
* 08/12/97 aliu Added equivalentTo. Misc other fixes. Updated |
| 27 |
* add() from Java source. |
| 28 |
* 07/28/98 stephen Sync up with JDK 1.2 |
| 29 |
* 09/14/98 stephen Changed type of kOneDay, kOneWeek to double. |
| 30 |
* Fixed bug in roll() |
| 31 |
* 10/15/99 aliu Fixed j31, incorrect WEEK_OF_YEAR computation. |
| 32 |
* 10/15/99 aliu Fixed j32, cannot set date to Feb 29 2000 AD. |
| 33 |
* {JDK bug 4210209 4209272} |
| 34 |
* 11/15/99 weiv Added YEAR_WOY and DOW_LOCAL computation |
| 35 |
* to timeToFields method, updated kMinValues, kMaxValues & kLeastMaxValues |
| 36 |
* 12/09/99 aliu Fixed j81, calculation errors and roll bugs |
| 37 |
* in year of cutover. |
| 38 |
* 01/24/2000 aliu Revised computeJulianDay for YEAR YEAR_WOY WOY. |
| 39 |
******************************************************************************** |
| 40 |
*/ |
| 41 |
|
| 42 |
#include "unicode/utypes.h" |
| 43 |
#include <float.h> |
| 44 |
|
| 45 |
#if !UCONFIG_NO_FORMATTING |
| 46 |
|
| 47 |
#include "unicode/gregocal.h" |
| 48 |
#include "gregoimp.h" |
| 49 |
#include "umutex.h" |
| 50 |
#include "uassert.h" |
| 51 |
|
| 52 |
// ***************************************************************************** |
| 53 |
// class GregorianCalendar |
| 54 |
// ***************************************************************************** |
| 55 |
|
| 56 |
/** |
| 57 |
* Note that the Julian date used here is not a true Julian date, since |
| 58 |
* it is measured from midnight, not noon. This value is the Julian |
| 59 |
* day number of January 1, 1970 (Gregorian calendar) at noon UTC. [LIU] |
| 60 |
*/ |
| 61 |
|
| 62 |
static const int16_t kNumDays[] |
| 63 |
= {0,31,59,90,120,151,181,212,243,273,304,334}; // 0-based, for day-in-year |
| 64 |
static const int16_t kLeapNumDays[] |
| 65 |
= {0,31,60,91,121,152,182,213,244,274,305,335}; // 0-based, for day-in-year |
| 66 |
static const int8_t kMonthLength[] |
| 67 |
= {31,28,31,30,31,30,31,31,30,31,30,31}; // 0-based |
| 68 |
static const int8_t kLeapMonthLength[] |
| 69 |
= {31,29,31,30,31,30,31,31,30,31,30,31}; // 0-based |
| 70 |
|
| 71 |
// setTimeInMillis() limits the Julian day range to +/-7F000000. |
| 72 |
// This would seem to limit the year range to: |
| 73 |
// ms=+183882168921600000 jd=7f000000 December 20, 5828963 AD |
| 74 |
// ms=-184303902528000000 jd=81000000 September 20, 5838270 BC |
| 75 |
// HOWEVER, CalendarRegressionTest/Test4167060 shows that the actual |
| 76 |
// range limit on the year field is smaller (~ +/-140000). [alan 3.0] |
| 77 |
|
| 78 |
static const int32_t kGregorianCalendarLimits[UCAL_FIELD_COUNT][4] = { |
| 79 |
// Minimum Greatest Least Maximum |
| 80 |
// Minimum Maximum |
| 81 |
{ 0, 0, 1, 1}, // ERA |
| 82 |
{ 1, 1, 140742, 144683}, // YEAR |
| 83 |
{ 0, 0, 11, 11}, // MONTH |
| 84 |
{ 1, 1, 52, 53}, // WEEK_OF_YEAR |
| 85 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH |
| 86 |
{ 1, 1, 28, 31}, // DAY_OF_MONTH |
| 87 |
{ 1, 1, 365, 366}, // DAY_OF_YEAR |
| 88 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK |
| 89 |
{ -1, -1, 4, 5}, // DAY_OF_WEEK_IN_MONTH |
| 90 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM |
| 91 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR |
| 92 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY |
| 93 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE |
| 94 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND |
| 95 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND |
| 96 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET |
| 97 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET |
| 98 |
{ -140742, -140742, 140742, 144683}, // YEAR_WOY |
| 99 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL |
| 100 |
{ -140742, -140742, 140742, 144683}, // EXTENDED_YEAR |
| 101 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY |
| 102 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY |
| 103 |
{/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH |
| 104 |
{ 0, 0, 11, 11}, // ORDINAL_MONTH |
| 105 |
}; |
| 106 |
|
| 107 |
/* |
| 108 |
* <pre> |
| 109 |
* Greatest Least |
| 110 |
* Field name Minimum Minimum Maximum Maximum |
| 111 |
* ---------- ------- ------- ------- ------- |
| 112 |
* ERA 0 0 1 1 |
| 113 |
* YEAR 1 1 140742 144683 |
| 114 |
* MONTH 0 0 11 11 |
| 115 |
* WEEK_OF_YEAR 1 1 52 53 |
| 116 |
* WEEK_OF_MONTH 0 0 4 6 |
| 117 |
* DAY_OF_MONTH 1 1 28 31 |
| 118 |
* DAY_OF_YEAR 1 1 365 366 |
| 119 |
* DAY_OF_WEEK 1 1 7 7 |
| 120 |
* DAY_OF_WEEK_IN_MONTH -1 -1 4 5 |
| 121 |
* AM_PM 0 0 1 1 |
| 122 |
* HOUR 0 0 11 11 |
| 123 |
* HOUR_OF_DAY 0 0 23 23 |
| 124 |
* MINUTE 0 0 59 59 |
| 125 |
* SECOND 0 0 59 59 |
| 126 |
* MILLISECOND 0 0 999 999 |
| 127 |
* ZONE_OFFSET -12* -12* 12* 12* |
| 128 |
* DST_OFFSET 0 0 1* 1* |
| 129 |
* YEAR_WOY 1 1 140742 144683 |
| 130 |
* DOW_LOCAL 1 1 7 7 |
| 131 |
* </pre> |
| 132 |
* (*) In units of one-hour |
| 133 |
*/ |
| 134 |
|
| 135 |
#if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL) |
| 136 |
#include <stdio.h> |
| 137 |
#endif |
| 138 |
|
| 139 |
U_NAMESPACE_BEGIN |
| 140 |
|
| 141 |
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(GregorianCalendar) |
| 142 |
|
| 143 |
// 00:00:00 UTC, October 15, 1582, expressed in ms from the epoch. |
| 144 |
// Note that only Italy and other Catholic countries actually |
| 145 |
// observed this cutover. Most other countries followed in |
| 146 |
// the next few centuries, some as late as 1928. [LIU] |
| 147 |
// in Java, -12219292800000L |
| 148 |
//const UDate GregorianCalendar::kPapalCutover = -12219292800000L; |
| 149 |
static const uint32_t kCutoverJulianDay = 2299161; |
| 150 |
static const int32_t kDefaultCutoverYear = 1582; |
| 151 |
static const UDate kPapalCutover = (2299161.0 - kEpochStartAsJulianDay) * U_MILLIS_PER_DAY; |
| 152 |
//static const UDate kPapalCutoverJulian = (2299161.0 - kEpochStartAsJulianDay); |
| 153 |
|
| 154 |
// ------------------------------------- |
| 155 |
|
| 156 |
GregorianCalendar::GregorianCalendar(UErrorCode& status) |
| 157 |
: Calendar(status), |
| 158 |
fGregorianCutover(kPapalCutover), |
| 159 |
fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(kDefaultCutoverYear), |
| 160 |
fIsGregorian(true), fInvertGregorian(false) |
| 161 |
{ |
| 162 |
setTimeInMillis(getNow(), status); |
| 163 |
} |
| 164 |
|
| 165 |
// ------------------------------------- |
| 166 |
|
| 167 |
GregorianCalendar::GregorianCalendar(TimeZone* zone, UErrorCode& status) |
| 168 |
: GregorianCalendar(zone, Locale::getDefault(), status) |
| 169 |
{ |
| 170 |
} |
| 171 |
|
| 172 |
// ------------------------------------- |
| 173 |
|
| 174 |
GregorianCalendar::GregorianCalendar(const TimeZone& zone, UErrorCode& status) |
| 175 |
: GregorianCalendar(zone, Locale::getDefault(), status) |
| 176 |
{ |
| 177 |
} |
| 178 |
|
| 179 |
// ------------------------------------- |
| 180 |
|
| 181 |
GregorianCalendar::GregorianCalendar(const Locale& aLocale, UErrorCode& status) |
| 182 |
: GregorianCalendar(TimeZone::forLocaleOrDefault(aLocale), aLocale, status) |
| 183 |
{ |
| 184 |
} |
| 185 |
|
| 186 |
// ------------------------------------- |
| 187 |
|
| 188 |
GregorianCalendar::GregorianCalendar(TimeZone* zone, const Locale& aLocale, |
| 189 |
UErrorCode& status) |
| 190 |
: Calendar(zone, aLocale, status), |
| 191 |
fGregorianCutover(kPapalCutover), |
| 192 |
fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(kDefaultCutoverYear), |
| 193 |
fIsGregorian(true), fInvertGregorian(false) |
| 194 |
{ |
| 195 |
setTimeInMillis(getNow(), status); |
| 196 |
} |
| 197 |
|
| 198 |
// ------------------------------------- |
| 199 |
|
| 200 |
GregorianCalendar::GregorianCalendar(const TimeZone& zone, const Locale& aLocale, |
| 201 |
UErrorCode& status) |
| 202 |
: Calendar(zone, aLocale, status), |
| 203 |
fGregorianCutover(kPapalCutover), |
| 204 |
fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(kDefaultCutoverYear), |
| 205 |
fIsGregorian(true), fInvertGregorian(false) |
| 206 |
{ |
| 207 |
setTimeInMillis(getNow(), status); |
| 208 |
} |
| 209 |
|
| 210 |
// ------------------------------------- |
| 211 |
|
| 212 |
GregorianCalendar::GregorianCalendar(int32_t year, int32_t month, int32_t date, |
| 213 |
UErrorCode& status) |
| 214 |
: Calendar(TimeZone::createDefault(), Locale::getDefault(), status), |
| 215 |
fGregorianCutover(kPapalCutover), |
| 216 |
fCutoverJulianDay(kCutoverJulianDay), fNormalizedGregorianCutover(fGregorianCutover), fGregorianCutoverYear(kDefaultCutoverYear), |
| 217 |
fIsGregorian(true), fInvertGregorian(false) |
| 218 |
{ |
| 219 |
set(UCAL_ERA, AD); |
| 220 |
set(UCAL_YEAR, year); |
| 221 |
set(UCAL_MONTH, month); |
| 222 |
set(UCAL_DATE, date); |
| 223 |
} |
| 224 |
|
| 225 |
// ------------------------------------- |
| 226 |
|
| 227 |
GregorianCalendar::GregorianCalendar(int32_t year, int32_t month, int32_t date, |
| 228 |
int32_t hour, int32_t minute, UErrorCode& status) |
| 229 |
: GregorianCalendar(year, month, date, status) |
| 230 |
{ |
| 231 |
set(UCAL_HOUR_OF_DAY, hour); |
| 232 |
set(UCAL_MINUTE, minute); |
| 233 |
} |
| 234 |
|
| 235 |
// ------------------------------------- |
| 236 |
|
| 237 |
GregorianCalendar::GregorianCalendar(int32_t year, int32_t month, int32_t date, |
| 238 |
int32_t hour, int32_t minute, int32_t second, |
| 239 |
UErrorCode& status) |
| 240 |
: GregorianCalendar(year, month, date, hour, minute, status) |
| 241 |
{ |
| 242 |
set(UCAL_SECOND, second); |
| 243 |
} |
| 244 |
|
| 245 |
// ------------------------------------- |
| 246 |
|
| 247 |
GregorianCalendar::~GregorianCalendar() |
| 248 |
{ |
| 249 |
} |
| 250 |
|
| 251 |
// ------------------------------------- |
| 252 |
|
| 253 |
GregorianCalendar::GregorianCalendar(const GregorianCalendar &source) |
| 254 |
: Calendar(source), |
| 255 |
fGregorianCutover(source.fGregorianCutover), |
| 256 |
fCutoverJulianDay(source.fCutoverJulianDay), fNormalizedGregorianCutover(source.fNormalizedGregorianCutover), fGregorianCutoverYear(source.fGregorianCutoverYear), |
| 257 |
fIsGregorian(source.fIsGregorian), fInvertGregorian(source.fInvertGregorian) |
| 258 |
{ |
| 259 |
} |
| 260 |
|
| 261 |
// ------------------------------------- |
| 262 |
|
| 263 |
GregorianCalendar* GregorianCalendar::clone() const |
| 264 |
{ |
| 265 |
return new GregorianCalendar(*this); |
| 266 |
} |
| 267 |
|
| 268 |
// ------------------------------------- |
| 269 |
|
| 270 |
GregorianCalendar & |
| 271 |
GregorianCalendar::operator=(const GregorianCalendar &right) |
| 272 |
{ |
| 273 |
if (this != &right) |
| 274 |
{ |
| 275 |
Calendar::operator=(right); |
| 276 |
fGregorianCutover = right.fGregorianCutover; |
| 277 |
fNormalizedGregorianCutover = right.fNormalizedGregorianCutover; |
| 278 |
fGregorianCutoverYear = right.fGregorianCutoverYear; |
| 279 |
fCutoverJulianDay = right.fCutoverJulianDay; |
| 280 |
} |
| 281 |
return *this; |
| 282 |
} |
| 283 |
|
| 284 |
// ------------------------------------- |
| 285 |
|
| 286 |
UBool GregorianCalendar::isEquivalentTo(const Calendar& other) const |
| 287 |
{ |
| 288 |
// Calendar override. |
| 289 |
return Calendar::isEquivalentTo(other) && |
| 290 |
fGregorianCutover == ((GregorianCalendar*)&other)->fGregorianCutover; |
| 291 |
} |
| 292 |
|
| 293 |
// ------------------------------------- |
| 294 |
|
| 295 |
void |
| 296 |
GregorianCalendar::setGregorianChange(UDate date, UErrorCode& status) |
| 297 |
{ |
| 298 |
if (U_FAILURE(status)) |
| 299 |
return; |
| 300 |
|
| 301 |
// Precompute two internal variables which we use to do the actual |
| 302 |
// cutover computations. These are the normalized cutover, which is the |
| 303 |
// midnight at or before the cutover, and the cutover year. The |
| 304 |
// normalized cutover is in pure date milliseconds; it contains no time |
| 305 |
// of day or timezone component, and it used to compare against other |
| 306 |
// pure date values. |
| 307 |
double cutoverDay = ClockMath::floorDivide(date, kOneDay); |
| 308 |
|
| 309 |
// Handle the rare case of numeric overflow where the user specifies a time |
| 310 |
// outside of INT32_MIN .. INT32_MAX number of days. |
| 311 |
|
| 312 |
if (cutoverDay <= INT32_MIN) { |
| 313 |
cutoverDay = INT32_MIN; |
| 314 |
fGregorianCutover = fNormalizedGregorianCutover = cutoverDay * kOneDay; |
| 315 |
} else if (cutoverDay >= INT32_MAX) { |
| 316 |
cutoverDay = INT32_MAX; |
| 317 |
fGregorianCutover = fNormalizedGregorianCutover = cutoverDay * kOneDay; |
| 318 |
} else { |
| 319 |
fNormalizedGregorianCutover = cutoverDay * kOneDay; |
| 320 |
fGregorianCutover = date; |
| 321 |
} |
| 322 |
|
| 323 |
// Normalize the year so BC values are represented as 0 and negative |
| 324 |
// values. |
| 325 |
GregorianCalendar *cal = new GregorianCalendar(getTimeZone(), status); |
| 326 |
/* test for nullptr */ |
| 327 |
if (cal == nullptr) { |
| 328 |
status = U_MEMORY_ALLOCATION_ERROR; |
| 329 |
return; |
| 330 |
} |
| 331 |
if(U_FAILURE(status)) { |
| 332 |
return; |
| 333 |
} |
| 334 |
cal->setTime(date, status); |
| 335 |
fGregorianCutoverYear = cal->get(UCAL_YEAR, status); |
| 336 |
if (cal->get(UCAL_ERA, status) == BC) { |
| 337 |
fGregorianCutoverYear = 1 - fGregorianCutoverYear; |
| 338 |
} |
| 339 |
fCutoverJulianDay = static_cast<int32_t>(cutoverDay); |
| 340 |
delete cal; |
| 341 |
} |
| 342 |
|
| 343 |
void GregorianCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) { |
| 344 |
int32_t eyear, month, dayOfMonth, dayOfYear, unusedRemainder; |
| 345 |
|
| 346 |
if(U_FAILURE(status)) { |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
#if defined (U_DEBUG_CAL) |
| 351 |
fprintf(stderr, "%s:%d: jd%d- (greg's %d)- [cut=%d]\n", |
| 352 |
__FILE__, __LINE__, julianDay, getGregorianDayOfYear(), fCutoverJulianDay); |
| 353 |
#endif |
| 354 |
|
| 355 |
if (julianDay >= fCutoverJulianDay) { |
| 356 |
month = getGregorianMonth(); |
| 357 |
dayOfMonth = getGregorianDayOfMonth(); |
| 358 |
dayOfYear = getGregorianDayOfYear(); |
| 359 |
eyear = getGregorianYear(); |
| 360 |
} else { |
| 361 |
// The Julian epoch day (not the same as Julian Day) |
| 362 |
// is zero on Saturday December 30, 0 (Gregorian). |
| 363 |
int32_t julianEpochDay = julianDay - (kJan1_1JulianDay - 2); |
| 364 |
eyear = static_cast<int32_t>(ClockMath::floorDivide((4.0 * julianEpochDay) + 1464.0, static_cast<int32_t>(1461), &unusedRemainder)); |
| 365 |
|
| 366 |
// Compute the Julian calendar day number for January 1, eyear |
| 367 |
int32_t january1 = 365 * (eyear - 1) + ClockMath::floorDivide(eyear - 1, static_cast<int32_t>(4)); |
| 368 |
dayOfYear = (julianEpochDay - january1); // 0-based |
| 369 |
|
| 370 |
// Julian leap years occurred historically every 4 years starting |
| 371 |
// with 8 AD. Before 8 AD the spacing is irregular; every 3 years |
| 372 |
// from 45 BC to 9 BC, and then none until 8 AD. However, we don't |
| 373 |
// implement this historical detail; instead, we implement the |
| 374 |
// computationally cleaner proleptic calendar, which assumes |
| 375 |
// consistent 4-year cycles throughout time. |
| 376 |
UBool isLeap = ((eyear&0x3) == 0); // equiv. to (eyear%4 == 0) |
| 377 |
|
| 378 |
// Common Julian/Gregorian calculation |
| 379 |
int32_t correction = 0; |
| 380 |
int32_t march1 = isLeap ? 60 : 59; // zero-based DOY for March 1 |
| 381 |
if (dayOfYear >= march1) { |
| 382 |
correction = isLeap ? 1 : 2; |
| 383 |
} |
| 384 |
month = (12 * (dayOfYear + correction) + 6) / 367; // zero-based month |
| 385 |
dayOfMonth = dayOfYear - (isLeap?kLeapNumDays[month]:kNumDays[month]) + 1; // one-based DOM |
| 386 |
++dayOfYear; |
| 387 |
#if defined (U_DEBUG_CAL) |
| 388 |
// fprintf(stderr, "%d - %d[%d] + 1\n", dayOfYear, isLeap?kLeapNumDays[month]:kNumDays[month], month ); |
| 389 |
// fprintf(stderr, "%s:%d: greg's HCF %d -> %d/%d/%d not %d/%d/%d\n", |
| 390 |
// __FILE__, __LINE__,julianDay, |
| 391 |
// eyear,month,dayOfMonth, |
| 392 |
// getGregorianYear(), getGregorianMonth(), getGregorianDayOfMonth() ); |
| 393 |
fprintf(stderr, "%s:%d: doy %d (greg's %d)- [cut=%d]\n", |
| 394 |
__FILE__, __LINE__, dayOfYear, getGregorianDayOfYear(), fCutoverJulianDay); |
| 395 |
#endif |
| 396 |
|
| 397 |
} |
| 398 |
|
| 399 |
// [j81] if we are after the cutover in its year, shift the day of the year |
| 400 |
if((eyear == fGregorianCutoverYear) && (julianDay >= fCutoverJulianDay)) { |
| 401 |
//from handleComputeMonthStart |
| 402 |
int32_t gregShift = Grego::gregorianShift(eyear); |
| 403 |
#if defined (U_DEBUG_CAL) |
| 404 |
fprintf(stderr, "%s:%d: gregorian shift %d ::: doy%d => %d [cut=%d]\n", |
| 405 |
__FILE__, __LINE__,gregShift, dayOfYear, dayOfYear+gregShift, fCutoverJulianDay); |
| 406 |
#endif |
| 407 |
dayOfYear += gregShift; |
| 408 |
} |
| 409 |
|
| 410 |
internalSet(UCAL_MONTH, month); |
| 411 |
internalSet(UCAL_ORDINAL_MONTH, month); |
| 412 |
internalSet(UCAL_DAY_OF_MONTH, dayOfMonth); |
| 413 |
internalSet(UCAL_DAY_OF_YEAR, dayOfYear); |
| 414 |
internalSet(UCAL_EXTENDED_YEAR, eyear); |
| 415 |
int32_t era = AD; |
| 416 |
if (eyear < 1) { |
| 417 |
era = BC; |
| 418 |
eyear = 1 - eyear; |
| 419 |
} |
| 420 |
internalSet(UCAL_ERA, era); |
| 421 |
internalSet(UCAL_YEAR, eyear); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
// ------------------------------------- |
| 426 |
|
| 427 |
UDate |
| 428 |
GregorianCalendar::getGregorianChange() const |
| 429 |
{ |
| 430 |
return fGregorianCutover; |
| 431 |
} |
| 432 |
|
| 433 |
// ------------------------------------- |
| 434 |
|
| 435 |
UBool |
| 436 |
GregorianCalendar::isLeapYear(int32_t year) const |
| 437 |
{ |
| 438 |
// MSVC complains bitterly if we try to use Grego::isLeapYear here |
| 439 |
// NOTE: year&0x3 == year%4 |
| 440 |
return (year >= fGregorianCutoverYear ? |
| 441 |
(((year&0x3) == 0) && ((year%100 != 0) || (year%400 == 0))) : // Gregorian |
| 442 |
((year&0x3) == 0)); // Julian |
| 443 |
} |
| 444 |
|
| 445 |
// ------------------------------------- |
| 446 |
|
| 447 |
int32_t GregorianCalendar::handleComputeJulianDay(UCalendarDateFields bestField, UErrorCode& status) |
| 448 |
{ |
| 449 |
fInvertGregorian = false; |
| 450 |
|
| 451 |
int32_t jd = Calendar::handleComputeJulianDay(bestField, status); |
| 452 |
if (U_FAILURE(status)) { |
| 453 |
return 0; |
| 454 |
} |
| 455 |
|
| 456 |
if((bestField == UCAL_WEEK_OF_YEAR) && // if we are doing WOY calculations, we are counting relative to Jan 1 *julian* |
| 457 |
(internalGet(UCAL_EXTENDED_YEAR)==fGregorianCutoverYear) && |
| 458 |
jd >= fCutoverJulianDay) { |
| 459 |
fInvertGregorian = true; // So that the Julian Jan 1 will be used in handleComputeMonthStart |
| 460 |
return Calendar::handleComputeJulianDay(bestField, status); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
// The following check handles portions of the cutover year BEFORE the |
| 465 |
// cutover itself happens. |
| 466 |
//if ((fIsGregorian==true) != (jd >= fCutoverJulianDay)) { /* cutoverJulianDay)) { */ |
| 467 |
if ((fIsGregorian) != (jd >= fCutoverJulianDay)) { /* cutoverJulianDay)) { */ |
| 468 |
#if defined (U_DEBUG_CAL) |
| 469 |
fprintf(stderr, "%s:%d: jd [invert] %d\n", |
| 470 |
__FILE__, __LINE__, jd); |
| 471 |
#endif |
| 472 |
fInvertGregorian = true; |
| 473 |
jd = Calendar::handleComputeJulianDay(bestField, status); |
| 474 |
if (U_FAILURE(status)) { |
| 475 |
return 0; |
| 476 |
} |
| 477 |
#if defined (U_DEBUG_CAL) |
| 478 |
fprintf(stderr, "%s:%d: fIsGregorian %s, fInvertGregorian %s - ", |
| 479 |
__FILE__, __LINE__,fIsGregorian?"T":"F", fInvertGregorian?"T":"F"); |
| 480 |
fprintf(stderr, " jd NOW %d\n", |
| 481 |
jd); |
| 482 |
#endif |
| 483 |
} else { |
| 484 |
#if defined (U_DEBUG_CAL) |
| 485 |
fprintf(stderr, "%s:%d: jd [==] %d - %sfIsGregorian %sfInvertGregorian, %d\n", |
| 486 |
__FILE__, __LINE__, jd, fIsGregorian?"T":"F", fInvertGregorian?"T":"F", bestField); |
| 487 |
#endif |
| 488 |
} |
| 489 |
|
| 490 |
if(fIsGregorian && (internalGet(UCAL_EXTENDED_YEAR) == fGregorianCutoverYear)) { |
| 491 |
int32_t gregShift = Grego::gregorianShift(internalGet(UCAL_EXTENDED_YEAR)); |
| 492 |
if (bestField == UCAL_DAY_OF_YEAR) { |
| 493 |
#if defined (U_DEBUG_CAL) |
| 494 |
fprintf(stderr, "%s:%d: [DOY%d] gregorian shift of JD %d += %d\n", |
| 495 |
__FILE__, __LINE__, fFields[bestField],jd, gregShift); |
| 496 |
#endif |
| 497 |
jd -= gregShift; |
| 498 |
} else if ( bestField == UCAL_WEEK_OF_MONTH ) { |
| 499 |
int32_t weekShift = 14; |
| 500 |
#if defined (U_DEBUG_CAL) |
| 501 |
fprintf(stderr, "%s:%d: [WOY/WOM] gregorian week shift of %d += %d\n", |
| 502 |
__FILE__, __LINE__, jd, weekShift); |
| 503 |
#endif |
| 504 |
jd += weekShift; // shift by weeks for week based fields. |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
return jd; |
| 509 |
} |
| 510 |
|
| 511 |
int64_t GregorianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, |
| 512 |
|
| 513 |
UBool /* useMonth */, UErrorCode& status) const |
| 514 |
{ |
| 515 |
if (U_FAILURE(status)) { |
| 516 |
return 0; |
| 517 |
} |
| 518 |
GregorianCalendar* nonConstThis = const_cast<GregorianCalendar*>(this); // cast away const |
| 519 |
|
| 520 |
// If the month is out of range, adjust it into range, and |
| 521 |
// modify the extended year value accordingly. |
| 522 |
if (month < 0 || month > 11) { |
| 523 |
if (uprv_add32_overflow(ClockMath::floorDivide(month, 12, &month), |
| 524 |
eyear, &eyear)) { |
| 525 |
status = U_ILLEGAL_ARGUMENT_ERROR; |
| 526 |
return 0; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
UBool isLeap = eyear%4 == 0; |
| 531 |
int64_t y = static_cast<int64_t>(eyear) - 1; |
| 532 |
int64_t julianDay = 365LL * y + |
| 533 |
ClockMath::floorDivideInt64(y, 4LL) + kJan1_1JulianDay - 3LL; |
| 534 |
|
| 535 |
nonConstThis->fIsGregorian = (eyear >= fGregorianCutoverYear); |
| 536 |
#if defined (U_DEBUG_CAL) |
| 537 |
fprintf(stderr, "%s:%d: (hcms%d/%d) fIsGregorian %s, fInvertGregorian %s\n", |
| 538 |
__FILE__, __LINE__, eyear,month, fIsGregorian?"T":"F", fInvertGregorian?"T":"F"); |
| 539 |
#endif |
| 540 |
if (fInvertGregorian) { |
| 541 |
nonConstThis->fIsGregorian = !fIsGregorian; |
| 542 |
} |
| 543 |
if (fIsGregorian) { |
| 544 |
isLeap = isLeap && ((eyear%100 != 0) || (eyear%400 == 0)); |
| 545 |
// Add 2 because Gregorian calendar starts 2 days after |
| 546 |
// Julian calendar |
| 547 |
int32_t gregShift = Grego::gregorianShift(eyear); |
| 548 |
#if defined (U_DEBUG_CAL) |
| 549 |
fprintf(stderr, "%s:%d: (hcms%d/%d) gregorian shift of %d += %d\n", |
| 550 |
__FILE__, __LINE__, eyear, month, julianDay, gregShift); |
| 551 |
#endif |
| 552 |
julianDay += gregShift; |
| 553 |
} |
| 554 |
|
| 555 |
// At this point julianDay indicates the day BEFORE the first |
| 556 |
// day of January 1, <eyear> of either the Julian or Gregorian |
| 557 |
// calendar. |
| 558 |
|
| 559 |
if (month != 0) { |
| 560 |
julianDay += isLeap?kLeapNumDays[month]:kNumDays[month]; |
| 561 |
} |
| 562 |
|
| 563 |
return julianDay; |
| 564 |
} |
| 565 |
|
| 566 |
int32_t GregorianCalendar::handleGetMonthLength(int32_t extendedYear, int32_t month, UErrorCode& /* status */) const |
| 567 |
{ |
| 568 |
// If the month is out of range, adjust it into range, and |
| 569 |
// modify the extended year value accordingly. |
| 570 |
if (month < 0 || month > 11) { |
| 571 |
extendedYear += ClockMath::floorDivide(month, 12, &month); |
| 572 |
} |
| 573 |
|
| 574 |
return isLeapYear(extendedYear) ? kLeapMonthLength[month] : kMonthLength[month]; |
| 575 |
} |
| 576 |
|
| 577 |
int32_t GregorianCalendar::handleGetYearLength(int32_t eyear, UErrorCode& status) const { |
| 578 |
if (U_FAILURE(status)) return 0; |
| 579 |
return isLeapYear(eyear) ? 366 : 365; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
int32_t |
| 584 |
GregorianCalendar::monthLength(int32_t month, UErrorCode& status) const |
| 585 |
{ |
| 586 |
int32_t year = internalGet(UCAL_EXTENDED_YEAR); |
| 587 |
return handleGetMonthLength(year, month, status); |
| 588 |
} |
| 589 |
|
| 590 |
// ------------------------------------- |
| 591 |
|
| 592 |
int32_t |
| 593 |
GregorianCalendar::monthLength(int32_t month, int32_t year) const |
| 594 |
{ |
| 595 |
return isLeapYear(year) ? kLeapMonthLength[month] : kMonthLength[month]; |
| 596 |
} |
| 597 |
|
| 598 |
// ------------------------------------- |
| 599 |
|
| 600 |
int32_t |
| 601 |
GregorianCalendar::yearLength() const |
| 602 |
{ |
| 603 |
return isLeapYear(internalGet(UCAL_YEAR)) ? 366 : 365; |
| 604 |
} |
| 605 |
|
| 606 |
// ------------------------------------- |
| 607 |
|
| 608 |
UBool |
| 609 |
GregorianCalendar::validateFields() const |
| 610 |
{ |
| 611 |
for (int32_t field = 0; field < UCAL_FIELD_COUNT; field++) { |
| 612 |
// Ignore DATE and DAY_OF_YEAR which are handled below |
| 613 |
if (field != UCAL_DATE && |
| 614 |
field != UCAL_DAY_OF_YEAR && |
| 615 |
isSet(static_cast<UCalendarDateFields>(field)) && |
| 616 |
!boundsCheck(internalGet(static_cast<UCalendarDateFields>(field)), static_cast<UCalendarDateFields>(field))) |
| 617 |
return false; |
| 618 |
} |
| 619 |
|
| 620 |
// Values differ in Least-Maximum and Maximum should be handled |
| 621 |
// specially. |
| 622 |
if (isSet(UCAL_DATE)) { |
| 623 |
int32_t date = internalGet(UCAL_DATE); |
| 624 |
UErrorCode internalStatus = U_ZERO_ERROR; |
| 625 |
if (date < getMinimum(UCAL_DATE) || |
| 626 |
date > monthLength(internalGetMonth(internalStatus), internalStatus) || |
| 627 |
U_FAILURE(internalStatus)) { |
| 628 |
return false; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
if (isSet(UCAL_DAY_OF_YEAR)) { |
| 633 |
int32_t days = internalGet(UCAL_DAY_OF_YEAR); |
| 634 |
if (days < 1 || days > yearLength()) { |
| 635 |
return false; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
// Handle DAY_OF_WEEK_IN_MONTH, which must not have the value zero. |
| 640 |
// We've checked against minimum and maximum above already. |
| 641 |
if (isSet(UCAL_DAY_OF_WEEK_IN_MONTH) && |
| 642 |
0 == internalGet(UCAL_DAY_OF_WEEK_IN_MONTH)) { |
| 643 |
return false; |
| 644 |
} |
| 645 |
|
| 646 |
return true; |
| 647 |
} |
| 648 |
|
| 649 |
// ------------------------------------- |
| 650 |
|
| 651 |
UBool |
| 652 |
GregorianCalendar::boundsCheck(int32_t value, UCalendarDateFields field) const |
| 653 |
{ |
| 654 |
return value >= getMinimum(field) && value <= getMaximum(field); |
| 655 |
} |
| 656 |
|
| 657 |
// ------------------------------------- |
| 658 |
|
| 659 |
UDate |
| 660 |
GregorianCalendar::getEpochDay(UErrorCode& status) |
| 661 |
{ |
| 662 |
complete(status); |
| 663 |
// Divide by 1000 (convert to seconds) in order to prevent overflow when |
| 664 |
// dealing with UDate(Long.MIN_VALUE) and UDate(Long.MAX_VALUE). |
| 665 |
double wallSec = internalGetTime()/1000 + (internalGet(UCAL_ZONE_OFFSET) + internalGet(UCAL_DST_OFFSET))/1000; |
| 666 |
|
| 667 |
return ClockMath::floorDivide(wallSec, kOneDay/1000.0); |
| 668 |
} |
| 669 |
|
| 670 |
// ------------------------------------- |
| 671 |
|
| 672 |
|
| 673 |
// ------------------------------------- |
| 674 |
|
| 675 |
/** |
| 676 |
* Compute the julian day number of the day BEFORE the first day of |
| 677 |
* January 1, year 1 of the given calendar. If julianDay == 0, it |
| 678 |
* specifies (Jan. 1, 1) - 1, in whatever calendar we are using (Julian |
| 679 |
* or Gregorian). |
| 680 |
*/ |
| 681 |
double GregorianCalendar::computeJulianDayOfYear(UBool isGregorian, |
| 682 |
int32_t year, UBool& isLeap) |
| 683 |
{ |
| 684 |
isLeap = year%4 == 0; |
| 685 |
int32_t y = year - 1; |
| 686 |
double julianDay = 365.0*y + ClockMath::floorDivide(y, 4) + (kJan1_1JulianDay - 3); |
| 687 |
|
| 688 |
if (isGregorian) { |
| 689 |
isLeap = isLeap && ((year%100 != 0) || (year%400 == 0)); |
| 690 |
// Add 2 because Gregorian calendar starts 2 days after Julian calendar |
| 691 |
julianDay += Grego::gregorianShift(year); |
| 692 |
} |
| 693 |
|
| 694 |
return julianDay; |
| 695 |
} |
| 696 |
|
| 697 |
// /** |
| 698 |
// * Compute the day of week, relative to the first day of week, from |
| 699 |
// * 0..6, of the current DOW_LOCAL or DAY_OF_WEEK fields. This is |
| 700 |
// * equivalent to get(DOW_LOCAL) - 1. |
| 701 |
// */ |
| 702 |
// int32_t GregorianCalendar::computeRelativeDOW() const { |
| 703 |
// int32_t relDow = 0; |
| 704 |
// if (fStamp[UCAL_DOW_LOCAL] > fStamp[UCAL_DAY_OF_WEEK]) { |
| 705 |
// relDow = internalGet(UCAL_DOW_LOCAL) - 1; // 1-based |
| 706 |
// } else if (fStamp[UCAL_DAY_OF_WEEK] != kUnset) { |
| 707 |
// relDow = internalGet(UCAL_DAY_OF_WEEK) - getFirstDayOfWeek(); |
| 708 |
// if (relDow < 0) relDow += 7; |
| 709 |
// } |
| 710 |
// return relDow; |
| 711 |
// } |
| 712 |
|
| 713 |
// /** |
| 714 |
// * Compute the day of week, relative to the first day of week, |
| 715 |
// * from 0..6 of the given julian day. |
| 716 |
// */ |
| 717 |
// int32_t GregorianCalendar::computeRelativeDOW(double julianDay) const { |
| 718 |
// int32_t relDow = julianDayToDayOfWeek(julianDay) - getFirstDayOfWeek(); |
| 719 |
// if (relDow < 0) { |
| 720 |
// relDow += 7; |
| 721 |
// } |
| 722 |
// return relDow; |
| 723 |
// } |
| 724 |
|
| 725 |
// /** |
| 726 |
// * Compute the DOY using the WEEK_OF_YEAR field and the julian day |
| 727 |
// * of the day BEFORE January 1 of a year (a return value from |
| 728 |
// * computeJulianDayOfYear). |
| 729 |
// */ |
| 730 |
// int32_t GregorianCalendar::computeDOYfromWOY(double julianDayOfYear) const { |
| 731 |
// // Compute DOY from day of week plus week of year |
| 732 |
|
| 733 |
// // Find the day of the week for the first of this year. This |
| 734 |
// // is zero-based, with 0 being the locale-specific first day of |
| 735 |
// // the week. Add 1 to get first day of year. |
| 736 |
// int32_t fdy = computeRelativeDOW(julianDayOfYear + 1); |
| 737 |
|
| 738 |
// return |
| 739 |
// // Compute doy of first (relative) DOW of WOY 1 |
| 740 |
// (((7 - fdy) < getMinimalDaysInFirstWeek()) |
| 741 |
// ? (8 - fdy) : (1 - fdy)) |
| 742 |
|
| 743 |
// // Adjust for the week number. |
| 744 |
// + (7 * (internalGet(UCAL_WEEK_OF_YEAR) - 1)) |
| 745 |
|
| 746 |
// // Adjust for the DOW |
| 747 |
// + computeRelativeDOW(); |
| 748 |
// } |
| 749 |
|
| 750 |
// ------------------------------------- |
| 751 |
|
| 752 |
double |
| 753 |
GregorianCalendar::millisToJulianDay(UDate millis) |
| 754 |
{ |
| 755 |
return static_cast<double>(kEpochStartAsJulianDay) + ClockMath::floorDivide(millis, kOneDay); |
| 756 |
} |
| 757 |
|
| 758 |
// ------------------------------------- |
| 759 |
|
| 760 |
UDate |
| 761 |
GregorianCalendar::julianDayToMillis(double julian) |
| 762 |
{ |
| 763 |
return static_cast<UDate>((julian - kEpochStartAsJulianDay) * kOneDay); |
| 764 |
} |
| 765 |
|
| 766 |
// ------------------------------------- |
| 767 |
|
| 768 |
int32_t |
| 769 |
GregorianCalendar::aggregateStamp(int32_t stamp_a, int32_t stamp_b) |
| 770 |
{ |
| 771 |
return (((stamp_a != kUnset && stamp_b != kUnset) |
| 772 |
? uprv_max(stamp_a, stamp_b) |
| 773 |
: static_cast<int32_t>(kUnset))); |
| 774 |
} |
| 775 |
|
| 776 |
// ------------------------------------- |
| 777 |
|
| 778 |
/** |
| 779 |
* Roll a field by a signed amount. |
| 780 |
* Note: This will be made public later. [LIU] |
| 781 |
*/ |
| 782 |
|
| 783 |
void |
| 784 |
GregorianCalendar::roll(EDateFields field, int32_t amount, UErrorCode& status) { |
| 785 |
roll(static_cast<UCalendarDateFields>(field), amount, status); |
| 786 |
} |
| 787 |
|
| 788 |
void |
| 789 |
GregorianCalendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) UPRV_NO_SANITIZE_UNDEFINED { |
| 790 |
if((amount == 0) || U_FAILURE(status)) { |
| 791 |
return; |
| 792 |
} |
| 793 |
|
| 794 |
// J81 processing. (gregorian cutover) |
| 795 |
UBool inCutoverMonth = false; |
| 796 |
int32_t cMonthLen=0; // 'c' for cutover; in days |
| 797 |
int32_t cDayOfMonth=0; // no discontinuity: [0, cMonthLen) |
| 798 |
double cMonthStart=0.0; // in ms |
| 799 |
|
| 800 |
// Common code - see if we're in the cutover month of the cutover year |
| 801 |
if(get(UCAL_EXTENDED_YEAR, status) == fGregorianCutoverYear) { |
| 802 |
switch (field) { |
| 803 |
case UCAL_DAY_OF_MONTH: |
| 804 |
case UCAL_WEEK_OF_MONTH: |
| 805 |
{ |
| 806 |
int32_t max = monthLength(internalGetMonth(status), status); |
| 807 |
if (U_FAILURE(status)) { |
| 808 |
return; |
| 809 |
} |
| 810 |
UDate t = internalGetTime(); |
| 811 |
// We subtract 1 from the DAY_OF_MONTH to make it zero-based, and an |
| 812 |
// additional 10 if we are after the cutover. Thus the monthStart |
| 813 |
// value will be correct iff we actually are in the cutover month. |
| 814 |
cDayOfMonth = internalGet(UCAL_DAY_OF_MONTH) - ((t >= fGregorianCutover) ? 10 : 0); |
| 815 |
cMonthStart = t - ((cDayOfMonth - 1) * kOneDay); |
| 816 |
// A month containing the cutover is 10 days shorter. |
| 817 |
if ((cMonthStart < fGregorianCutover) && |
| 818 |
(cMonthStart + (cMonthLen=(max-10))*kOneDay >= fGregorianCutover)) { |
| 819 |
inCutoverMonth = true; |
| 820 |
} |
| 821 |
} |
| 822 |
break; |
| 823 |
default: |
| 824 |
; |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
switch (field) { |
| 829 |
case UCAL_WEEK_OF_YEAR: { |
| 830 |
// Unlike WEEK_OF_MONTH, WEEK_OF_YEAR never shifts the day of the |
| 831 |
// week. Also, rolling the week of the year can have seemingly |
| 832 |
// strange effects simply because the year of the week of year |
| 833 |
// may be different from the calendar year. For example, the |
| 834 |
// date Dec 28, 1997 is the first day of week 1 of 1998 (if |
| 835 |
// weeks start on Sunday and the minimal days in first week is |
| 836 |
// <= 3). |
| 837 |
int32_t woy = get(UCAL_WEEK_OF_YEAR, status); |
| 838 |
// Get the ISO year, which matches the week of year. This |
| 839 |
// may be one year before or after the calendar year. |
| 840 |
int32_t isoYear = get(UCAL_YEAR_WOY, status); |
| 841 |
int32_t isoDoy = internalGet(UCAL_DAY_OF_YEAR); |
| 842 |
int32_t month = internalGetMonth(status); |
| 843 |
if (U_FAILURE(status)) { |
| 844 |
return; |
| 845 |
} |
| 846 |
if (month == UCAL_JANUARY) { |
| 847 |
if (woy >= 52) { |
| 848 |
isoDoy += handleGetYearLength(isoYear, status); |
| 849 |
} |
| 850 |
} else { |
| 851 |
if (woy == 1) { |
| 852 |
isoDoy -= handleGetYearLength(isoYear - 1, status); |
| 853 |
} |
| 854 |
} |
| 855 |
if (U_FAILURE(status)) return; |
| 856 |
if (uprv_add32_overflow(woy, amount, &woy)) { |
| 857 |
status = U_ILLEGAL_ARGUMENT_ERROR; |
| 858 |
return; |
| 859 |
} |
| 860 |
// Do fast checks to avoid unnecessary computation: |
| 861 |
if (woy < 1 || woy > 52) { |
| 862 |
// Determine the last week of the ISO year. |
| 863 |
// We do this using the standard formula we use |
| 864 |
// everywhere in this file. If we can see that the |
| 865 |
// days at the end of the year are going to fall into |
| 866 |
// week 1 of the next year, we drop the last week by |
| 867 |
// subtracting 7 from the last day of the year. |
| 868 |
int32_t lastDoy = handleGetYearLength(isoYear, status); |
| 869 |
if (U_FAILURE(status)) return; |
| 870 |
int32_t lastRelDow = (lastDoy - isoDoy + internalGet(UCAL_DAY_OF_WEEK) - |
| 871 |
getFirstDayOfWeek()) % 7; |
| 872 |
if (lastRelDow < 0) lastRelDow += 7; |
| 873 |
if ((6 - lastRelDow) >= getMinimalDaysInFirstWeek()) lastDoy -= 7; |
| 874 |
int32_t lastWoy = weekNumber(lastDoy, lastRelDow + 1); |
| 875 |
woy = ((woy + lastWoy - 1) % lastWoy) + 1; |
| 876 |
} |
| 877 |
set(UCAL_WEEK_OF_YEAR, woy); |
| 878 |
set(UCAL_YEAR_WOY,isoYear); |
| 879 |
return; |
| 880 |
} |
| 881 |
|
| 882 |
case UCAL_DAY_OF_MONTH: |
| 883 |
if( !inCutoverMonth ) { |
| 884 |
Calendar::roll(field, amount, status); |
| 885 |
return; |
| 886 |
} |
| 887 |
{ |
| 888 |
// [j81] 1582 special case for DOM |
| 889 |
// The default computation works except when the current month |
| 890 |
// contains the Gregorian cutover. We handle this special case |
| 891 |
// here. [j81 - aliu] |
| 892 |
double monthLen = cMonthLen * kOneDay; |
| 893 |
double msIntoMonth = uprv_fmod(internalGetTime() - cMonthStart + |
| 894 |
amount * kOneDay, monthLen); |
| 895 |
if (msIntoMonth < 0) { |
| 896 |
msIntoMonth += monthLen; |
| 897 |
} |
| 898 |
#if defined (U_DEBUG_CAL) |
| 899 |
fprintf(stderr, "%s:%d: roll DOM %d -> %.0lf ms \n", |
| 900 |
__FILE__, __LINE__,amount, cMonthLen, cMonthStart+msIntoMonth); |
| 901 |
#endif |
| 902 |
setTimeInMillis(cMonthStart + msIntoMonth, status); |
| 903 |
return; |
| 904 |
} |
| 905 |
|
| 906 |
case UCAL_WEEK_OF_MONTH: |
| 907 |
if( !inCutoverMonth ) { |
| 908 |
Calendar::roll(field, amount, status); |
| 909 |
return; |
| 910 |
} |
| 911 |
{ |
| 912 |
#if defined (U_DEBUG_CAL) |
| 913 |
fprintf(stderr, "%s:%d: roll WOM %d ??????????????????? \n", |
| 914 |
__FILE__, __LINE__,amount); |
| 915 |
#endif |
| 916 |
// NOTE: following copied from the old |
| 917 |
// GregorianCalendar::roll( WEEK_OF_MONTH ) code |
| 918 |
|
| 919 |
// This is tricky, because during the roll we may have to shift |
| 920 |
// to a different day of the week. For example: |
| 921 |
|
| 922 |
// s m t w r f s |
| 923 |
// 1 2 3 4 5 |
| 924 |
// 6 7 8 9 10 11 12 |
| 925 |
|
| 926 |
// When rolling from the 6th or 7th back one week, we go to the |
| 927 |
// 1st (assuming that the first partial week counts). The same |
| 928 |
// thing happens at the end of the month. |
| 929 |
|
| 930 |
// The other tricky thing is that we have to figure out whether |
| 931 |
// the first partial week actually counts or not, based on the |
| 932 |
// minimal first days in the week. And we have to use the |
| 933 |
// correct first day of the week to delineate the week |
| 934 |
// boundaries. |
| 935 |
|
| 936 |
// Here's our algorithm. First, we find the real boundaries of |
| 937 |
// the month. Then we discard the first partial week if it |
| 938 |
// doesn't count in this locale. Then we fill in the ends with |
| 939 |
// phantom days, so that the first partial week and the last |
| 940 |
// partial week are full weeks. We then have a nice square |
| 941 |
// block of weeks. We do the usual rolling within this block, |
| 942 |
// as is done elsewhere in this method. If we wind up on one of |
| 943 |
// the phantom days that we added, we recognize this and pin to |
| 944 |
// the first or the last day of the month. Easy, eh? |
| 945 |
|
| 946 |
// Another wrinkle: To fix jitterbug 81, we have to make all this |
| 947 |
// work in the oddball month containing the Gregorian cutover. |
| 948 |
// This month is 10 days shorter than usual, and also contains |
| 949 |
// a discontinuity in the days; e.g., the default cutover month |
| 950 |
// is Oct 1582, and goes from day of month 4 to day of month 15. |
| 951 |
|
| 952 |
// Normalize the DAY_OF_WEEK so that 0 is the first day of the week |
| 953 |
// in this locale. We have dow in 0..6. |
| 954 |
int32_t dow = internalGet(UCAL_DAY_OF_WEEK) - getFirstDayOfWeek(); |
| 955 |
if (dow < 0) |
| 956 |
dow += 7; |
| 957 |
|
| 958 |
// Find the day of month, compensating for cutover discontinuity. |
| 959 |
int32_t dom = cDayOfMonth; |
| 960 |
|
| 961 |
// Find the day of the week (normalized for locale) for the first |
| 962 |
// of the month. |
| 963 |
int32_t fdm = (dow - dom + 1) % 7; |
| 964 |
if (fdm < 0) |
| 965 |
fdm += 7; |
| 966 |
|
| 967 |
// Get the first day of the first full week of the month, |
| 968 |
// including phantom days, if any. Figure out if the first week |
| 969 |
// counts or not; if it counts, then fill in phantom days. If |
| 970 |
// not, advance to the first real full week (skip the partial week). |
| 971 |
int32_t start; |
| 972 |
if ((7 - fdm) < getMinimalDaysInFirstWeek()) |
| 973 |
start = 8 - fdm; // Skip the first partial week |
| 974 |
else |
| 975 |
start = 1 - fdm; // This may be zero or negative |
| 976 |
|
| 977 |
// Get the day of the week (normalized for locale) for the last |
| 978 |
// day of the month. |
| 979 |
int32_t monthLen = cMonthLen; |
| 980 |
int32_t ldm = (monthLen - dom + dow) % 7; |
| 981 |
// We know monthLen >= DAY_OF_MONTH so we skip the += 7 step here. |
| 982 |
|
| 983 |
// Get the limit day for the blocked-off rectangular month; that |
| 984 |
// is, the day which is one past the last day of the month, |
| 985 |
// after the month has already been filled in with phantom days |
| 986 |
// to fill out the last week. This day has a normalized DOW of 0. |
| 987 |
int32_t limit = monthLen + 7 - ldm; |
| 988 |
|
| 989 |
// Now roll between start and (limit - 1). |
| 990 |
int32_t gap = limit - start; |
| 991 |
int32_t newDom = (dom + amount*7 - start) % gap; |
| 992 |
if (newDom < 0) |
| 993 |
newDom += gap; |
| 994 |
newDom += start; |
| 995 |
|
| 996 |
// Finally, pin to the real start and end of the month. |
| 997 |
if (newDom < 1) |
| 998 |
newDom = 1; |
| 999 |
if (newDom > monthLen) |
| 1000 |
newDom = monthLen; |
| 1001 |
|
| 1002 |
// Set the DAY_OF_MONTH. We rely on the fact that this field |
| 1003 |
// takes precedence over everything else (since all other fields |
| 1004 |
// are also set at this point). If this fact changes (if the |
| 1005 |
// disambiguation algorithm changes) then we will have to unset |
| 1006 |
// the appropriate fields here so that DAY_OF_MONTH is attended |
| 1007 |
// to. |
| 1008 |
|
| 1009 |
// If we are in the cutover month, manipulate ms directly. Don't do |
| 1010 |
// this in general because it doesn't work across DST boundaries |
| 1011 |
// (details, details). This takes care of the discontinuity. |
| 1012 |
setTimeInMillis(cMonthStart + (newDom-1)*kOneDay, status); |
| 1013 |
return; |
| 1014 |
} |
| 1015 |
|
| 1016 |
default: |
| 1017 |
Calendar::roll(field, amount, status); |
| 1018 |
return; |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
// ------------------------------------- |
| 1023 |
|
| 1024 |
|
| 1025 |
/** |
| 1026 |
* Return the minimum value that this field could have, given the current date. |
| 1027 |
* For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum(). |
| 1028 |
* @param field the time field. |
| 1029 |
* @return the minimum value that this field could have, given the current date. |
| 1030 |
* @deprecated ICU 2.6. Use getActualMinimum(UCalendarDateFields field) instead. |
| 1031 |
*/ |
| 1032 |
int32_t GregorianCalendar::getActualMinimum(EDateFields field) const |
| 1033 |
{ |
| 1034 |
return getMinimum(static_cast<UCalendarDateFields>(field)); |
| 1035 |
} |
| 1036 |
|
| 1037 |
int32_t GregorianCalendar::getActualMinimum(EDateFields field, UErrorCode& /* status */) const |
| 1038 |
{ |
| 1039 |
return getMinimum(static_cast<UCalendarDateFields>(field)); |
| 1040 |
} |
| 1041 |
|
| 1042 |
/** |
| 1043 |
* Return the minimum value that this field could have, given the current date. |
| 1044 |
* For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum(). |
| 1045 |
* @param field the time field. |
| 1046 |
* @return the minimum value that this field could have, given the current date. |
| 1047 |
* @draft ICU 2.6. |
| 1048 |
*/ |
| 1049 |
int32_t GregorianCalendar::getActualMinimum(UCalendarDateFields field, UErrorCode& /* status */) const |
| 1050 |
{ |
| 1051 |
return getMinimum(field); |
| 1052 |
} |
| 1053 |
|
| 1054 |
|
| 1055 |
// ------------------------------------ |
| 1056 |
|
| 1057 |
/** |
| 1058 |
* Old year limits were least max 292269054, max 292278994. |
| 1059 |
*/ |
| 1060 |
|
| 1061 |
/** |
| 1062 |
* @stable ICU 2.0 |
| 1063 |
*/ |
| 1064 |
int32_t GregorianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const { |
| 1065 |
return kGregorianCalendarLimits[field][limitType]; |
| 1066 |
} |
| 1067 |
|
| 1068 |
/** |
| 1069 |
* Return the maximum value that this field could have, given the current date. |
| 1070 |
* For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual |
| 1071 |
* maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar, |
| 1072 |
* for some years the actual maximum for MONTH is 12, and for others 13. |
| 1073 |
* @stable ICU 2.0 |
| 1074 |
*/ |
| 1075 |
int32_t GregorianCalendar::getActualMaximum(UCalendarDateFields field, UErrorCode& status) const |
| 1076 |
{ |
| 1077 |
/* It is a known limitation that the code here (and in getActualMinimum) |
| 1078 |
* won't behave properly at the extreme limits of GregorianCalendar's |
| 1079 |
* representable range (except for the code that handles the YEAR |
| 1080 |
* field). That's because the ends of the representable range are at |
| 1081 |
* odd spots in the year. For calendars with the default Gregorian |
| 1082 |
* cutover, these limits are Sun Dec 02 16:47:04 GMT 292269055 BC to Sun |
| 1083 |
* Aug 17 07:12:55 GMT 292278994 AD, somewhat different for non-GMT |
| 1084 |
* zones. As a result, if the calendar is set to Aug 1 292278994 AD, |
| 1085 |
* the actual maximum of DAY_OF_MONTH is 17, not 30. If the date is Mar |
| 1086 |
* 31 in that year, the actual maximum month might be Jul, whereas is |
| 1087 |
* the date is Mar 15, the actual maximum might be Aug -- depending on |
| 1088 |
* the precise semantics that are desired. Similar considerations |
| 1089 |
* affect all fields. Nonetheless, this effect is sufficiently arcane |
| 1090 |
* that we permit it, rather than complicating the code to handle such |
| 1091 |
* intricacies. - liu 8/20/98 |
| 1092 |
|
| 1093 |
* UPDATE: No longer true, since we have pulled in the limit values on |
| 1094 |
* the year. - Liu 11/6/00 */ |
| 1095 |
|
| 1096 |
switch (field) { |
| 1097 |
|
| 1098 |
case UCAL_YEAR: |
| 1099 |
/* The year computation is no different, in principle, from the |
| 1100 |
* others, however, the range of possible maxima is large. In |
| 1101 |
* addition, the way we know we've exceeded the range is different. |
| 1102 |
* For these reasons, we use the special case code below to handle |
| 1103 |
* this field. |
| 1104 |
* |
| 1105 |
* The actual maxima for YEAR depend on the type of calendar: |
| 1106 |
* |
| 1107 |
* Gregorian = May 17, 292275056 BC - Aug 17, 292278994 AD |
| 1108 |
* Julian = Dec 2, 292269055 BC - Jan 3, 292272993 AD |
| 1109 |
* Hybrid = Dec 2, 292269055 BC - Aug 17, 292278994 AD |
| 1110 |
* |
| 1111 |
* We know we've exceeded the maximum when either the month, date, |
| 1112 |
* time, or era changes in response to setting the year. We don't |
| 1113 |
* check for month, date, and time here because the year and era are |
| 1114 |
* sufficient to detect an invalid year setting. NOTE: If code is |
| 1115 |
* added to check the month and date in the future for some reason, |
| 1116 |
* Feb 29 must be allowed to shift to Mar 1 when setting the year. |
| 1117 |
*/ |
| 1118 |
{ |
| 1119 |
if(U_FAILURE(status)) return 0; |
| 1120 |
Calendar *cal = clone(); |
| 1121 |
if(!cal) { |
| 1122 |
status = U_MEMORY_ALLOCATION_ERROR; |
| 1123 |
return 0; |
| 1124 |
} |
| 1125 |
|
| 1126 |
cal->setLenient(true); |
| 1127 |
|
| 1128 |
int32_t era = cal->get(UCAL_ERA, status); |
| 1129 |
UDate d = cal->getTime(status); |
| 1130 |
|
| 1131 |
/* Perform a binary search, with the invariant that lowGood is a |
| 1132 |
* valid year, and highBad is an out of range year. |
| 1133 |
*/ |
| 1134 |
int32_t lowGood = kGregorianCalendarLimits[UCAL_YEAR][1]; |
| 1135 |
int32_t highBad = kGregorianCalendarLimits[UCAL_YEAR][2]+1; |
| 1136 |
while ((lowGood + 1) < highBad) { |
| 1137 |
int32_t y = (lowGood + highBad) / 2; |
| 1138 |
cal->set(UCAL_YEAR, y); |
| 1139 |
if (cal->get(UCAL_YEAR, status) == y && cal->get(UCAL_ERA, status) == era) { |
| 1140 |
lowGood = y; |
| 1141 |
} else { |
| 1142 |
highBad = y; |
| 1143 |
cal->setTime(d, status); // Restore original fields |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|
| 1147 |
delete cal; |
| 1148 |
return lowGood; |
| 1149 |
} |
| 1150 |
|
| 1151 |
default: |
| 1152 |
return Calendar::getActualMaximum(field,status); |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
|
| 1157 |
int32_t GregorianCalendar::handleGetExtendedYear(UErrorCode& status) { |
| 1158 |
if (U_FAILURE(status)) { |
| 1159 |
return 0; |
| 1160 |
} |
| 1161 |
// the year to return |
| 1162 |
int32_t year = kEpochYear; |
| 1163 |
|
| 1164 |
// year field to use |
| 1165 |
// There are three separate fields which could be used to |
| 1166 |
// derive the proper year. Use the one most recently set. |
| 1167 |
UCalendarDateFields yearField = newerField( |
| 1168 |
newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR), UCAL_YEAR_WOY); |
| 1169 |
|
| 1170 |
// based on the "best" year field, get the year |
| 1171 |
switch(yearField) { |
| 1172 |
case UCAL_EXTENDED_YEAR: |
| 1173 |
year = internalGet(UCAL_EXTENDED_YEAR, kEpochYear); |
| 1174 |
break; |
| 1175 |
|
| 1176 |
case UCAL_YEAR: |
| 1177 |
{ |
| 1178 |
// The year defaults to the epoch start, the era to AD |
| 1179 |
int32_t era = internalGet(UCAL_ERA, AD); |
| 1180 |
if (era == BC) { |
| 1181 |
year = 1 - internalGet(UCAL_YEAR, 1); // Convert to extended year |
| 1182 |
} else if (era == AD) { |
| 1183 |
year = internalGet(UCAL_YEAR, kEpochYear); |
| 1184 |
} else { |
| 1185 |
status = U_ILLEGAL_ARGUMENT_ERROR; |
| 1186 |
return 0; |
| 1187 |
} |
| 1188 |
} |
| 1189 |
break; |
| 1190 |
|
| 1191 |
case UCAL_YEAR_WOY: |
| 1192 |
year = handleGetExtendedYearFromWeekFields( |
| 1193 |
internalGet(UCAL_YEAR_WOY), internalGet(UCAL_WEEK_OF_YEAR), status); |
| 1194 |
if (U_FAILURE(status)) { |
| 1195 |
return 0; |
| 1196 |
} |
| 1197 |
#if defined (U_DEBUG_CAL) |
| 1198 |
// if(internalGet(UCAL_YEAR_WOY) != year) { |
| 1199 |
fprintf(stderr, "%s:%d: hGEYFWF[%d,%d] -> %d\n", |
| 1200 |
__FILE__, __LINE__,internalGet(UCAL_YEAR_WOY),internalGet(UCAL_WEEK_OF_YEAR),year); |
| 1201 |
//} |
| 1202 |
#endif |
| 1203 |
break; |
| 1204 |
|
| 1205 |
default: |
| 1206 |
year = kEpochYear; |
| 1207 |
} |
| 1208 |
return year; |
| 1209 |
} |
| 1210 |
|
| 1211 |
int32_t GregorianCalendar::handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy, UErrorCode& status) |
| 1212 |
{ |
| 1213 |
if (U_FAILURE(status)) { |
| 1214 |
return 0; |
| 1215 |
} |
| 1216 |
// convert year to extended form |
| 1217 |
int32_t era = internalGet(UCAL_ERA, AD); |
| 1218 |
if(era == BC) { |
| 1219 |
yearWoy = 1 - yearWoy; |
| 1220 |
} |
| 1221 |
return Calendar::handleGetExtendedYearFromWeekFields(yearWoy, woy, status); |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
// ------------------------------------- |
| 1226 |
|
| 1227 |
/** |
| 1228 |
* Return the ERA. We need a special method for this because the |
| 1229 |
* default ERA is AD, but a zero (unset) ERA is BC. |
| 1230 |
*/ |
| 1231 |
int32_t |
| 1232 |
GregorianCalendar::internalGetEra() const { |
| 1233 |
return isSet(UCAL_ERA) ? internalGet(UCAL_ERA) : static_cast<int32_t>(AD); |
| 1234 |
} |
| 1235 |
|
| 1236 |
const char * |
| 1237 |
GregorianCalendar::getType() const { |
| 1238 |
//static const char kGregorianType = "gregorian"; |
| 1239 |
|
| 1240 |
return "gregorian"; |
| 1241 |
} |
| 1242 |
|
| 1243 |
IMPL_SYSTEM_DEFAULT_CENTURY(GregorianCalendar, "@calendar=gregory") |
| 1244 |
|
| 1245 |
U_NAMESPACE_END |
| 1246 |
|
| 1247 |
#endif /* #if !UCONFIG_NO_FORMATTING */ |
| 1248 |
|
| 1249 |
//eof |