]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/calendar.cpp
ICU-400.40.tar.gz
[apple/icu.git] / icuSources / i18n / calendar.cpp
1 /*
2 *******************************************************************************
3 * Copyright (C) 1997-2008, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
6 *
7 * File CALENDAR.CPP
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 02/03/97 clhuang Creation.
13 * 04/22/97 aliu Cleaned up, fixed memory leak, made
14 * setWeekCountData() more robust.
15 * Moved platform code to TPlatformUtilities.
16 * 05/01/97 aliu Made equals(), before(), after() arguments const.
17 * 05/20/97 aliu Changed logic of when to compute fields and time
18 * to fix bugs.
19 * 08/12/97 aliu Added equivalentTo. Misc other fixes.
20 * 07/28/98 stephen Sync up with JDK 1.2
21 * 09/02/98 stephen Sync with JDK 1.2 8/31 build (getActualMin/Max)
22 * 03/17/99 stephen Changed adoptTimeZone() - now fAreFieldsSet is
23 * set to FALSE to force update of time.
24 *******************************************************************************
25 */
26
27 #include "unicode/utypes.h"
28
29 #if !UCONFIG_NO_FORMATTING
30
31 #include "unicode/gregocal.h"
32 #include "gregoimp.h"
33 #include "buddhcal.h"
34 #include "taiwncal.h"
35 #include "japancal.h"
36 #include "islamcal.h"
37 #include "hebrwcal.h"
38 #include "persncal.h"
39 #include "indiancal.h"
40 #include "chnsecal.h"
41 #include "coptccal.h"
42 #include "ethpccal.h"
43 #include "unicode/calendar.h"
44 #include "cpputils.h"
45 #include "servloc.h"
46 #include "ucln_in.h"
47 #include "cstring.h"
48 #include "locbased.h"
49 #include "uresimp.h"
50
51 #if !UCONFIG_NO_SERVICE
52 static U_NAMESPACE_QUALIFIER ICULocaleService* gService = NULL;
53 #endif
54
55 // INTERNAL - for cleanup
56
57 U_CDECL_BEGIN
58 static UBool calendar_cleanup(void) {
59 #if !UCONFIG_NO_SERVICE
60 if (gService) {
61 delete gService;
62 gService = NULL;
63 }
64 #endif
65 return TRUE;
66 }
67 U_CDECL_END
68
69 // ------------------------------------------
70 //
71 // Registration
72 //
73 //-------------------------------------------
74 //#define U_DEBUG_CALSVC 1
75 //
76
77 #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL)
78
79 /**
80 * fldName was removed as a duplicate implementation.
81 * use udbg_ services instead,
82 * which depend on include files and library from ../tools/ctestfw
83 */
84 #include "unicode/udbgutil.h"
85 #include <stdio.h>
86
87 /**
88 * convert a UCalendarDateFields into a string - for debugging
89 * @param f field enum
90 * @return static string to the field name
91 * @internal
92 */
93
94 const char* fldName(UCalendarDateFields f) {
95 return udbg_enumName(UDBG_UCalendarDateFields, (int32_t)f);
96 }
97
98 #if UCAL_DEBUG_DUMP
99 // from CalendarTest::calToStr - but doesn't modify contents.
100 void ucal_dump(const Calendar &cal) {
101 cal.dump();
102 }
103
104 void Calendar::dump() const {
105 int i;
106 fprintf(stderr, "@calendar=%s, timeset=%c, fieldset=%c, allfields=%c, virtualset=%c, t=%.2f",
107 getType(), fIsTimeSet?'y':'n', fAreFieldsSet?'y':'n', fAreAllFieldsSet?'y':'n',
108 fAreFieldsVirtuallySet?'y':'n',
109 fTime);
110
111 // can add more things here: DST, zone, etc.
112 fprintf(stderr, "\n");
113 for(i = 0;i<UCAL_FIELD_COUNT;i++) {
114 int n;
115 const char *f = fldName((UCalendarDateFields)i);
116 fprintf(stderr, " %25s: %-11ld", f, fFields[i]);
117 if(fStamp[i] == kUnset) {
118 fprintf(stderr, " (unset) ");
119 } else if(fStamp[i] == kInternallySet) {
120 fprintf(stderr, " (internally set) ");
121 //} else if(fStamp[i] == kInternalDefault) {
122 // fprintf(stderr, " (internal default) ");
123 } else {
124 fprintf(stderr, " %%%d ", fStamp[i]);
125 }
126 fprintf(stderr, "\n");
127
128 }
129 }
130
131 U_CFUNC void ucal_dump(UCalendar* cal) {
132 ucal_dump( *((Calendar*)cal) );
133 }
134 #endif
135
136 #endif
137
138 static const char * const gCalendarKeywords[] = {
139 "gregorian",
140 "japanese",
141 "buddhist",
142 "roc",
143 "persian",
144 "islamic-civil",
145 "islamic",
146 "hebrew",
147 "chinese",
148 "indian",
149 "coptic",
150 "ethiopic",
151 "ethiopic-amete-alem",
152 NULL
153 };
154
155 U_NAMESPACE_BEGIN
156
157 static UBool isStandardSupportedKeyword(const char *keyword, UErrorCode& status) {
158 if(U_FAILURE(status)) {
159 return FALSE;
160 }
161 for(int32_t i=0; gCalendarKeywords[i] != NULL; i++) {
162 if(uprv_strcmp(gCalendarKeywords[i], keyword) == 0) {
163 return TRUE;
164 }
165 }
166 return FALSE;
167 }
168
169 static void getCalendarKeyword(const UnicodeString &id, char *targetBuffer, int32_t targetBufferSize) {
170 UnicodeString calendarKeyword = UNICODE_STRING_SIMPLE("calendar=");
171 int32_t calKeyLen = calendarKeyword.length();
172 int32_t keyLen = 0;
173
174 int32_t keywordIdx = id.indexOf((UChar)0x003D); /* '=' */
175 if (id[0] == 0x40/*'@'*/
176 && id.compareBetween(1, keywordIdx+1, calendarKeyword, 0, calKeyLen) == 0)
177 {
178 keyLen = id.extract(keywordIdx+1, id.length(), targetBuffer, targetBufferSize, US_INV);
179 }
180 targetBuffer[keyLen] = 0;
181 }
182
183 static Calendar *createStandardCalendar(char *calType, const Locale &canLoc, UErrorCode& status) {
184 #ifdef U_DEBUG_CALSVC
185 fprintf(stderr, "BasicCalendarFactory %p: creating type for %s\n",
186 this, (const char*)curLoc.getName());
187 fflush(stderr);
188 #endif
189
190 if(!calType || !*calType || !uprv_strcmp(calType,"gregorian")) { // Gregorian (default)
191 return new GregorianCalendar(canLoc, status);
192 } else if(!uprv_strcmp(calType, "japanese")) {
193 return new JapaneseCalendar(canLoc, status);
194 } else if(!uprv_strcmp(calType, "buddhist")) {
195 return new BuddhistCalendar(canLoc, status);
196 } else if(!uprv_strcmp(calType, "roc")) {
197 return new TaiwanCalendar(canLoc, status);
198 } else if(!uprv_strcmp(calType, "islamic-civil")) {
199 return new IslamicCalendar(canLoc, status, IslamicCalendar::CIVIL);
200 } else if(!uprv_strcmp(calType, "islamic")) {
201 return new IslamicCalendar(canLoc, status, IslamicCalendar::ASTRONOMICAL);
202 } else if(!uprv_strcmp(calType, "hebrew")) {
203 return new HebrewCalendar(canLoc, status);
204 } else if(!uprv_strcmp(calType, "persian")) {
205 return new PersianCalendar(canLoc, status);
206 } else if(!uprv_strcmp(calType, "chinese")) {
207 return new ChineseCalendar(canLoc, status);
208 } else if(!uprv_strcmp(calType, "indian")) {
209 return new IndianCalendar(canLoc, status);
210 } else if(!uprv_strcmp(calType, "coptic")) {
211 return new CopticCalendar(canLoc, status);
212 } else if(!uprv_strcmp(calType, "ethiopic")) {
213 return new EthiopicCalendar(canLoc, status, EthiopicCalendar::AMETE_MIHRET_ERA);
214 } else if(!uprv_strcmp(calType, "ethiopic-amete-alem")) {
215 return new EthiopicCalendar(canLoc, status, EthiopicCalendar::AMETE_ALEM_ERA);
216 } else {
217 status = U_UNSUPPORTED_ERROR;
218 return NULL;
219 }
220 }
221
222 #if !UCONFIG_NO_SERVICE
223
224 // -------------------------------------
225
226 /**
227 * a Calendar Factory which creates the "basic" calendar types, that is, those
228 * shipped with ICU.
229 */
230 class BasicCalendarFactory : public LocaleKeyFactory {
231 public:
232 /**
233 * @param calendarType static const string (caller owns storage - will be aliased) to calendar type
234 */
235 BasicCalendarFactory()
236 : LocaleKeyFactory(LocaleKeyFactory::INVISIBLE) { }
237
238 virtual ~BasicCalendarFactory() {}
239
240 protected:
241 //virtual UBool isSupportedID( const UnicodeString& id, UErrorCode& status) const {
242 // if(U_FAILURE(status)) {
243 // return FALSE;
244 // }
245 // char keyword[ULOC_FULLNAME_CAPACITY];
246 // getCalendarKeyword(id, keyword, (int32_t)sizeof(keyword));
247 // return isStandardSupportedKeyword(keyword, status);
248 //}
249
250 virtual void updateVisibleIDs(Hashtable& result, UErrorCode& status) const
251 {
252 if (U_SUCCESS(status)) {
253 for(int32_t i=0;gCalendarKeywords[i] != NULL;i++) {
254 UnicodeString id((UChar)0x40); /* '@' a variant character */
255 id.append(UNICODE_STRING_SIMPLE("calendar="));
256 id.append(UnicodeString(gCalendarKeywords[i], -1, US_INV));
257 result.put(id, (void*)this, status);
258 }
259 }
260 }
261
262 virtual UObject* create(const ICUServiceKey& key, const ICUService* /*service*/, UErrorCode& status) const {
263 #ifdef U_DEBUG_CALSVC
264 if(key.getDynamicClassID() != LocaleKey::getStaticClassID()) {
265 fprintf(stderr, "::create - not a LocaleKey!\n");
266 }
267 #endif
268 const LocaleKey& lkey = (LocaleKey&)key;
269 Locale curLoc; // current locale
270 Locale canLoc; // Canonical locale
271
272 lkey.currentLocale(curLoc);
273 lkey.canonicalLocale(canLoc);
274
275 char keyword[ULOC_FULLNAME_CAPACITY];
276 UnicodeString str;
277
278 key.currentID(str);
279 getCalendarKeyword(str, keyword, (int32_t) sizeof(keyword));
280
281 #ifdef U_DEBUG_CALSVC
282 fprintf(stderr, "BasicCalendarFactory::create() - cur %s, can %s\n", (const char*)curLoc.getName(), (const char*)canLoc.getName());
283 #endif
284
285 if(!isStandardSupportedKeyword(keyword,status)) { // Do we handle this type?
286 #ifdef U_DEBUG_CALSVC
287
288 fprintf(stderr, "BasicCalendarFactory - not handling %s.[%s]\n", (const char*) curLoc.getName(), tmp );
289 #endif
290 return NULL;
291 }
292
293 return createStandardCalendar(keyword, canLoc, status);
294 }
295 };
296
297
298 /**
299 * A factory which looks up the DefaultCalendar resource to determine which class of calendar to use
300 */
301
302 class DefaultCalendarFactory : public ICUResourceBundleFactory {
303 public:
304 DefaultCalendarFactory(): ICUResourceBundleFactory() { }
305 protected:
306 virtual UObject* create(const ICUServiceKey& key, const ICUService* /*service*/, UErrorCode& status) const {
307
308 LocaleKey &lkey = (LocaleKey&)key;
309 Locale loc;
310 lkey.currentLocale(loc);
311
312 UnicodeString myString;
313
314 // attempt keyword lookup
315 char keyword[128];
316
317 if(!loc.getKeywordValue("calendar", keyword, sizeof(keyword)-1, status)) {
318 // fetch default calendar id
319 char funcEquiv[ULOC_FULLNAME_CAPACITY];
320 ures_getFunctionalEquivalent(funcEquiv, sizeof(funcEquiv)-1,
321 NULL, "calendar", "calendar",
322 loc.getName(),
323 NULL, FALSE, &status);
324 uloc_getKeywordValue(funcEquiv, "calendar", keyword,
325 sizeof(keyword)-1, &status);
326 #ifdef U_DEBUG_CALSVC
327 fprintf(stderr, " getFunctionalEquivalent calendar=%s [%s]\n", keyword, u_errorName(status));
328 #endif
329 }
330 #ifdef U_DEBUG_CALSVC
331 else { fprintf(stderr, " explicit calendar=%s\n", keyword); }
332 #endif
333
334
335 if(U_FAILURE(status)) {
336 return NULL;
337 } else {
338 UnicodeString *ret = new UnicodeString();
339 if (ret == NULL) {
340 status = U_MEMORY_ALLOCATION_ERROR;
341 } else {
342 ret->append((UChar)0x40); // '@' is a variant character
343 ret->append(UNICODE_STRING("calendar=", 9));
344 (*ret) += UnicodeString(keyword,-1,US_INV);
345 }
346 return ret;
347 }
348 }
349 };
350
351 // -------------------------------------
352 class CalendarService : public ICULocaleService {
353 public:
354 CalendarService()
355 : ICULocaleService(UNICODE_STRING_SIMPLE("Calendar"))
356 {
357 UErrorCode status = U_ZERO_ERROR;
358 registerFactory(new DefaultCalendarFactory(), status);
359 }
360
361 virtual UObject* cloneInstance(UObject* instance) const {
362 if(instance->getDynamicClassID() == UnicodeString::getStaticClassID()) {
363 return ((UnicodeString*)instance)->clone();
364 } else {
365 #ifdef U_DEBUG_CALSVC_F
366 UErrorCode status2 = U_ZERO_ERROR;
367 fprintf(stderr, "Cloning a %s calendar with tz=%ld\n", ((Calendar*)instance)->getType(), ((Calendar*)instance)->get(UCAL_ZONE_OFFSET, status2));
368 #endif
369 return ((Calendar*)instance)->clone();
370 }
371 }
372
373 virtual UObject* handleDefault(const ICUServiceKey& key, UnicodeString* /*actualID*/, UErrorCode& status) const {
374 LocaleKey& lkey = (LocaleKey&)key;
375 //int32_t kind = lkey.kind();
376
377 Locale loc;
378 lkey.canonicalLocale(loc);
379
380 #ifdef U_DEBUG_CALSVC
381 Locale loc2;
382 lkey.currentLocale(loc2);
383 fprintf(stderr, "CalSvc:handleDefault for currentLoc %s, canloc %s\n", (const char*)loc.getName(), (const char*)loc2.getName());
384 #endif
385 Calendar *nc = new GregorianCalendar(loc, status);
386
387 #ifdef U_DEBUG_CALSVC
388 UErrorCode status2 = U_ZERO_ERROR;
389 fprintf(stderr, "New default calendar has tz=%d\n", ((Calendar*)nc)->get(UCAL_ZONE_OFFSET, status2));
390 #endif
391 return nc;
392 }
393
394 virtual UBool isDefault() const {
395 return countFactories() == 1;
396 }
397 };
398
399 // -------------------------------------
400
401 static inline UBool
402 isCalendarServiceUsed() {
403 UBool retVal;
404 UMTX_CHECK(NULL, gService != NULL, retVal);
405 return retVal;
406 }
407
408 // -------------------------------------
409
410 static ICULocaleService*
411 getCalendarService(UErrorCode &status)
412 {
413 UBool needInit;
414 UMTX_CHECK(NULL, (UBool)(gService == NULL), needInit);
415 if (needInit) {
416 #ifdef U_DEBUG_CALSVC
417 fprintf(stderr, "Spinning up Calendar Service\n");
418 #endif
419 ICULocaleService * newservice = new CalendarService();
420 if (newservice == NULL) {
421 status = U_MEMORY_ALLOCATION_ERROR;
422 return newservice;
423 }
424 #ifdef U_DEBUG_CALSVC
425 fprintf(stderr, "Registering classes..\n");
426 #endif
427
428 // Register all basic instances.
429 newservice->registerFactory(new BasicCalendarFactory(),status);
430
431 #ifdef U_DEBUG_CALSVC
432 fprintf(stderr, "Done..\n");
433 #endif
434
435 if(U_FAILURE(status)) {
436 #ifdef U_DEBUG_CALSVC
437 fprintf(stderr, "err (%s) registering classes, deleting service.....\n", u_errorName(status));
438 #endif
439 delete newservice;
440 newservice = NULL;
441 }
442
443 if (newservice) {
444 umtx_lock(NULL);
445 if (gService == NULL) {
446 gService = newservice;
447 newservice = NULL;
448 }
449 umtx_unlock(NULL);
450 }
451 if (newservice) {
452 delete newservice;
453 } else {
454 // we won the contention - we can register the cleanup.
455 ucln_i18n_registerCleanup(UCLN_I18N_CALENDAR, calendar_cleanup);
456 }
457 }
458 return gService;
459 }
460
461 URegistryKey Calendar::registerFactory(ICUServiceFactory* toAdopt, UErrorCode& status)
462 {
463 return getCalendarService(status)->registerFactory(toAdopt, status);
464 }
465
466 UBool Calendar::unregister(URegistryKey key, UErrorCode& status) {
467 return getCalendarService(status)->unregister(key, status);
468 }
469 #endif /* UCONFIG_NO_SERVICE */
470
471 // -------------------------------------
472
473 static const int32_t kCalendarLimits[UCAL_FIELD_COUNT][4] = {
474 // Minimum Greatest min Least max Greatest max
475 {/*N/A*/-1, /*N/A*/-1, /*N/A*/-1, /*N/A*/-1}, // ERA
476 {/*N/A*/-1, /*N/A*/-1, /*N/A*/-1, /*N/A*/-1}, // YEAR
477 {/*N/A*/-1, /*N/A*/-1, /*N/A*/-1, /*N/A*/-1}, // MONTH
478 {/*N/A*/-1, /*N/A*/-1, /*N/A*/-1, /*N/A*/-1}, // WEEK_OF_YEAR
479 {/*N/A*/-1, /*N/A*/-1, /*N/A*/-1, /*N/A*/-1}, // WEEK_OF_MONTH
480 {/*N/A*/-1, /*N/A*/-1, /*N/A*/-1, /*N/A*/-1}, // DAY_OF_MONTH
481 {/*N/A*/-1, /*N/A*/-1, /*N/A*/-1, /*N/A*/-1}, // DAY_OF_YEAR
482 { 1, 1, 7, 7 }, // DAY_OF_WEEK
483 {/*N/A*/-1, /*N/A*/-1, /*N/A*/-1, /*N/A*/-1}, // DAY_OF_WEEK_IN_MONTH
484 { 0, 0, 1, 1 }, // AM_PM
485 { 0, 0, 11, 11 }, // HOUR
486 { 0, 0, 23, 23 }, // HOUR_OF_DAY
487 { 0, 0, 59, 59 }, // MINUTE
488 { 0, 0, 59, 59 }, // SECOND
489 { 0, 0, 999, 999 }, // MILLISECOND
490 {-12*kOneHour, -12*kOneHour, 12*kOneHour, 15*kOneHour }, // ZONE_OFFSET
491 { 0, 0, 1*kOneHour, 1*kOneHour }, // DST_OFFSET
492 {/*N/A*/-1, /*N/A*/-1, /*N/A*/-1, /*N/A*/-1}, // YEAR_WOY
493 { 1, 1, 7, 7 }, // DOW_LOCAL
494 {/*N/A*/-1, /*N/A*/-1, /*N/A*/-1, /*N/A*/-1}, // EXTENDED_YEAR
495 { -0x7F000000, -0x7F000000, 0x7F000000, 0x7F000000 }, // JULIAN_DAY
496 { 0, 0, 24*kOneHour-1, 24*kOneHour-1 }, // MILLISECONDS_IN_DAY
497 { 0, 0, 1, 1 }, // IS_LEAP_MONTH
498 };
499
500 // Resource bundle tags read by this class
501 static const char gDateTimeElements[] = "DateTimeElements";
502
503 // Data flow in Calendar
504 // ---------------------
505
506 // The current time is represented in two ways by Calendar: as UTC
507 // milliseconds from the epoch start (1 January 1970 0:00 UTC), and as local
508 // fields such as MONTH, HOUR, AM_PM, etc. It is possible to compute the
509 // millis from the fields, and vice versa. The data needed to do this
510 // conversion is encapsulated by a TimeZone object owned by the Calendar.
511 // The data provided by the TimeZone object may also be overridden if the
512 // user sets the ZONE_OFFSET and/or DST_OFFSET fields directly. The class
513 // keeps track of what information was most recently set by the caller, and
514 // uses that to compute any other information as needed.
515
516 // If the user sets the fields using set(), the data flow is as follows.
517 // This is implemented by the Calendar subclass's computeTime() method.
518 // During this process, certain fields may be ignored. The disambiguation
519 // algorithm for resolving which fields to pay attention to is described
520 // above.
521
522 // local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.)
523 // |
524 // | Using Calendar-specific algorithm
525 // V
526 // local standard millis
527 // |
528 // | Using TimeZone or user-set ZONE_OFFSET / DST_OFFSET
529 // V
530 // UTC millis (in time data member)
531
532 // If the user sets the UTC millis using setTime(), the data flow is as
533 // follows. This is implemented by the Calendar subclass's computeFields()
534 // method.
535
536 // UTC millis (in time data member)
537 // |
538 // | Using TimeZone getOffset()
539 // V
540 // local standard millis
541 // |
542 // | Using Calendar-specific algorithm
543 // V
544 // local fields (YEAR, MONTH, DATE, HOUR, MINUTE, etc.)
545
546 // In general, a round trip from fields, through local and UTC millis, and
547 // back out to fields is made when necessary. This is implemented by the
548 // complete() method. Resolving a partial set of fields into a UTC millis
549 // value allows all remaining fields to be generated from that value. If
550 // the Calendar is lenient, the fields are also renormalized to standard
551 // ranges when they are regenerated.
552
553 // -------------------------------------
554
555 Calendar::Calendar(UErrorCode& success)
556 : UObject(),
557 fIsTimeSet(FALSE),
558 fAreFieldsSet(FALSE),
559 fAreAllFieldsSet(FALSE),
560 fAreFieldsVirtuallySet(FALSE),
561 fNextStamp((int32_t)kMinimumUserStamp),
562 fTime(0),
563 fLenient(TRUE),
564 fZone(0)
565 {
566 clear();
567 fZone = TimeZone::createDefault();
568 if (fZone == NULL) {
569 success = U_MEMORY_ALLOCATION_ERROR;
570 }
571 setWeekCountData(Locale::getDefault(), NULL, success);
572 }
573
574 // -------------------------------------
575
576 Calendar::Calendar(TimeZone* zone, const Locale& aLocale, UErrorCode& success)
577 : UObject(),
578 fIsTimeSet(FALSE),
579 fAreFieldsSet(FALSE),
580 fAreAllFieldsSet(FALSE),
581 fAreFieldsVirtuallySet(FALSE),
582 fNextStamp((int32_t)kMinimumUserStamp),
583 fTime(0),
584 fLenient(TRUE),
585 fZone(0)
586 {
587 if(zone == 0) {
588 #if defined (U_DEBUG_CAL)
589 fprintf(stderr, "%s:%d: ILLEGAL ARG because timezone cannot be 0\n",
590 __FILE__, __LINE__);
591 #endif
592 success = U_ILLEGAL_ARGUMENT_ERROR;
593 return;
594 }
595
596 clear();
597 fZone = zone;
598
599 setWeekCountData(aLocale, NULL, success);
600 }
601
602 // -------------------------------------
603
604 Calendar::Calendar(const TimeZone& zone, const Locale& aLocale, UErrorCode& success)
605 : UObject(),
606 fIsTimeSet(FALSE),
607 fAreFieldsSet(FALSE),
608 fAreAllFieldsSet(FALSE),
609 fAreFieldsVirtuallySet(FALSE),
610 fNextStamp((int32_t)kMinimumUserStamp),
611 fTime(0),
612 fLenient(TRUE),
613 fZone(0)
614 {
615 clear();
616 fZone = zone.clone();
617 if (fZone == NULL) {
618 success = U_MEMORY_ALLOCATION_ERROR;
619 }
620 setWeekCountData(aLocale, NULL, success);
621 }
622
623 // -------------------------------------
624
625 Calendar::~Calendar()
626 {
627 delete fZone;
628 }
629
630 // -------------------------------------
631
632 Calendar::Calendar(const Calendar &source)
633 : UObject(source)
634 {
635 fZone = 0;
636 *this = source;
637 }
638
639 // -------------------------------------
640
641 Calendar &
642 Calendar::operator=(const Calendar &right)
643 {
644 if (this != &right) {
645 uprv_arrayCopy(right.fFields, fFields, UCAL_FIELD_COUNT);
646 uprv_arrayCopy(right.fIsSet, fIsSet, UCAL_FIELD_COUNT);
647 uprv_arrayCopy(right.fStamp, fStamp, UCAL_FIELD_COUNT);
648 fTime = right.fTime;
649 fIsTimeSet = right.fIsTimeSet;
650 fAreAllFieldsSet = right.fAreAllFieldsSet;
651 fAreFieldsSet = right.fAreFieldsSet;
652 fAreFieldsVirtuallySet = right.fAreFieldsVirtuallySet;
653 fLenient = right.fLenient;
654 if (fZone != NULL) {
655 delete fZone;
656 }
657 if (right.fZone != NULL) {
658 fZone = right.fZone->clone();
659 }
660 fFirstDayOfWeek = right.fFirstDayOfWeek;
661 fMinimalDaysInFirstWeek = right.fMinimalDaysInFirstWeek;
662 fNextStamp = right.fNextStamp;
663 }
664
665 return *this;
666 }
667
668 // -------------------------------------
669
670 Calendar* U_EXPORT2
671 Calendar::createInstance(UErrorCode& success)
672 {
673 return createInstance(TimeZone::createDefault(), Locale::getDefault(), success);
674 }
675
676 // -------------------------------------
677
678 Calendar* U_EXPORT2
679 Calendar::createInstance(const TimeZone& zone, UErrorCode& success)
680 {
681 return createInstance(zone, Locale::getDefault(), success);
682 }
683
684 // -------------------------------------
685
686 Calendar* U_EXPORT2
687 Calendar::createInstance(const Locale& aLocale, UErrorCode& success)
688 {
689 return createInstance(TimeZone::createDefault(), aLocale, success);
690 }
691
692 // ------------------------------------- Adopting
693
694 // Note: this is the bottleneck that actually calls the service routines.
695
696 Calendar* U_EXPORT2
697 Calendar::createInstance(TimeZone* zone, const Locale& aLocale, UErrorCode& success)
698 {
699 Locale actualLoc;
700 UObject* u;
701 #if !UCONFIG_NO_SERVICE
702 if (isCalendarServiceUsed()) {
703 u = getCalendarService(success)->get(aLocale, LocaleKey::KIND_ANY, &actualLoc, success);
704 }
705 else
706 #endif
707 {
708 UErrorCode feErr;
709 char calLocaleType[ULOC_FULLNAME_CAPACITY];
710 calLocaleType[0] = 0; // NULL terminate
711 int32_t keywordCapacity = aLocale.getKeywordValue("calendar", calLocaleType, sizeof(calLocaleType)-1, success);
712 if (keywordCapacity == 0) {
713 char funcEquiv[ULOC_FULLNAME_CAPACITY];
714
715 feErr = success;
716
717 // fetch default calendar id
718 ures_getFunctionalEquivalent(funcEquiv, sizeof(funcEquiv)-1,
719 NULL, "calendar", "calendar",
720 aLocale.getName(),
721 NULL, FALSE, &feErr);
722 keywordCapacity = uloc_getKeywordValue(funcEquiv, "calendar", calLocaleType,
723 sizeof(calLocaleType)-1, &feErr); // This can fail if there is no data.
724 // Don't want to stop calendar construction just because we couldn't get this type.
725 if (keywordCapacity == 0 || U_FAILURE(feErr)) {
726 // no calendar type. Default to nothing.
727 calLocaleType[0] = 0;
728 }
729 #ifdef U_DEBUG_CALSVC
730 fprintf(stderr, " getFunctionalEquivalent calendar=%s [%s]\n", keyword, u_errorName(status));
731 #endif
732 }
733 #ifdef U_DEBUG_CALSVC
734 else { fprintf(stderr, " explicit calendar=%s\n", keyword); }
735 #endif
736 u = createStandardCalendar(calLocaleType, aLocale, success);
737 }
738 Calendar* c = NULL;
739
740 if(U_FAILURE(success) || !u) {
741 delete zone;
742 if(U_SUCCESS(success)) { // Propagate some kind of err
743 success = U_INTERNAL_PROGRAM_ERROR;
744 }
745 return NULL;
746 }
747
748 #if !UCONFIG_NO_SERVICE
749 if(u->getDynamicClassID() == UnicodeString::getStaticClassID()) {
750 // It's a unicode string telling us what type of calendar to load ("gregorian", etc)
751 const UnicodeString& str = *(UnicodeString*)u;
752
753 // Create a Locale over this string
754 Locale l("");
755 LocaleUtility::initLocaleFromName(str, l);
756
757 #ifdef U_DEBUG_CALSVC
758 fprintf(stderr, "Calendar::createInstance(%s), looking up [%s]\n", aLocale.getName(), l.getName());
759 #endif
760
761 Locale actualLoc2;
762 delete u;
763 u = NULL;
764
765 // Don't overwrite actualLoc, since the actual loc from this call
766 // may be something like "@calendar=gregorian" -- TODO investigate
767 // further...
768 c = (Calendar*)getCalendarService(success)->get(l, LocaleKey::KIND_ANY, &actualLoc2, success);
769
770 if(U_FAILURE(success) || !c) {
771 delete zone;
772 if(U_SUCCESS(success)) {
773 success = U_INTERNAL_PROGRAM_ERROR; // Propagate some err
774 }
775 return NULL;
776 }
777
778 if(c->getDynamicClassID() == UnicodeString::getStaticClassID()) {
779 // recursed! Second lookup returned a UnicodeString.
780 // Perhaps DefaultCalendar{} was set to another locale.
781 #ifdef U_DEBUG_CALSVC
782 char tmp[200];
783 const UnicodeString& str = *(UnicodeString*)c;
784 // Extract a char* out of it..
785 int32_t len = str.length();
786 int32_t actLen = sizeof(tmp)-1;
787 if(len > actLen) {
788 len = actLen;
789 }
790 str.extract(0,len,tmp);
791 tmp[len]=0;
792
793 fprintf(stderr, "err - recursed, 2nd lookup was unistring %s\n", tmp);
794 #endif
795 success = U_MISSING_RESOURCE_ERROR; // requested a calendar type which could NOT be found.
796 delete c;
797 delete zone;
798 return NULL;
799 }
800 #ifdef U_DEBUG_CALSVC
801 fprintf(stderr, "%p: setting week count data to locale %s, actual locale %s\n", c, (const char*)aLocale.getName(), (const char *)actualLoc.getName());
802 #endif
803 c->setWeekCountData(aLocale, c->getType(), success); // set the correct locale (this was an indirected calendar)
804 }
805 else
806 #endif /* UCONFIG_NO_SERVICE */
807 {
808 // a calendar was returned - we assume the factory did the right thing.
809 c = (Calendar*)u;
810 }
811
812 // Now, reset calendar to default state:
813 c->adoptTimeZone(zone); // Set the correct time zone
814 c->setTimeInMillis(getNow(), success); // let the new calendar have the current time.
815
816 return c;
817 }
818
819 // -------------------------------------
820
821 Calendar* U_EXPORT2
822 Calendar::createInstance(const TimeZone& zone, const Locale& aLocale, UErrorCode& success)
823 {
824 Calendar* c = createInstance(aLocale, success);
825 if(U_SUCCESS(success) && c) {
826 c->setTimeZone(zone);
827 }
828 return c;
829 }
830
831 // -------------------------------------
832
833 UBool
834 Calendar::operator==(const Calendar& that) const
835 {
836 UErrorCode status = U_ZERO_ERROR;
837 return isEquivalentTo(that) &&
838 getTimeInMillis(status) == that.getTimeInMillis(status) &&
839 U_SUCCESS(status);
840 }
841
842 UBool
843 Calendar::isEquivalentTo(const Calendar& other) const
844 {
845 return getDynamicClassID() == other.getDynamicClassID() &&
846 fLenient == other.fLenient &&
847 fFirstDayOfWeek == other.fFirstDayOfWeek &&
848 fMinimalDaysInFirstWeek == other.fMinimalDaysInFirstWeek &&
849 *fZone == *other.fZone;
850 }
851
852 // -------------------------------------
853
854 UBool
855 Calendar::equals(const Calendar& when, UErrorCode& status) const
856 {
857 return (this == &when ||
858 getTime(status) == when.getTime(status));
859 }
860
861 // -------------------------------------
862
863 UBool
864 Calendar::before(const Calendar& when, UErrorCode& status) const
865 {
866 return (this != &when &&
867 getTimeInMillis(status) < when.getTimeInMillis(status));
868 }
869
870 // -------------------------------------
871
872 UBool
873 Calendar::after(const Calendar& when, UErrorCode& status) const
874 {
875 return (this != &when &&
876 getTimeInMillis(status) > when.getTimeInMillis(status));
877 }
878
879 // -------------------------------------
880
881
882 const Locale* U_EXPORT2
883 Calendar::getAvailableLocales(int32_t& count)
884 {
885 return Locale::getAvailableLocales(count);
886 }
887
888 // -------------------------------------
889
890 UDate U_EXPORT2
891 Calendar::getNow()
892 {
893 return uprv_getUTCtime(); // return as milliseconds
894 }
895
896 // -------------------------------------
897
898 /**
899 * Gets this Calendar's current time as a long.
900 * @return the current time as UTC milliseconds from the epoch.
901 */
902 double
903 Calendar::getTimeInMillis(UErrorCode& status) const
904 {
905 if(U_FAILURE(status))
906 return 0.0;
907
908 if ( ! fIsTimeSet)
909 ((Calendar*)this)->updateTime(status);
910
911 /* Test for buffer overflows */
912 if(U_FAILURE(status)) {
913 return 0.0;
914 }
915 return fTime;
916 }
917
918 // -------------------------------------
919
920 /**
921 * Sets this Calendar's current time from the given long value.
922 * @param date the new time in UTC milliseconds from the epoch.
923 */
924 void
925 Calendar::setTimeInMillis( double millis, UErrorCode& status ) {
926 if(U_FAILURE(status))
927 return;
928
929 if (millis > MAX_MILLIS) {
930 millis = MAX_MILLIS;
931 } else if (millis < MIN_MILLIS) {
932 millis = MIN_MILLIS;
933 }
934
935 fTime = millis;
936 fAreFieldsSet = fAreAllFieldsSet = FALSE;
937 fIsTimeSet = fAreFieldsVirtuallySet = TRUE;
938 }
939
940 // -------------------------------------
941
942 int32_t
943 Calendar::get(UCalendarDateFields field, UErrorCode& status) const
944 {
945 // field values are only computed when actually requested; for more on when computation
946 // of various things happens, see the "data flow in Calendar" description at the top
947 // of this file
948 if (U_SUCCESS(status)) ((Calendar*)this)->complete(status); // Cast away const
949 return U_SUCCESS(status) ? fFields[field] : 0;
950 }
951
952 // -------------------------------------
953
954 void
955 Calendar::set(UCalendarDateFields field, int32_t value)
956 {
957 if (fAreFieldsVirtuallySet) {
958 UErrorCode ec = U_ZERO_ERROR;
959 computeFields(ec);
960 }
961 fFields[field] = value;
962 fStamp[field] = fNextStamp++;
963 fIsSet[field] = TRUE; // Remove later
964 fIsTimeSet = fAreFieldsSet = fAreFieldsVirtuallySet = FALSE;
965 }
966
967 // -------------------------------------
968
969 void
970 Calendar::set(int32_t year, int32_t month, int32_t date)
971 {
972 set(UCAL_YEAR, year);
973 set(UCAL_MONTH, month);
974 set(UCAL_DATE, date);
975 }
976
977 // -------------------------------------
978
979 void
980 Calendar::set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute)
981 {
982 set(UCAL_YEAR, year);
983 set(UCAL_MONTH, month);
984 set(UCAL_DATE, date);
985 set(UCAL_HOUR_OF_DAY, hour);
986 set(UCAL_MINUTE, minute);
987 }
988
989 // -------------------------------------
990
991 void
992 Calendar::set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute, int32_t second)
993 {
994 set(UCAL_YEAR, year);
995 set(UCAL_MONTH, month);
996 set(UCAL_DATE, date);
997 set(UCAL_HOUR_OF_DAY, hour);
998 set(UCAL_MINUTE, minute);
999 set(UCAL_SECOND, second);
1000 }
1001
1002 // -------------------------------------
1003
1004 void
1005 Calendar::clear()
1006 {
1007 for (int32_t i=0; i<UCAL_FIELD_COUNT; ++i) {
1008 fFields[i] = 0; // Must do this; other code depends on it
1009 fStamp[i] = kUnset;
1010 fIsSet[i] = FALSE; // Remove later
1011 }
1012 fIsTimeSet = fAreFieldsSet = fAreAllFieldsSet = fAreFieldsVirtuallySet = FALSE;
1013 // fTime is not 'cleared' - may be used if no fields are set.
1014 }
1015
1016 // -------------------------------------
1017
1018 void
1019 Calendar::clear(UCalendarDateFields field)
1020 {
1021 if (fAreFieldsVirtuallySet) {
1022 UErrorCode ec = U_ZERO_ERROR;
1023 computeFields(ec);
1024 }
1025 fFields[field] = 0;
1026 fStamp[field] = kUnset;
1027 fIsSet[field] = FALSE; // Remove later
1028 fIsTimeSet = fAreFieldsSet = fAreAllFieldsSet = fAreFieldsVirtuallySet = FALSE;
1029 }
1030
1031 // -------------------------------------
1032
1033 UBool
1034 Calendar::isSet(UCalendarDateFields field) const
1035 {
1036 return fAreFieldsVirtuallySet || (fStamp[field] != kUnset);
1037 }
1038
1039
1040 int32_t Calendar::newestStamp(UCalendarDateFields first, UCalendarDateFields last, int32_t bestStampSoFar) const
1041 {
1042 int32_t bestStamp = bestStampSoFar;
1043 for (int32_t i=(int32_t)first; i<=(int32_t)last; ++i) {
1044 if (fStamp[i] > bestStamp) {
1045 bestStamp = fStamp[i];
1046 }
1047 }
1048 return bestStamp;
1049 }
1050
1051
1052 // -------------------------------------
1053
1054 void
1055 Calendar::complete(UErrorCode& status)
1056 {
1057 if (!fIsTimeSet) {
1058 updateTime(status);
1059 /* Test for buffer overflows */
1060 if(U_FAILURE(status)) {
1061 return;
1062 }
1063 }
1064 if (!fAreFieldsSet) {
1065 computeFields(status); // fills in unset fields
1066 /* Test for buffer overflows */
1067 if(U_FAILURE(status)) {
1068 return;
1069 }
1070 fAreFieldsSet = TRUE;
1071 fAreAllFieldsSet = TRUE;
1072 }
1073 }
1074
1075 //-------------------------------------------------------------------------
1076 // Protected utility methods for use by subclasses. These are very handy
1077 // for implementing add, roll, and computeFields.
1078 //-------------------------------------------------------------------------
1079
1080 /**
1081 * Adjust the specified field so that it is within
1082 * the allowable range for the date to which this calendar is set.
1083 * For example, in a Gregorian calendar pinning the {@link #DAY_OF_MONTH DAY_OF_MONTH}
1084 * field for a calendar set to April 31 would cause it to be set
1085 * to April 30.
1086 * <p>
1087 * <b>Subclassing:</b>
1088 * <br>
1089 * This utility method is intended for use by subclasses that need to implement
1090 * their own overrides of {@link #roll roll} and {@link #add add}.
1091 * <p>
1092 * <b>Note:</b>
1093 * <code>pinField</code> is implemented in terms of
1094 * {@link #getActualMinimum getActualMinimum}
1095 * and {@link #getActualMaximum getActualMaximum}. If either of those methods uses
1096 * a slow, iterative algorithm for a particular field, it would be
1097 * unwise to attempt to call <code>pinField</code> for that field. If you
1098 * really do need to do so, you should override this method to do
1099 * something more efficient for that field.
1100 * <p>
1101 * @param field The calendar field whose value should be pinned.
1102 *
1103 * @see #getActualMinimum
1104 * @see #getActualMaximum
1105 * @stable ICU 2.0
1106 */
1107 void Calendar::pinField(UCalendarDateFields field, UErrorCode& status) {
1108 int32_t max = getActualMaximum(field, status);
1109 int32_t min = getActualMinimum(field, status);
1110
1111 if (fFields[field] > max) {
1112 set(field, max);
1113 } else if (fFields[field] < min) {
1114 set(field, min);
1115 }
1116 }
1117
1118
1119 void Calendar::computeFields(UErrorCode &ec)
1120 {
1121 if (U_FAILURE(ec)) {
1122 return;
1123 }
1124 // Compute local wall millis
1125 double localMillis = internalGetTime();
1126 int32_t rawOffset, dstOffset;
1127 getTimeZone().getOffset(localMillis, FALSE, rawOffset, dstOffset, ec);
1128 localMillis += (rawOffset + dstOffset);
1129
1130 // Mark fields as set. Do this before calling handleComputeFields().
1131 uint32_t mask = //fInternalSetMask;
1132 (1 << UCAL_ERA) |
1133 (1 << UCAL_YEAR) |
1134 (1 << UCAL_MONTH) |
1135 (1 << UCAL_DAY_OF_MONTH) | // = UCAL_DATE
1136 (1 << UCAL_DAY_OF_YEAR) |
1137 (1 << UCAL_EXTENDED_YEAR);
1138
1139 for (int32_t i=0; i<UCAL_FIELD_COUNT; ++i) {
1140 if ((mask & 1) == 0) {
1141 fStamp[i] = kInternallySet;
1142 fIsSet[i] = TRUE; // Remove later
1143 } else {
1144 fStamp[i] = kUnset;
1145 fIsSet[i] = FALSE; // Remove later
1146 }
1147 mask >>= 1;
1148 }
1149
1150 // We used to check for and correct extreme millis values (near
1151 // Long.MIN_VALUE or Long.MAX_VALUE) here. Such values would cause
1152 // overflows from positive to negative (or vice versa) and had to
1153 // be manually tweaked. We no longer need to do this because we
1154 // have limited the range of supported dates to those that have a
1155 // Julian day that fits into an int. This allows us to implement a
1156 // JULIAN_DAY field and also removes some inelegant code. - Liu
1157 // 11/6/00
1158
1159 int32_t days = (int32_t)Math::floorDivide(localMillis, (double)kOneDay);
1160
1161 internalSet(UCAL_JULIAN_DAY,days + kEpochStartAsJulianDay);
1162
1163 #if defined (U_DEBUG_CAL)
1164 //fprintf(stderr, "%s:%d- Hmm! Jules @ %d, as per %.0lf millis\n",
1165 //__FILE__, __LINE__, fFields[UCAL_JULIAN_DAY], localMillis);
1166 #endif
1167
1168 computeGregorianAndDOWFields(fFields[UCAL_JULIAN_DAY], ec);
1169
1170 // Call framework method to have subclass compute its fields.
1171 // These must include, at a minimum, MONTH, DAY_OF_MONTH,
1172 // EXTENDED_YEAR, YEAR, DAY_OF_YEAR. This method will call internalSet(),
1173 // which will update stamp[].
1174 handleComputeFields(fFields[UCAL_JULIAN_DAY], ec);
1175
1176 // Compute week-related fields, based on the subclass-computed
1177 // fields computed by handleComputeFields().
1178 computeWeekFields(ec);
1179
1180 // Compute time-related fields. These are indepent of the date and
1181 // of the subclass algorithm. They depend only on the local zone
1182 // wall milliseconds in day.
1183 int32_t millisInDay = (int32_t) (localMillis - (days * kOneDay));
1184 fFields[UCAL_MILLISECONDS_IN_DAY] = millisInDay;
1185 fFields[UCAL_MILLISECOND] = millisInDay % 1000;
1186 millisInDay /= 1000;
1187 fFields[UCAL_SECOND] = millisInDay % 60;
1188 millisInDay /= 60;
1189 fFields[UCAL_MINUTE] = millisInDay % 60;
1190 millisInDay /= 60;
1191 fFields[UCAL_HOUR_OF_DAY] = millisInDay;
1192 fFields[UCAL_AM_PM] = millisInDay / 12; // Assume AM == 0
1193 fFields[UCAL_HOUR] = millisInDay % 12;
1194 fFields[UCAL_ZONE_OFFSET] = rawOffset;
1195 fFields[UCAL_DST_OFFSET] = dstOffset;
1196 }
1197
1198 uint8_t Calendar::julianDayToDayOfWeek(double julian)
1199 {
1200 // If julian is negative, then julian%7 will be negative, so we adjust
1201 // accordingly. We add 1 because Julian day 0 is Monday.
1202 int8_t dayOfWeek = (int8_t) uprv_fmod(julian + 1, 7);
1203
1204 uint8_t result = (uint8_t)(dayOfWeek + ((dayOfWeek < 0) ? (7+UCAL_SUNDAY ) : UCAL_SUNDAY));
1205 return result;
1206 }
1207
1208 /**
1209 * Compute the Gregorian calendar year, month, and day of month from
1210 * the given Julian day. These values are not stored in fields, but in
1211 * member variables gregorianXxx. Also compute the DAY_OF_WEEK and
1212 * DOW_LOCAL fields.
1213 */
1214 void Calendar::computeGregorianAndDOWFields(int32_t julianDay, UErrorCode &ec)
1215 {
1216 computeGregorianFields(julianDay, ec);
1217
1218 // Compute day of week: JD 0 = Monday
1219 int32_t dow = julianDayToDayOfWeek(julianDay);
1220 internalSet(UCAL_DAY_OF_WEEK,dow);
1221
1222 // Calculate 1-based localized day of week
1223 int32_t dowLocal = dow - getFirstDayOfWeek() + 1;
1224 if (dowLocal < 1) {
1225 dowLocal += 7;
1226 }
1227 internalSet(UCAL_DOW_LOCAL,dowLocal);
1228 fFields[UCAL_DOW_LOCAL] = dowLocal;
1229 }
1230
1231 /**
1232 * Compute the Gregorian calendar year, month, and day of month from the
1233 * Julian day. These values are not stored in fields, but in member
1234 * variables gregorianXxx. They are used for time zone computations and by
1235 * subclasses that are Gregorian derivatives. Subclasses may call this
1236 * method to perform a Gregorian calendar millis->fields computation.
1237 * To perform a Gregorian calendar fields->millis computation, call
1238 * computeGregorianMonthStart().
1239 * @see #computeGregorianMonthStart
1240 */
1241 void Calendar::computeGregorianFields(int32_t julianDay, UErrorCode & /* ec */) {
1242 int32_t gregorianDayOfWeekUnused;
1243 Grego::dayToFields(julianDay - kEpochStartAsJulianDay, fGregorianYear, fGregorianMonth, fGregorianDayOfMonth, gregorianDayOfWeekUnused, fGregorianDayOfYear);
1244 }
1245
1246 /**
1247 * Compute the fields WEEK_OF_YEAR, YEAR_WOY, WEEK_OF_MONTH,
1248 * DAY_OF_WEEK_IN_MONTH, and DOW_LOCAL from EXTENDED_YEAR, YEAR,
1249 * DAY_OF_WEEK, and DAY_OF_YEAR. The latter fields are computed by the
1250 * subclass based on the calendar system.
1251 *
1252 * <p>The YEAR_WOY field is computed simplistically. It is equal to YEAR
1253 * most of the time, but at the year boundary it may be adjusted to YEAR-1
1254 * or YEAR+1 to reflect the overlap of a week into an adjacent year. In
1255 * this case, a simple increment or decrement is performed on YEAR, even
1256 * though this may yield an invalid YEAR value. For instance, if the YEAR
1257 * is part of a calendar system with an N-year cycle field CYCLE, then
1258 * incrementing the YEAR may involve incrementing CYCLE and setting YEAR
1259 * back to 0 or 1. This is not handled by this code, and in fact cannot be
1260 * simply handled without having subclasses define an entire parallel set of
1261 * fields for fields larger than or equal to a year. This additional
1262 * complexity is not warranted, since the intention of the YEAR_WOY field is
1263 * to support ISO 8601 notation, so it will typically be used with a
1264 * proleptic Gregorian calendar, which has no field larger than a year.
1265 */
1266 void Calendar::computeWeekFields(UErrorCode &ec) {
1267 if(U_FAILURE(ec)) {
1268 return;
1269 }
1270 int32_t eyear = fFields[UCAL_EXTENDED_YEAR];
1271 int32_t dayOfWeek = fFields[UCAL_DAY_OF_WEEK];
1272 int32_t dayOfYear = fFields[UCAL_DAY_OF_YEAR];
1273
1274 // WEEK_OF_YEAR start
1275 // Compute the week of the year. For the Gregorian calendar, valid week
1276 // numbers run from 1 to 52 or 53, depending on the year, the first day
1277 // of the week, and the minimal days in the first week. For other
1278 // calendars, the valid range may be different -- it depends on the year
1279 // length. Days at the start of the year may fall into the last week of
1280 // the previous year; days at the end of the year may fall into the
1281 // first week of the next year. ASSUME that the year length is less than
1282 // 7000 days.
1283 int32_t yearOfWeekOfYear = eyear;
1284 int32_t relDow = (dayOfWeek + 7 - getFirstDayOfWeek()) % 7; // 0..6
1285 int32_t relDowJan1 = (dayOfWeek - dayOfYear + 7001 - getFirstDayOfWeek()) % 7; // 0..6
1286 int32_t woy = (dayOfYear - 1 + relDowJan1) / 7; // 0..53
1287 if ((7 - relDowJan1) >= getMinimalDaysInFirstWeek()) {
1288 ++woy;
1289 }
1290
1291 // Adjust for weeks at the year end that overlap into the previous or
1292 // next calendar year.
1293 if (woy == 0) {
1294 // We are the last week of the previous year.
1295 // Check to see if we are in the last week; if so, we need
1296 // to handle the case in which we are the first week of the
1297 // next year.
1298
1299 int32_t prevDoy = dayOfYear + handleGetYearLength(eyear - 1);
1300 woy = weekNumber(prevDoy, dayOfWeek);
1301 yearOfWeekOfYear--;
1302 } else {
1303 int32_t lastDoy = handleGetYearLength(eyear);
1304 // Fast check: For it to be week 1 of the next year, the DOY
1305 // must be on or after L-5, where L is yearLength(), then it
1306 // cannot possibly be week 1 of the next year:
1307 // L-5 L
1308 // doy: 359 360 361 362 363 364 365 001
1309 // dow: 1 2 3 4 5 6 7
1310 if (dayOfYear >= (lastDoy - 5)) {
1311 int32_t lastRelDow = (relDow + lastDoy - dayOfYear) % 7;
1312 if (lastRelDow < 0) {
1313 lastRelDow += 7;
1314 }
1315 if (((6 - lastRelDow) >= getMinimalDaysInFirstWeek()) &&
1316 ((dayOfYear + 7 - relDow) > lastDoy)) {
1317 woy = 1;
1318 yearOfWeekOfYear++;
1319 }
1320 }
1321 }
1322 fFields[UCAL_WEEK_OF_YEAR] = woy;
1323 fFields[UCAL_YEAR_WOY] = yearOfWeekOfYear;
1324 // WEEK_OF_YEAR end
1325
1326 int32_t dayOfMonth = fFields[UCAL_DAY_OF_MONTH];
1327 fFields[UCAL_WEEK_OF_MONTH] = weekNumber(dayOfMonth, dayOfWeek);
1328 fFields[UCAL_DAY_OF_WEEK_IN_MONTH] = (dayOfMonth-1) / 7 + 1;
1329 #if defined (U_DEBUG_CAL)
1330 if(fFields[UCAL_DAY_OF_WEEK_IN_MONTH]==0) fprintf(stderr, "%s:%d: DOWIM %d on %g\n",
1331 __FILE__, __LINE__,fFields[UCAL_DAY_OF_WEEK_IN_MONTH], fTime);
1332 #endif
1333 }
1334
1335
1336 int32_t Calendar::weekNumber(int32_t desiredDay, int32_t dayOfPeriod, int32_t dayOfWeek)
1337 {
1338 // Determine the day of the week of the first day of the period
1339 // in question (either a year or a month). Zero represents the
1340 // first day of the week on this calendar.
1341 int32_t periodStartDayOfWeek = (dayOfWeek - getFirstDayOfWeek() - dayOfPeriod + 1) % 7;
1342 if (periodStartDayOfWeek < 0) periodStartDayOfWeek += 7;
1343
1344 // Compute the week number. Initially, ignore the first week, which
1345 // may be fractional (or may not be). We add periodStartDayOfWeek in
1346 // order to fill out the first week, if it is fractional.
1347 int32_t weekNo = (desiredDay + periodStartDayOfWeek - 1)/7;
1348
1349 // If the first week is long enough, then count it. If
1350 // the minimal days in the first week is one, or if the period start
1351 // is zero, we always increment weekNo.
1352 if ((7 - periodStartDayOfWeek) >= getMinimalDaysInFirstWeek()) ++weekNo;
1353
1354 return weekNo;
1355 }
1356
1357 void Calendar::handleComputeFields(int32_t /* julianDay */, UErrorCode &/* status */)
1358 {
1359 internalSet(UCAL_MONTH, getGregorianMonth());
1360 internalSet(UCAL_DAY_OF_MONTH, getGregorianDayOfMonth());
1361 internalSet(UCAL_DAY_OF_YEAR, getGregorianDayOfYear());
1362 int32_t eyear = getGregorianYear();
1363 internalSet(UCAL_EXTENDED_YEAR, eyear);
1364 int32_t era = GregorianCalendar::AD;
1365 if (eyear < 1) {
1366 era = GregorianCalendar::BC;
1367 eyear = 1 - eyear;
1368 }
1369 internalSet(UCAL_ERA, era);
1370 internalSet(UCAL_YEAR, eyear);
1371 }
1372 // -------------------------------------
1373
1374
1375 void Calendar::roll(EDateFields field, int32_t amount, UErrorCode& status)
1376 {
1377 roll((UCalendarDateFields)field, amount, status);
1378 }
1379
1380 void Calendar::roll(UCalendarDateFields field, int32_t amount, UErrorCode& status)
1381 {
1382 if (amount == 0) {
1383 return; // Nothing to do
1384 }
1385
1386 complete(status);
1387
1388 if(U_FAILURE(status)) {
1389 return;
1390 }
1391 switch (field) {
1392 case UCAL_DAY_OF_MONTH:
1393 case UCAL_AM_PM:
1394 case UCAL_MINUTE:
1395 case UCAL_SECOND:
1396 case UCAL_MILLISECOND:
1397 case UCAL_MILLISECONDS_IN_DAY:
1398 case UCAL_ERA:
1399 // These are the standard roll instructions. These work for all
1400 // simple cases, that is, cases in which the limits are fixed, such
1401 // as the hour, the day of the month, and the era.
1402 {
1403 int32_t min = getActualMinimum(field,status);
1404 int32_t max = getActualMaximum(field,status);
1405 int32_t gap = max - min + 1;
1406
1407 int32_t value = internalGet(field) + amount;
1408 value = (value - min) % gap;
1409 if (value < 0) {
1410 value += gap;
1411 }
1412 value += min;
1413
1414 set(field, value);
1415 return;
1416 }
1417
1418 case UCAL_HOUR:
1419 case UCAL_HOUR_OF_DAY:
1420 // Rolling the hour is difficult on the ONSET and CEASE days of
1421 // daylight savings. For example, if the change occurs at
1422 // 2 AM, we have the following progression:
1423 // ONSET: 12 Std -> 1 Std -> 3 Dst -> 4 Dst
1424 // CEASE: 12 Dst -> 1 Dst -> 1 Std -> 2 Std
1425 // To get around this problem we don't use fields; we manipulate
1426 // the time in millis directly.
1427 {
1428 // Assume min == 0 in calculations below
1429 double start = getTimeInMillis(status);
1430 int32_t oldHour = internalGet(field);
1431 int32_t max = getMaximum(field);
1432 int32_t newHour = (oldHour + amount) % (max + 1);
1433 if (newHour < 0) {
1434 newHour += max + 1;
1435 }
1436 setTimeInMillis(start + kOneHour * (newHour - oldHour),status);
1437 return;
1438 }
1439
1440 case UCAL_MONTH:
1441 // Rolling the month involves both pinning the final value
1442 // and adjusting the DAY_OF_MONTH if necessary. We only adjust the
1443 // DAY_OF_MONTH if, after updating the MONTH field, it is illegal.
1444 // E.g., <jan31>.roll(MONTH, 1) -> <feb28> or <feb29>.
1445 {
1446 int32_t max = getActualMaximum(UCAL_MONTH, status);
1447 int32_t mon = (internalGet(UCAL_MONTH) + amount) % (max+1);
1448
1449 if (mon < 0) {
1450 mon += (max + 1);
1451 }
1452 set(UCAL_MONTH, mon);
1453
1454 // Keep the day of month in range. We don't want to spill over
1455 // into the next month; e.g., we don't want jan31 + 1 mo -> feb31 ->
1456 // mar3.
1457 pinField(UCAL_DAY_OF_MONTH,status);
1458 return;
1459 }
1460
1461 case UCAL_YEAR:
1462 case UCAL_YEAR_WOY:
1463 case UCAL_EXTENDED_YEAR:
1464 // Rolling the year can involve pinning the DAY_OF_MONTH.
1465 set(field, internalGet(field) + amount);
1466 pinField(UCAL_MONTH,status);
1467 pinField(UCAL_DAY_OF_MONTH,status);
1468 return;
1469
1470 case UCAL_WEEK_OF_MONTH:
1471 {
1472 // This is tricky, because during the roll we may have to shift
1473 // to a different day of the week. For example:
1474
1475 // s m t w r f s
1476 // 1 2 3 4 5
1477 // 6 7 8 9 10 11 12
1478
1479 // When rolling from the 6th or 7th back one week, we go to the
1480 // 1st (assuming that the first partial week counts). The same
1481 // thing happens at the end of the month.
1482
1483 // The other tricky thing is that we have to figure out whether
1484 // the first partial week actually counts or not, based on the
1485 // minimal first days in the week. And we have to use the
1486 // correct first day of the week to delineate the week
1487 // boundaries.
1488
1489 // Here's our algorithm. First, we find the real boundaries of
1490 // the month. Then we discard the first partial week if it
1491 // doesn't count in this locale. Then we fill in the ends with
1492 // phantom days, so that the first partial week and the last
1493 // partial week are full weeks. We then have a nice square
1494 // block of weeks. We do the usual rolling within this block,
1495 // as is done elsewhere in this method. If we wind up on one of
1496 // the phantom days that we added, we recognize this and pin to
1497 // the first or the last day of the month. Easy, eh?
1498
1499 // Normalize the DAY_OF_WEEK so that 0 is the first day of the week
1500 // in this locale. We have dow in 0..6.
1501 int32_t dow = internalGet(UCAL_DAY_OF_WEEK) - getFirstDayOfWeek();
1502 if (dow < 0) dow += 7;
1503
1504 // Find the day of the week (normalized for locale) for the first
1505 // of the month.
1506 int32_t fdm = (dow - internalGet(UCAL_DAY_OF_MONTH) + 1) % 7;
1507 if (fdm < 0) fdm += 7;
1508
1509 // Get the first day of the first full week of the month,
1510 // including phantom days, if any. Figure out if the first week
1511 // counts or not; if it counts, then fill in phantom days. If
1512 // not, advance to the first real full week (skip the partial week).
1513 int32_t start;
1514 if ((7 - fdm) < getMinimalDaysInFirstWeek())
1515 start = 8 - fdm; // Skip the first partial week
1516 else
1517 start = 1 - fdm; // This may be zero or negative
1518
1519 // Get the day of the week (normalized for locale) for the last
1520 // day of the month.
1521 int32_t monthLen = getActualMaximum(UCAL_DAY_OF_MONTH, status);
1522 int32_t ldm = (monthLen - internalGet(UCAL_DAY_OF_MONTH) + dow) % 7;
1523 // We know monthLen >= DAY_OF_MONTH so we skip the += 7 step here.
1524
1525 // Get the limit day for the blocked-off rectangular month; that
1526 // is, the day which is one past the last day of the month,
1527 // after the month has already been filled in with phantom days
1528 // to fill out the last week. This day has a normalized DOW of 0.
1529 int32_t limit = monthLen + 7 - ldm;
1530
1531 // Now roll between start and (limit - 1).
1532 int32_t gap = limit - start;
1533 int32_t day_of_month = (internalGet(UCAL_DAY_OF_MONTH) + amount*7 -
1534 start) % gap;
1535 if (day_of_month < 0) day_of_month += gap;
1536 day_of_month += start;
1537
1538 // Finally, pin to the real start and end of the month.
1539 if (day_of_month < 1) day_of_month = 1;
1540 if (day_of_month > monthLen) day_of_month = monthLen;
1541
1542 // Set the DAY_OF_MONTH. We rely on the fact that this field
1543 // takes precedence over everything else (since all other fields
1544 // are also set at this point). If this fact changes (if the
1545 // disambiguation algorithm changes) then we will have to unset
1546 // the appropriate fields here so that DAY_OF_MONTH is attended
1547 // to.
1548 set(UCAL_DAY_OF_MONTH, day_of_month);
1549 return;
1550 }
1551 case UCAL_WEEK_OF_YEAR:
1552 {
1553 // This follows the outline of WEEK_OF_MONTH, except it applies
1554 // to the whole year. Please see the comment for WEEK_OF_MONTH
1555 // for general notes.
1556
1557 // Normalize the DAY_OF_WEEK so that 0 is the first day of the week
1558 // in this locale. We have dow in 0..6.
1559 int32_t dow = internalGet(UCAL_DAY_OF_WEEK) - getFirstDayOfWeek();
1560 if (dow < 0) dow += 7;
1561
1562 // Find the day of the week (normalized for locale) for the first
1563 // of the year.
1564 int32_t fdy = (dow - internalGet(UCAL_DAY_OF_YEAR) + 1) % 7;
1565 if (fdy < 0) fdy += 7;
1566
1567 // Get the first day of the first full week of the year,
1568 // including phantom days, if any. Figure out if the first week
1569 // counts or not; if it counts, then fill in phantom days. If
1570 // not, advance to the first real full week (skip the partial week).
1571 int32_t start;
1572 if ((7 - fdy) < getMinimalDaysInFirstWeek())
1573 start = 8 - fdy; // Skip the first partial week
1574 else
1575 start = 1 - fdy; // This may be zero or negative
1576
1577 // Get the day of the week (normalized for locale) for the last
1578 // day of the year.
1579 int32_t yearLen = getActualMaximum(UCAL_DAY_OF_YEAR,status);
1580 int32_t ldy = (yearLen - internalGet(UCAL_DAY_OF_YEAR) + dow) % 7;
1581 // We know yearLen >= DAY_OF_YEAR so we skip the += 7 step here.
1582
1583 // Get the limit day for the blocked-off rectangular year; that
1584 // is, the day which is one past the last day of the year,
1585 // after the year has already been filled in with phantom days
1586 // to fill out the last week. This day has a normalized DOW of 0.
1587 int32_t limit = yearLen + 7 - ldy;
1588
1589 // Now roll between start and (limit - 1).
1590 int32_t gap = limit - start;
1591 int32_t day_of_year = (internalGet(UCAL_DAY_OF_YEAR) + amount*7 -
1592 start) % gap;
1593 if (day_of_year < 0) day_of_year += gap;
1594 day_of_year += start;
1595
1596 // Finally, pin to the real start and end of the month.
1597 if (day_of_year < 1) day_of_year = 1;
1598 if (day_of_year > yearLen) day_of_year = yearLen;
1599
1600 // Make sure that the year and day of year are attended to by
1601 // clearing other fields which would normally take precedence.
1602 // If the disambiguation algorithm is changed, this section will
1603 // have to be updated as well.
1604 set(UCAL_DAY_OF_YEAR, day_of_year);
1605 clear(UCAL_MONTH);
1606 return;
1607 }
1608 case UCAL_DAY_OF_YEAR:
1609 {
1610 // Roll the day of year using millis. Compute the millis for
1611 // the start of the year, and get the length of the year.
1612 double delta = amount * kOneDay; // Scale up from days to millis
1613 double min2 = internalGet(UCAL_DAY_OF_YEAR)-1;
1614 min2 *= kOneDay;
1615 min2 = internalGetTime() - min2;
1616
1617 // double min2 = internalGetTime() - (internalGet(UCAL_DAY_OF_YEAR) - 1.0) * kOneDay;
1618 double newtime;
1619
1620 double yearLength = getActualMaximum(UCAL_DAY_OF_YEAR,status);
1621 double oneYear = yearLength;
1622 oneYear *= kOneDay;
1623 newtime = uprv_fmod((internalGetTime() + delta - min2), oneYear);
1624 if (newtime < 0) newtime += oneYear;
1625 setTimeInMillis(newtime + min2, status);
1626 return;
1627 }
1628 case UCAL_DAY_OF_WEEK:
1629 case UCAL_DOW_LOCAL:
1630 {
1631 // Roll the day of week using millis. Compute the millis for
1632 // the start of the week, using the first day of week setting.
1633 // Restrict the millis to [start, start+7days).
1634 double delta = amount * kOneDay; // Scale up from days to millis
1635 // Compute the number of days before the current day in this
1636 // week. This will be a value 0..6.
1637 int32_t leadDays = internalGet(field);
1638 leadDays -= (field == UCAL_DAY_OF_WEEK) ? getFirstDayOfWeek() : 1;
1639 if (leadDays < 0) leadDays += 7;
1640 double min2 = internalGetTime() - leadDays * kOneDay;
1641 double newtime = uprv_fmod((internalGetTime() + delta - min2), kOneWeek);
1642 if (newtime < 0) newtime += kOneWeek;
1643 setTimeInMillis(newtime + min2, status);
1644 return;
1645 }
1646 case UCAL_DAY_OF_WEEK_IN_MONTH:
1647 {
1648 // Roll the day of week in the month using millis. Determine
1649 // the first day of the week in the month, and then the last,
1650 // and then roll within that range.
1651 double delta = amount * kOneWeek; // Scale up from weeks to millis
1652 // Find the number of same days of the week before this one
1653 // in this month.
1654 int32_t preWeeks = (internalGet(UCAL_DAY_OF_MONTH) - 1) / 7;
1655 // Find the number of same days of the week after this one
1656 // in this month.
1657 int32_t postWeeks = (getActualMaximum(UCAL_DAY_OF_MONTH,status) -
1658 internalGet(UCAL_DAY_OF_MONTH)) / 7;
1659 // From these compute the min and gap millis for rolling.
1660 double min2 = internalGetTime() - preWeeks * kOneWeek;
1661 double gap2 = kOneWeek * (preWeeks + postWeeks + 1); // Must add 1!
1662 // Roll within this range
1663 double newtime = uprv_fmod((internalGetTime() + delta - min2), gap2);
1664 if (newtime < 0) newtime += gap2;
1665 setTimeInMillis(newtime + min2, status);
1666 return;
1667 }
1668 case UCAL_JULIAN_DAY:
1669 set(field, internalGet(field) + amount);
1670 return;
1671 default:
1672 // Other fields cannot be rolled by this method
1673 #if defined (U_DEBUG_CAL)
1674 fprintf(stderr, "%s:%d: ILLEGAL ARG because of roll on non-rollable field %s\n",
1675 __FILE__, __LINE__,fldName(field));
1676 #endif
1677 status = U_ILLEGAL_ARGUMENT_ERROR;
1678 }
1679 }
1680
1681 void Calendar::add(EDateFields field, int32_t amount, UErrorCode& status)
1682 {
1683 Calendar::add((UCalendarDateFields)field, amount, status);
1684 }
1685
1686 // -------------------------------------
1687 void Calendar::add(UCalendarDateFields field, int32_t amount, UErrorCode& status)
1688 {
1689 if (amount == 0) {
1690 return; // Do nothing!
1691 }
1692
1693 // We handle most fields in the same way. The algorithm is to add
1694 // a computed amount of millis to the current millis. The only
1695 // wrinkle is with DST -- for some fields, like the DAY_OF_MONTH,
1696 // we don't want the HOUR to shift due to changes in DST. If the
1697 // result of the add operation is to move from DST to Standard, or
1698 // vice versa, we need to adjust by an hour forward or back,
1699 // respectively. For such fields we set keepHourInvariant to TRUE.
1700
1701 // We only adjust the DST for fields larger than an hour. For
1702 // fields smaller than an hour, we cannot adjust for DST without
1703 // causing problems. for instance, if you add one hour to April 5,
1704 // 1998, 1:00 AM, in PST, the time becomes "2:00 AM PDT" (an
1705 // illegal value), but then the adjustment sees the change and
1706 // compensates by subtracting an hour. As a result the time
1707 // doesn't advance at all.
1708
1709 // For some fields larger than a day, such as a UCAL_MONTH, we pin the
1710 // UCAL_DAY_OF_MONTH. This allows <March 31>.add(UCAL_MONTH, 1) to be
1711 // <April 30>, rather than <April 31> => <May 1>.
1712
1713 double delta = amount; // delta in ms
1714 UBool keepHourInvariant = TRUE;
1715
1716 switch (field) {
1717 case UCAL_ERA:
1718 set(field, get(field, status) + amount);
1719 pinField(UCAL_ERA, status);
1720 return;
1721
1722 case UCAL_YEAR:
1723 case UCAL_EXTENDED_YEAR:
1724 case UCAL_YEAR_WOY:
1725 case UCAL_MONTH:
1726 set(field, get(field, status) + amount);
1727 pinField(UCAL_DAY_OF_MONTH, status);
1728 return;
1729
1730 case UCAL_WEEK_OF_YEAR:
1731 case UCAL_WEEK_OF_MONTH:
1732 case UCAL_DAY_OF_WEEK_IN_MONTH:
1733 delta *= kOneWeek;
1734 break;
1735
1736 case UCAL_AM_PM:
1737 delta *= 12 * kOneHour;
1738 break;
1739
1740 case UCAL_DAY_OF_MONTH:
1741 case UCAL_DAY_OF_YEAR:
1742 case UCAL_DAY_OF_WEEK:
1743 case UCAL_DOW_LOCAL:
1744 case UCAL_JULIAN_DAY:
1745 delta *= kOneDay;
1746 break;
1747
1748 case UCAL_HOUR_OF_DAY:
1749 case UCAL_HOUR:
1750 delta *= kOneHour;
1751 keepHourInvariant = FALSE;
1752 break;
1753
1754 case UCAL_MINUTE:
1755 delta *= kOneMinute;
1756 keepHourInvariant = FALSE;
1757 break;
1758
1759 case UCAL_SECOND:
1760 delta *= kOneSecond;
1761 keepHourInvariant = FALSE;
1762 break;
1763
1764 case UCAL_MILLISECOND:
1765 case UCAL_MILLISECONDS_IN_DAY:
1766 keepHourInvariant = FALSE;
1767 break;
1768
1769 default:
1770 #if defined (U_DEBUG_CAL)
1771 fprintf(stderr, "%s:%d: ILLEGAL ARG because field %s not addable",
1772 __FILE__, __LINE__, fldName(field));
1773 #endif
1774 status = U_ILLEGAL_ARGUMENT_ERROR;
1775 return;
1776 // throw new IllegalArgumentException("Calendar.add(" + fieldName(field) +
1777 // ") not supported");
1778 }
1779
1780 // In order to keep the hour invariant (for fields where this is
1781 // appropriate), record the DST_OFFSET before and after the add()
1782 // operation. If it has changed, then adjust the millis to
1783 // compensate.
1784 int32_t dst = 0;
1785 int32_t hour = 0;
1786 if (keepHourInvariant) {
1787 dst = get(UCAL_DST_OFFSET, status);
1788 hour = internalGet(UCAL_HOUR_OF_DAY);
1789 }
1790
1791 setTimeInMillis(getTimeInMillis(status) + delta, status);
1792
1793 if (keepHourInvariant) {
1794 dst -= get(UCAL_DST_OFFSET, status);
1795 if (dst != 0) {
1796 // We have done an hour-invariant adjustment but the
1797 // DST offset has altered. We adjust millis to keep
1798 // the hour constant. In cases such as midnight after
1799 // a DST change which occurs at midnight, there is the
1800 // danger of adjusting into a different day. To avoid
1801 // this we make the adjustment only if it actually
1802 // maintains the hour.
1803 double t = internalGetTime();
1804 setTimeInMillis(t + dst, status);
1805 if (get(UCAL_HOUR_OF_DAY, status) != hour) {
1806 setTimeInMillis(t, status);
1807 }
1808 }
1809 }
1810 }
1811
1812 // -------------------------------------
1813 int32_t Calendar::fieldDifference(UDate when, EDateFields field, UErrorCode& status) {
1814 return fieldDifference(when, (UCalendarDateFields) field, status);
1815 }
1816
1817 int32_t Calendar::fieldDifference(UDate targetMs, UCalendarDateFields field, UErrorCode& ec) {
1818 if (U_FAILURE(ec)) return 0;
1819 int32_t min = 0;
1820 double startMs = getTimeInMillis(ec);
1821 // Always add from the start millis. This accomodates
1822 // operations like adding years from February 29, 2000 up to
1823 // February 29, 2004. If 1, 1, 1, 1 is added to the year
1824 // field, the DOM gets pinned to 28 and stays there, giving an
1825 // incorrect DOM difference of 1. We have to add 1, reset, 2,
1826 // reset, 3, reset, 4.
1827 if (startMs < targetMs) {
1828 int32_t max = 1;
1829 // Find a value that is too large
1830 while (U_SUCCESS(ec)) {
1831 setTimeInMillis(startMs, ec);
1832 add(field, max, ec);
1833 double ms = getTimeInMillis(ec);
1834 if (ms == targetMs) {
1835 return max;
1836 } else if (ms > targetMs) {
1837 break;
1838 } else {
1839 max <<= 1;
1840 if (max < 0) {
1841 // Field difference too large to fit into int32_t
1842 #if defined (U_DEBUG_CAL)
1843 fprintf(stderr, "%s:%d: ILLEGAL ARG because field %s's max too large for int32_t\n",
1844 __FILE__, __LINE__, fldName(field));
1845 #endif
1846 ec = U_ILLEGAL_ARGUMENT_ERROR;
1847 }
1848 }
1849 }
1850 // Do a binary search
1851 while ((max - min) > 1 && U_SUCCESS(ec)) {
1852 int32_t t = (min + max) / 2;
1853 setTimeInMillis(startMs, ec);
1854 add(field, t, ec);
1855 double ms = getTimeInMillis(ec);
1856 if (ms == targetMs) {
1857 return t;
1858 } else if (ms > targetMs) {
1859 max = t;
1860 } else {
1861 min = t;
1862 }
1863 }
1864 } else if (startMs > targetMs) {
1865 int32_t max = -1;
1866 // Find a value that is too small
1867 while (U_SUCCESS(ec)) {
1868 setTimeInMillis(startMs, ec);
1869 add(field, max, ec);
1870 double ms = getTimeInMillis(ec);
1871 if (ms == targetMs) {
1872 return max;
1873 } else if (ms < targetMs) {
1874 break;
1875 } else {
1876 max <<= 1;
1877 if (max == 0) {
1878 // Field difference too large to fit into int32_t
1879 #if defined (U_DEBUG_CAL)
1880 fprintf(stderr, "%s:%d: ILLEGAL ARG because field %s's max too large for int32_t\n",
1881 __FILE__, __LINE__, fldName(field));
1882 #endif
1883 ec = U_ILLEGAL_ARGUMENT_ERROR;
1884 }
1885 }
1886 }
1887 // Do a binary search
1888 while ((min - max) > 1 && U_SUCCESS(ec)) {
1889 int32_t t = (min + max) / 2;
1890 setTimeInMillis(startMs, ec);
1891 add(field, t, ec);
1892 double ms = getTimeInMillis(ec);
1893 if (ms == targetMs) {
1894 return t;
1895 } else if (ms < targetMs) {
1896 max = t;
1897 } else {
1898 min = t;
1899 }
1900 }
1901 }
1902 // Set calendar to end point
1903 setTimeInMillis(startMs, ec);
1904 add(field, min, ec);
1905
1906 /* Test for buffer overflows */
1907 if(U_FAILURE(ec)) {
1908 return 0;
1909 }
1910 return min;
1911 }
1912
1913 // -------------------------------------
1914
1915 void
1916 Calendar::adoptTimeZone(TimeZone* zone)
1917 {
1918 // Do nothing if passed-in zone is NULL
1919 if (zone == NULL) return;
1920
1921 // fZone should always be non-null
1922 if (fZone != NULL) delete fZone;
1923 fZone = zone;
1924
1925 // if the zone changes, we need to recompute the time fields
1926 fAreFieldsSet = FALSE;
1927 }
1928
1929 // -------------------------------------
1930 void
1931 Calendar::setTimeZone(const TimeZone& zone)
1932 {
1933 adoptTimeZone(zone.clone());
1934 }
1935
1936 // -------------------------------------
1937
1938 const TimeZone&
1939 Calendar::getTimeZone() const
1940 {
1941 return *fZone;
1942 }
1943
1944 // -------------------------------------
1945
1946 TimeZone*
1947 Calendar::orphanTimeZone()
1948 {
1949 TimeZone *z = fZone;
1950 // we let go of the time zone; the new time zone is the system default time zone
1951 fZone = TimeZone::createDefault();
1952 return z;
1953 }
1954
1955 // -------------------------------------
1956
1957 void
1958 Calendar::setLenient(UBool lenient)
1959 {
1960 fLenient = lenient;
1961 }
1962
1963 // -------------------------------------
1964
1965 UBool
1966 Calendar::isLenient() const
1967 {
1968 return fLenient;
1969 }
1970
1971 // -------------------------------------
1972
1973 void
1974 Calendar::setFirstDayOfWeek(UCalendarDaysOfWeek value)
1975 {
1976 if (fFirstDayOfWeek != value &&
1977 value >= UCAL_SUNDAY && value <= UCAL_SATURDAY) {
1978 fFirstDayOfWeek = value;
1979 fAreFieldsSet = FALSE;
1980 }
1981 }
1982
1983 // -------------------------------------
1984
1985 Calendar::EDaysOfWeek
1986 Calendar::getFirstDayOfWeek() const
1987 {
1988 return (Calendar::EDaysOfWeek)fFirstDayOfWeek;
1989 }
1990
1991 UCalendarDaysOfWeek
1992 Calendar::getFirstDayOfWeek(UErrorCode & /*status*/) const
1993 {
1994 return fFirstDayOfWeek;
1995 }
1996 // -------------------------------------
1997
1998 void
1999 Calendar::setMinimalDaysInFirstWeek(uint8_t value)
2000 {
2001 // Values less than 1 have the same effect as 1; values greater
2002 // than 7 have the same effect as 7. However, we normalize values
2003 // so operator== and so forth work.
2004 if (value < 1) {
2005 value = 1;
2006 } else if (value > 7) {
2007 value = 7;
2008 }
2009 if (fMinimalDaysInFirstWeek != value) {
2010 fMinimalDaysInFirstWeek = value;
2011 fAreFieldsSet = FALSE;
2012 }
2013 }
2014
2015 // -------------------------------------
2016
2017 uint8_t
2018 Calendar::getMinimalDaysInFirstWeek() const
2019 {
2020 return fMinimalDaysInFirstWeek;
2021 }
2022
2023 // ------------------------------------- limits
2024
2025 int32_t
2026 Calendar::getMinimum(EDateFields field) const {
2027 return getLimit((UCalendarDateFields) field,UCAL_LIMIT_MINIMUM);
2028 }
2029
2030 int32_t
2031 Calendar::getMinimum(UCalendarDateFields field) const
2032 {
2033 return getLimit(field,UCAL_LIMIT_MINIMUM);
2034 }
2035
2036 // -------------------------------------
2037 int32_t
2038 Calendar::getMaximum(EDateFields field) const
2039 {
2040 return getLimit((UCalendarDateFields) field,UCAL_LIMIT_MAXIMUM);
2041 }
2042
2043 int32_t
2044 Calendar::getMaximum(UCalendarDateFields field) const
2045 {
2046 return getLimit(field,UCAL_LIMIT_MAXIMUM);
2047 }
2048
2049 // -------------------------------------
2050 int32_t
2051 Calendar::getGreatestMinimum(EDateFields field) const
2052 {
2053 return getLimit((UCalendarDateFields)field,UCAL_LIMIT_GREATEST_MINIMUM);
2054 }
2055
2056 int32_t
2057 Calendar::getGreatestMinimum(UCalendarDateFields field) const
2058 {
2059 return getLimit(field,UCAL_LIMIT_GREATEST_MINIMUM);
2060 }
2061
2062 // -------------------------------------
2063 int32_t
2064 Calendar::getLeastMaximum(EDateFields field) const
2065 {
2066 return getLimit((UCalendarDateFields) field,UCAL_LIMIT_LEAST_MAXIMUM);
2067 }
2068
2069 int32_t
2070 Calendar::getLeastMaximum(UCalendarDateFields field) const
2071 {
2072 return getLimit( field,UCAL_LIMIT_LEAST_MAXIMUM);
2073 }
2074
2075 // -------------------------------------
2076 int32_t
2077 Calendar::getActualMinimum(EDateFields field, UErrorCode& status) const
2078 {
2079 return getActualMinimum((UCalendarDateFields) field, status);
2080 }
2081
2082 int32_t Calendar::getLimit(UCalendarDateFields field, ELimitType limitType) const {
2083 switch (field) {
2084 case UCAL_DAY_OF_WEEK:
2085 case UCAL_AM_PM:
2086 case UCAL_HOUR:
2087 case UCAL_HOUR_OF_DAY:
2088 case UCAL_MINUTE:
2089 case UCAL_SECOND:
2090 case UCAL_MILLISECOND:
2091 case UCAL_ZONE_OFFSET:
2092 case UCAL_DST_OFFSET:
2093 case UCAL_DOW_LOCAL:
2094 case UCAL_JULIAN_DAY:
2095 case UCAL_MILLISECONDS_IN_DAY:
2096 case UCAL_IS_LEAP_MONTH:
2097 return kCalendarLimits[field][limitType];
2098
2099 case UCAL_WEEK_OF_MONTH:
2100 {
2101 int32_t limit;
2102 if (limitType == UCAL_LIMIT_MINIMUM) {
2103 limit = getMinimalDaysInFirstWeek() == 1 ? 1 : 0;
2104 } else if (limitType == UCAL_LIMIT_GREATEST_MINIMUM) {
2105 limit = 1;
2106 } else {
2107 int32_t minDaysInFirst = getMinimalDaysInFirstWeek();
2108 int32_t daysInMonth = handleGetLimit(UCAL_DAY_OF_MONTH, limitType);
2109 if (limitType == UCAL_LIMIT_LEAST_MAXIMUM) {
2110 limit = (daysInMonth + (7 - minDaysInFirst)) / 7;
2111 } else { // limitType == UCAL_LIMIT_MAXIMUM
2112 limit = (daysInMonth + 6 + (7 - minDaysInFirst)) / 7;
2113 }
2114 }
2115 return limit;
2116 }
2117 default:
2118 return handleGetLimit(field, limitType);
2119 }
2120 }
2121
2122
2123 int32_t
2124 Calendar::getActualMinimum(UCalendarDateFields field, UErrorCode& status) const
2125 {
2126 int32_t fieldValue = getGreatestMinimum(field);
2127 int32_t endValue = getMinimum(field);
2128
2129 // if we know that the minimum value is always the same, just return it
2130 if (fieldValue == endValue) {
2131 return fieldValue;
2132 }
2133
2134 // clone the calendar so we don't mess with the real one, and set it to
2135 // accept anything for the field values
2136 Calendar *work = (Calendar*)this->clone();
2137 if (work == NULL) {
2138 status = U_MEMORY_ALLOCATION_ERROR;
2139 return 0;
2140 }
2141 work->setLenient(TRUE);
2142
2143 // now try each value from getLeastMaximum() to getMaximum() one by one until
2144 // we get a value that normalizes to another value. The last value that
2145 // normalizes to itself is the actual minimum for the current date
2146 int32_t result = fieldValue;
2147
2148 do {
2149 work->set(field, fieldValue);
2150 if (work->get(field, status) != fieldValue) {
2151 break;
2152 }
2153 else {
2154 result = fieldValue;
2155 fieldValue--;
2156 }
2157 } while (fieldValue >= endValue);
2158
2159 delete work;
2160
2161 /* Test for buffer overflows */
2162 if(U_FAILURE(status)) {
2163 return 0;
2164 }
2165 return result;
2166 }
2167
2168 // -------------------------------------
2169
2170
2171
2172 /**
2173 * Ensure that each field is within its valid range by calling {@link
2174 * #validateField(int)} on each field that has been set. This method
2175 * should only be called if this calendar is not lenient.
2176 * @see #isLenient
2177 * @see #validateField(int)
2178 */
2179 void Calendar::validateFields(UErrorCode &status) {
2180 for (int32_t field = 0; U_SUCCESS(status) && (field < UCAL_FIELD_COUNT); field++) {
2181 if (isSet((UCalendarDateFields)field)) {
2182 validateField((UCalendarDateFields)field, status);
2183 }
2184 }
2185 }
2186
2187 /**
2188 * Validate a single field of this calendar. Subclasses should
2189 * override this method to validate any calendar-specific fields.
2190 * Generic fields can be handled by
2191 * <code>Calendar.validateField()</code>.
2192 * @see #validateField(int, int, int)
2193 */
2194 void Calendar::validateField(UCalendarDateFields field, UErrorCode &status) {
2195 int32_t y;
2196 switch (field) {
2197 case UCAL_DAY_OF_MONTH:
2198 y = handleGetExtendedYear();
2199 validateField(field, 1, handleGetMonthLength(y, internalGet(UCAL_MONTH)), status);
2200 break;
2201 case UCAL_DAY_OF_YEAR:
2202 y = handleGetExtendedYear();
2203 validateField(field, 1, handleGetYearLength(y), status);
2204 break;
2205 case UCAL_DAY_OF_WEEK_IN_MONTH:
2206 if (internalGet(field) == 0) {
2207 #if defined (U_DEBUG_CAL)
2208 fprintf(stderr, "%s:%d: ILLEGAL ARG because DOW in month cannot be 0\n",
2209 __FILE__, __LINE__);
2210 #endif
2211 status = U_ILLEGAL_ARGUMENT_ERROR; // "DAY_OF_WEEK_IN_MONTH cannot be zero"
2212 return;
2213 }
2214 validateField(field, getMinimum(field), getMaximum(field), status);
2215 break;
2216 default:
2217 validateField(field, getMinimum(field), getMaximum(field), status);
2218 break;
2219 }
2220 }
2221
2222 /**
2223 * Validate a single field of this calendar given its minimum and
2224 * maximum allowed value. If the field is out of range, throw a
2225 * descriptive <code>IllegalArgumentException</code>. Subclasses may
2226 * use this method in their implementation of {@link
2227 * #validateField(int)}.
2228 */
2229 void Calendar::validateField(UCalendarDateFields field, int32_t min, int32_t max, UErrorCode& status)
2230 {
2231 int32_t value = fFields[field];
2232 if (value < min || value > max) {
2233 #if defined (U_DEBUG_CAL)
2234 fprintf(stderr, "%s:%d: ILLEGAL ARG because of field %s out of range %d..%d at %d\n",
2235 __FILE__, __LINE__,fldName(field),min,max,value);
2236 #endif
2237 status = U_ILLEGAL_ARGUMENT_ERROR;
2238 return;
2239 }
2240 }
2241
2242 // -------------------------
2243
2244 const UFieldResolutionTable* Calendar::getFieldResolutionTable() const {
2245 return kDatePrecedence;
2246 }
2247
2248
2249 UCalendarDateFields Calendar::newerField(UCalendarDateFields defaultField, UCalendarDateFields alternateField) const
2250 {
2251 if (fStamp[alternateField] > fStamp[defaultField]) {
2252 return alternateField;
2253 }
2254 return defaultField;
2255 }
2256
2257 UCalendarDateFields Calendar::resolveFields(const UFieldResolutionTable* precedenceTable) {
2258 int32_t bestField = UCAL_FIELD_COUNT;
2259 for (int32_t g=0; precedenceTable[g][0][0] != -1 && (bestField == UCAL_FIELD_COUNT); ++g) {
2260 int32_t bestStamp = kUnset;
2261 for (int32_t l=0; precedenceTable[g][l][0] != -1; ++l) {
2262 int32_t lineStamp = kUnset;
2263 // Skip over first entry if it is negative
2264 for (int32_t i=((precedenceTable[g][l][0]>=kResolveRemap)?1:0); precedenceTable[g][l][i]!=-1; ++i) {
2265 int32_t s = fStamp[precedenceTable[g][l][i]];
2266 // If any field is unset then don't use this line
2267 if (s == kUnset) {
2268 goto linesInGroup;
2269 } else if(s > lineStamp) {
2270 lineStamp = s;
2271 }
2272 }
2273 // Record new maximum stamp & field no.
2274 if (lineStamp > bestStamp) {
2275 bestStamp = lineStamp;
2276 bestField = precedenceTable[g][l][0]; // First field refers to entire line
2277 }
2278 linesInGroup:
2279 ;
2280 }
2281 }
2282 return (UCalendarDateFields)( (bestField>=kResolveRemap)?(bestField&(kResolveRemap-1)):bestField );
2283 }
2284
2285 const UFieldResolutionTable Calendar::kDatePrecedence[] =
2286 {
2287 {
2288 { UCAL_DAY_OF_MONTH, kResolveSTOP },
2289 { UCAL_WEEK_OF_YEAR, UCAL_DAY_OF_WEEK, kResolveSTOP },
2290 { UCAL_WEEK_OF_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP },
2291 { UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP },
2292 { UCAL_WEEK_OF_YEAR, UCAL_DOW_LOCAL, kResolveSTOP },
2293 { UCAL_WEEK_OF_MONTH, UCAL_DOW_LOCAL, kResolveSTOP },
2294 { UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DOW_LOCAL, kResolveSTOP },
2295 { UCAL_DAY_OF_YEAR, kResolveSTOP },
2296 { kResolveRemap | UCAL_DAY_OF_MONTH, UCAL_YEAR, kResolveSTOP }, // if YEAR is set over YEAR_WOY use DAY_OF_MONTH
2297 { kResolveRemap | UCAL_WEEK_OF_YEAR, UCAL_YEAR_WOY, kResolveSTOP }, // if YEAR_WOY is set, calc based on WEEK_OF_YEAR
2298 { kResolveSTOP }
2299 },
2300 {
2301 { UCAL_WEEK_OF_YEAR, kResolveSTOP },
2302 { UCAL_WEEK_OF_MONTH, kResolveSTOP },
2303 { UCAL_DAY_OF_WEEK_IN_MONTH, kResolveSTOP },
2304 { kResolveRemap | UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DAY_OF_WEEK, kResolveSTOP },
2305 { kResolveRemap | UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_DOW_LOCAL, kResolveSTOP },
2306 { kResolveSTOP }
2307 },
2308 {{kResolveSTOP}}
2309 };
2310
2311
2312 const UFieldResolutionTable Calendar::kDOWPrecedence[] =
2313 {
2314 {
2315 { UCAL_DAY_OF_WEEK,kResolveSTOP, kResolveSTOP },
2316 { UCAL_DOW_LOCAL,kResolveSTOP, kResolveSTOP },
2317 {kResolveSTOP}
2318 },
2319 {{kResolveSTOP}}
2320 };
2321
2322 // precedence for calculating a year
2323 const UFieldResolutionTable Calendar::kYearPrecedence[] =
2324 {
2325 {
2326 { UCAL_YEAR, kResolveSTOP },
2327 { UCAL_EXTENDED_YEAR, kResolveSTOP },
2328 { UCAL_YEAR_WOY, UCAL_WEEK_OF_YEAR, kResolveSTOP }, // YEAR_WOY is useless without WEEK_OF_YEAR
2329 { kResolveSTOP }
2330 },
2331 {{kResolveSTOP}}
2332 };
2333
2334
2335 // -------------------------
2336
2337
2338 void Calendar::computeTime(UErrorCode& status) {
2339 if (!isLenient()) {
2340 validateFields(status);
2341 if (U_FAILURE(status)) {
2342 return;
2343 }
2344 }
2345
2346 // Compute the Julian day
2347 int32_t julianDay = computeJulianDay();
2348
2349 double millis = Grego::julianDayToMillis(julianDay);
2350
2351 #if defined (U_DEBUG_CAL)
2352 // int32_t julianInsanityCheck = (int32_t)Math::floorDivide(millis, kOneDay);
2353 // julianInsanityCheck += kEpochStartAsJulianDay;
2354 // if(1 || julianInsanityCheck != julianDay) {
2355 // fprintf(stderr, "%s:%d- D'oh- computed jules %d, to mills (%s)%.lf, recomputed %d\n",
2356 // __FILE__, __LINE__, julianDay, millis<0.0?"NEG":"", millis, julianInsanityCheck);
2357 // }
2358 #endif
2359
2360 int32_t millisInDay;
2361
2362 // We only use MILLISECONDS_IN_DAY if it has been set by the user.
2363 // This makes it possible for the caller to set the calendar to a
2364 // time and call clear(MONTH) to reset the MONTH to January. This
2365 // is legacy behavior. Without this, clear(MONTH) has no effect,
2366 // since the internally set JULIAN_DAY is used.
2367 if (fStamp[UCAL_MILLISECONDS_IN_DAY] >= ((int32_t)kMinimumUserStamp) &&
2368 newestStamp(UCAL_AM_PM, UCAL_MILLISECOND, kUnset) <= fStamp[UCAL_MILLISECONDS_IN_DAY]) {
2369 millisInDay = internalGet(UCAL_MILLISECONDS_IN_DAY);
2370 } else {
2371 millisInDay = computeMillisInDay();
2372 }
2373
2374 // Compute the time zone offset and DST offset. There are two potential
2375 // ambiguities here. We'll assume a 2:00 am (wall time) switchover time
2376 // for discussion purposes here.
2377 // 1. The transition into DST. Here, a designated time of 2:00 am - 2:59 am
2378 // can be in standard or in DST depending. However, 2:00 am is an invalid
2379 // representation (the representation jumps from 1:59:59 am Std to 3:00:00 am DST).
2380 // We assume standard time, that is, 2:30 am is interpreted as 3:30 am DST.
2381 // 2. The transition out of DST. Here, a designated time of 1:00 am - 1:59 am
2382 // can be in standard or DST. Both are valid representations (the rep
2383 // jumps from 1:59:59 DST to 1:00:00 Std).
2384 // Again, we assume standard time, that is, 1:30 am is interpreted as 1:30 am Std.
2385 // We use the TimeZone object, unless the user has explicitly set the ZONE_OFFSET
2386 // or DST_OFFSET fields; then we use those fields.
2387 if (fStamp[UCAL_ZONE_OFFSET] >= ((int32_t)kMinimumUserStamp) ||
2388 fStamp[UCAL_DST_OFFSET] >= ((int32_t)kMinimumUserStamp)) {
2389 millisInDay -= internalGet(UCAL_ZONE_OFFSET) + internalGet(UCAL_DST_OFFSET);
2390 } else {
2391 millisInDay -= computeZoneOffset(millis, millisInDay,status);
2392 }
2393
2394 internalSetTime(millis + millisInDay);
2395 }
2396
2397 /**
2398 * Compute the milliseconds in the day from the fields. This is a
2399 * value from 0 to 23:59:59.999 inclusive, unless fields are out of
2400 * range, in which case it can be an arbitrary value. This value
2401 * reflects local zone wall time.
2402 * @stable ICU 2.0
2403 */
2404 int32_t Calendar::computeMillisInDay() {
2405 // Do the time portion of the conversion.
2406
2407 int32_t millisInDay = 0;
2408
2409 // Find the best set of fields specifying the time of day. There
2410 // are only two possibilities here; the HOUR_OF_DAY or the
2411 // AM_PM and the HOUR.
2412 int32_t hourOfDayStamp = fStamp[UCAL_HOUR_OF_DAY];
2413 int32_t hourStamp = (fStamp[UCAL_HOUR] > fStamp[UCAL_AM_PM])?fStamp[UCAL_HOUR]:fStamp[UCAL_AM_PM];
2414 int32_t bestStamp = (hourStamp > hourOfDayStamp) ? hourStamp : hourOfDayStamp;
2415
2416 // Hours
2417 if (bestStamp != kUnset) {
2418 if (bestStamp == hourOfDayStamp) {
2419 // Don't normalize here; let overflow bump into the next period.
2420 // This is consistent with how we handle other fields.
2421 millisInDay += internalGet(UCAL_HOUR_OF_DAY);
2422 } else {
2423 // Don't normalize here; let overflow bump into the next period.
2424 // This is consistent with how we handle other fields.
2425 millisInDay += internalGet(UCAL_HOUR);
2426 millisInDay += 12 * internalGet(UCAL_AM_PM); // Default works for unset AM_PM
2427 }
2428 }
2429
2430 // We use the fact that unset == 0; we start with millisInDay
2431 // == HOUR_OF_DAY.
2432 millisInDay *= 60;
2433 millisInDay += internalGet(UCAL_MINUTE); // now have minutes
2434 millisInDay *= 60;
2435 millisInDay += internalGet(UCAL_SECOND); // now have seconds
2436 millisInDay *= 1000;
2437 millisInDay += internalGet(UCAL_MILLISECOND); // now have millis
2438
2439 return millisInDay;
2440 }
2441
2442 /**
2443 * This method can assume EXTENDED_YEAR has been set.
2444 * @param millis milliseconds of the date fields
2445 * @param millisInDay milliseconds of the time fields; may be out
2446 * or range.
2447 * @stable ICU 2.0
2448 */
2449 int32_t Calendar::computeZoneOffset(double millis, int32_t millisInDay, UErrorCode &ec) {
2450 int32_t rawOffset, dstOffset;
2451 getTimeZone().getOffset(millis+millisInDay, TRUE, rawOffset, dstOffset, ec);
2452 return rawOffset + dstOffset;
2453 // Note: Because we pass in wall millisInDay, rather than
2454 // standard millisInDay, we interpret "1:00 am" on the day
2455 // of cessation of DST as "1:00 am Std" (assuming the time
2456 // of cessation is 2:00 am).
2457 }
2458
2459 int32_t Calendar::computeJulianDay()
2460 {
2461 // We want to see if any of the date fields is newer than the
2462 // JULIAN_DAY. If not, then we use JULIAN_DAY. If so, then we do
2463 // the normal resolution. We only use JULIAN_DAY if it has been
2464 // set by the user. This makes it possible for the caller to set
2465 // the calendar to a time and call clear(MONTH) to reset the MONTH
2466 // to January. This is legacy behavior. Without this,
2467 // clear(MONTH) has no effect, since the internally set JULIAN_DAY
2468 // is used.
2469 if (fStamp[UCAL_JULIAN_DAY] >= (int32_t)kMinimumUserStamp) {
2470 int32_t bestStamp = newestStamp(UCAL_ERA, UCAL_DAY_OF_WEEK_IN_MONTH, kUnset);
2471 bestStamp = newestStamp(UCAL_YEAR_WOY, UCAL_EXTENDED_YEAR, bestStamp);
2472 if (bestStamp <= fStamp[UCAL_JULIAN_DAY]) {
2473 return internalGet(UCAL_JULIAN_DAY);
2474 }
2475 }
2476
2477 UCalendarDateFields bestField = resolveFields(getFieldResolutionTable());
2478 if (bestField == UCAL_FIELD_COUNT) {
2479 bestField = UCAL_DAY_OF_MONTH;
2480 }
2481
2482 return handleComputeJulianDay(bestField);
2483 }
2484
2485 // -------------------------------------------
2486
2487 int32_t Calendar::handleComputeJulianDay(UCalendarDateFields bestField) {
2488 UBool useMonth = (bestField == UCAL_DAY_OF_MONTH ||
2489 bestField == UCAL_WEEK_OF_MONTH ||
2490 bestField == UCAL_DAY_OF_WEEK_IN_MONTH);
2491 int32_t year;
2492
2493 if (bestField == UCAL_WEEK_OF_YEAR) {
2494 year = internalGet(UCAL_YEAR_WOY, handleGetExtendedYear());
2495 internalSet(UCAL_EXTENDED_YEAR, year);
2496 } else {
2497 year = handleGetExtendedYear();
2498 internalSet(UCAL_EXTENDED_YEAR, year);
2499 }
2500
2501 #if defined (U_DEBUG_CAL)
2502 fprintf(stderr, "%s:%d: bestField= %s - y=%d\n", __FILE__, __LINE__, fldName(bestField), year);
2503 #endif
2504
2505 // Get the Julian day of the day BEFORE the start of this year.
2506 // If useMonth is true, get the day before the start of the month.
2507
2508 // give calendar subclass a chance to have a default 'first' month
2509 int32_t month;
2510
2511 if(isSet(UCAL_MONTH)) {
2512 month = internalGet(UCAL_MONTH);
2513 } else {
2514 month = getDefaultMonthInYear();
2515 }
2516
2517 int32_t julianDay = handleComputeMonthStart(year, useMonth ? month : 0, useMonth);
2518
2519 if (bestField == UCAL_DAY_OF_MONTH) {
2520
2521 // give calendar subclass a chance to have a default 'first' dom
2522 int32_t dayOfMonth;
2523 if(isSet(UCAL_DAY_OF_MONTH)) {
2524 dayOfMonth = internalGet(UCAL_DAY_OF_MONTH,1);
2525 } else {
2526 dayOfMonth = getDefaultDayInMonth(month);
2527 }
2528 return julianDay + dayOfMonth;
2529 }
2530
2531 if (bestField == UCAL_DAY_OF_YEAR) {
2532 return julianDay + internalGet(UCAL_DAY_OF_YEAR);
2533 }
2534
2535 int32_t firstDayOfWeek = getFirstDayOfWeek(); // Localized fdw
2536
2537 // At this point julianDay is the 0-based day BEFORE the first day of
2538 // January 1, year 1 of the given calendar. If julianDay == 0, it
2539 // specifies (Jan. 1, 1) - 1, in whatever calendar we are using (Julian
2540 // or Gregorian). (or it is before the month we are in, if useMonth is True)
2541
2542 // At this point we need to process the WEEK_OF_MONTH or
2543 // WEEK_OF_YEAR, which are similar, or the DAY_OF_WEEK_IN_MONTH.
2544 // First, perform initial shared computations. These locate the
2545 // first week of the period.
2546
2547 // Get the 0-based localized DOW of day one of the month or year.
2548 // Valid range 0..6.
2549 int32_t first = julianDayToDayOfWeek(julianDay + 1) - firstDayOfWeek;
2550 if (first < 0) {
2551 first += 7;
2552 }
2553
2554 int32_t dowLocal = getLocalDOW();
2555
2556 // Find the first target DOW (dowLocal) in the month or year.
2557 // Actually, it may be just before the first of the month or year.
2558 // It will be an integer from -5..7.
2559 int32_t date = 1 - first + dowLocal;
2560
2561 if (bestField == UCAL_DAY_OF_WEEK_IN_MONTH) {
2562 // Adjust the target DOW to be in the month or year.
2563 if (date < 1) {
2564 date += 7;
2565 }
2566
2567 // The only trickiness occurs if the day-of-week-in-month is
2568 // negative.
2569 int32_t dim = internalGet(UCAL_DAY_OF_WEEK_IN_MONTH, 1);
2570 if (dim >= 0) {
2571 date += 7*(dim - 1);
2572
2573 } else {
2574 // Move date to the last of this day-of-week in this month,
2575 // then back up as needed. If dim==-1, we don't back up at
2576 // all. If dim==-2, we back up once, etc. Don't back up
2577 // past the first of the given day-of-week in this month.
2578 // Note that we handle -2, -3, etc. correctly, even though
2579 // values < -1 are technically disallowed.
2580 int32_t m = internalGet(UCAL_MONTH, UCAL_JANUARY);
2581 int32_t monthLength = handleGetMonthLength(year, m);
2582 date += ((monthLength - date) / 7 + dim + 1) * 7;
2583 }
2584 } else {
2585 #if defined (U_DEBUG_CAL)
2586 fprintf(stderr, "%s:%d - bf= %s\n", __FILE__, __LINE__, fldName(bestField));
2587 #endif
2588
2589 if(bestField == UCAL_WEEK_OF_YEAR) { // ------------------------------------- WOY -------------
2590 if(!isSet(UCAL_YEAR_WOY) || // YWOY not set at all or
2591 ( (resolveFields(kYearPrecedence) != UCAL_YEAR_WOY) // YWOY doesn't have precedence
2592 && (fStamp[UCAL_YEAR_WOY]!=kInternallySet) ) ) // (excluding where all fields are internally set - then YWOY is used)
2593 {
2594 // need to be sure to stay in 'real' year.
2595 int32_t woy = internalGet(bestField);
2596
2597 int32_t nextJulianDay = handleComputeMonthStart(year+1, 0, FALSE); // jd of day before jan 1
2598 int32_t nextFirst = julianDayToDayOfWeek(nextJulianDay + 1) - firstDayOfWeek;
2599
2600 if (nextFirst < 0) { // 0..6 ldow of Jan 1
2601 nextFirst += 7;
2602 }
2603
2604 if(woy==1) { // FIRST WEEK ---------------------------------
2605 #if defined (U_DEBUG_CAL)
2606 fprintf(stderr, "%s:%d - woy=%d, yp=%d, nj(%d)=%d, nf=%d", __FILE__, __LINE__,
2607 internalGet(bestField), resolveFields(kYearPrecedence), year+1,
2608 nextJulianDay, nextFirst);
2609
2610 fprintf(stderr, " next: %d DFW, min=%d \n", (7-nextFirst), getMinimalDaysInFirstWeek() );
2611 #endif
2612
2613 // nextFirst is now the localized DOW of Jan 1 of y-woy+1
2614 if((nextFirst > 0) && // Jan 1 starts on FDOW
2615 (7-nextFirst) >= getMinimalDaysInFirstWeek()) // or enough days in the week
2616 {
2617 // Jan 1 of (yearWoy+1) is in yearWoy+1 - recalculate JD to next year
2618 #if defined (U_DEBUG_CAL)
2619 fprintf(stderr, "%s:%d - was going to move JD from %d to %d [d%d]\n", __FILE__, __LINE__,
2620 julianDay, nextJulianDay, (nextJulianDay-julianDay));
2621 #endif
2622 julianDay = nextJulianDay;
2623
2624 // recalculate 'first' [0-based local dow of jan 1]
2625 first = julianDayToDayOfWeek(julianDay + 1) - firstDayOfWeek;
2626 if (first < 0) {
2627 first += 7;
2628 }
2629 // recalculate date.
2630 date = 1 - first + dowLocal;
2631 }
2632 } else if(woy>=getLeastMaximum(bestField)) {
2633 // could be in the last week- find out if this JD would overstep
2634 int32_t testDate = date;
2635 if ((7 - first) < getMinimalDaysInFirstWeek()) {
2636 testDate += 7;
2637 }
2638
2639 // Now adjust for the week number.
2640 testDate += 7 * (woy - 1);
2641
2642 #if defined (U_DEBUG_CAL)
2643 fprintf(stderr, "%s:%d - y=%d, y-1=%d doy%d, njd%d (C.F. %d)\n",
2644 __FILE__, __LINE__, year, year-1, testDate, julianDay+testDate, nextJulianDay);
2645 #endif
2646 if(julianDay+testDate > nextJulianDay) { // is it past Dec 31? (nextJulianDay is day BEFORE year+1's Jan 1)
2647 // Fire up the calculating engines.. retry YWOY = (year-1)
2648 julianDay = handleComputeMonthStart(year-1, 0, FALSE); // jd before Jan 1 of previous year
2649 first = julianDayToDayOfWeek(julianDay + 1) - firstDayOfWeek; // 0 based local dow of first week
2650
2651 if(first < 0) { // 0..6
2652 first += 7;
2653 }
2654 date = 1 - first + dowLocal;
2655
2656 #if defined (U_DEBUG_CAL)
2657 fprintf(stderr, "%s:%d - date now %d, jd%d, ywoy%d\n",
2658 __FILE__, __LINE__, date, julianDay, year-1);
2659 #endif
2660
2661
2662 } /* correction needed */
2663 } /* leastmaximum */
2664 } /* resolvefields(year) != year_woy */
2665 } /* bestfield != week_of_year */
2666
2667 // assert(bestField == WEEK_OF_MONTH || bestField == WEEK_OF_YEAR)
2668 // Adjust for minimal days in first week
2669 if ((7 - first) < getMinimalDaysInFirstWeek()) {
2670 date += 7;
2671 }
2672
2673 // Now adjust for the week number.
2674 date += 7 * (internalGet(bestField) - 1);
2675 }
2676
2677 return julianDay + date;
2678 }
2679
2680 int32_t
2681 Calendar::getDefaultMonthInYear()
2682 {
2683 return 0;
2684 }
2685
2686 int32_t
2687 Calendar::getDefaultDayInMonth(int32_t /*month*/)
2688 {
2689 return 1;
2690 }
2691
2692
2693 int32_t Calendar::getLocalDOW()
2694 {
2695 // Get zero-based localized DOW, valid range 0..6. This is the DOW
2696 // we are looking for.
2697 int32_t dowLocal = 0;
2698 switch (resolveFields(kDOWPrecedence)) {
2699 case UCAL_DAY_OF_WEEK:
2700 dowLocal = internalGet(UCAL_DAY_OF_WEEK) - fFirstDayOfWeek;
2701 break;
2702 case UCAL_DOW_LOCAL:
2703 dowLocal = internalGet(UCAL_DOW_LOCAL) - 1;
2704 break;
2705 default:
2706 break;
2707 }
2708 dowLocal = dowLocal % 7;
2709 if (dowLocal < 0) {
2710 dowLocal += 7;
2711 }
2712 return dowLocal;
2713 }
2714
2715 int32_t Calendar::handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy)
2716 {
2717 // We have UCAL_YEAR_WOY and UCAL_WEEK_OF_YEAR - from those, determine
2718 // what year we fall in, so that other code can set it properly.
2719 // (code borrowed from computeWeekFields and handleComputeJulianDay)
2720 //return yearWoy;
2721
2722 // First, we need a reliable DOW.
2723 UCalendarDateFields bestField = resolveFields(kDatePrecedence); // !! Note: if subclasses have a different table, they should override handleGetExtendedYearFromWeekFields
2724
2725 // Now, a local DOW
2726 int32_t dowLocal = getLocalDOW(); // 0..6
2727 int32_t firstDayOfWeek = getFirstDayOfWeek(); // Localized fdw
2728 int32_t jan1Start = handleComputeMonthStart(yearWoy, 0, FALSE);
2729 int32_t nextJan1Start = handleComputeMonthStart(yearWoy+1, 0, FALSE); // next year's Jan1 start
2730
2731 // At this point julianDay is the 0-based day BEFORE the first day of
2732 // January 1, year 1 of the given calendar. If julianDay == 0, it
2733 // specifies (Jan. 1, 1) - 1, in whatever calendar we are using (Julian
2734 // or Gregorian). (or it is before the month we are in, if useMonth is True)
2735
2736 // At this point we need to process the WEEK_OF_MONTH or
2737 // WEEK_OF_YEAR, which are similar, or the DAY_OF_WEEK_IN_MONTH.
2738 // First, perform initial shared computations. These locate the
2739 // first week of the period.
2740
2741 // Get the 0-based localized DOW of day one of the month or year.
2742 // Valid range 0..6.
2743 int32_t first = julianDayToDayOfWeek(jan1Start + 1) - firstDayOfWeek;
2744 if (first < 0) {
2745 first += 7;
2746 }
2747 int32_t nextFirst = julianDayToDayOfWeek(nextJan1Start + 1) - firstDayOfWeek;
2748 if (nextFirst < 0) {
2749 nextFirst += 7;
2750 }
2751
2752 int32_t minDays = getMinimalDaysInFirstWeek();
2753 UBool jan1InPrevYear = FALSE; // January 1st in the year of WOY is the 1st week? (i.e. first week is < minimal )
2754 //UBool nextJan1InPrevYear = FALSE; // January 1st of Year of WOY + 1 is in the first week?
2755
2756 if((7 - first) < minDays) {
2757 jan1InPrevYear = TRUE;
2758 }
2759
2760 // if((7 - nextFirst) < minDays) {
2761 // nextJan1InPrevYear = TRUE;
2762 // }
2763
2764 switch(bestField) {
2765 case UCAL_WEEK_OF_YEAR:
2766 if(woy == 1) {
2767 if(jan1InPrevYear == TRUE) {
2768 // the first week of January is in the previous year
2769 // therefore WOY1 is always solidly within yearWoy
2770 return yearWoy;
2771 } else {
2772 // First WOY is split between two years
2773 if( dowLocal < first) { // we are prior to Jan 1
2774 return yearWoy-1; // previous year
2775 } else {
2776 return yearWoy; // in this year
2777 }
2778 }
2779 } else if(woy >= getLeastMaximum(bestField)) {
2780 // we _might_ be in the last week..
2781 int32_t jd = // Calculate JD of our target day:
2782 jan1Start + // JD of Jan 1
2783 (7-first) + // days in the first week (Jan 1.. )
2784 (woy-1)*7 + // add the weeks of the year
2785 dowLocal; // the local dow (0..6) of last week
2786 if(jan1InPrevYear==FALSE) {
2787 jd -= 7; // woy already includes Jan 1's week.
2788 }
2789
2790 if( (jd+1) >= nextJan1Start ) {
2791 // we are in week 52 or 53 etc. - actual year is yearWoy+1
2792 return yearWoy+1;
2793 } else {
2794 // still in yearWoy;
2795 return yearWoy;
2796 }
2797 } else {
2798 // we're not possibly in the last week -must be ywoy
2799 return yearWoy;
2800 }
2801 break;
2802
2803 case UCAL_DATE:
2804 if((internalGet(UCAL_MONTH)==0) &&
2805 (woy >= getLeastMaximum(UCAL_WEEK_OF_YEAR))) {
2806 return yearWoy+1; // month 0, late woy = in the next year
2807 } else if(woy==1) {
2808 //if(nextJan1InPrevYear) {
2809 if(internalGet(UCAL_MONTH)==0) {
2810 return yearWoy;
2811 } else {
2812 return yearWoy-1;
2813 }
2814 //}
2815 }
2816
2817 //(internalGet(UCAL_DATE) <= (7-first)) /* && in minDow */ ) {
2818 //within 1st week and in this month..
2819 //return yearWoy+1;
2820 return yearWoy;
2821 break;
2822
2823 default: // assume the year is appropriate
2824 return yearWoy;
2825 break;
2826 }
2827
2828 #if defined (U_DEBUG_CAL)
2829 fprintf(stderr, "%s:%d - forgot a return on field %s\n", __FILE__, __LINE__, fldName(bestField));
2830 #endif
2831
2832 return yearWoy;
2833 }
2834
2835 int32_t Calendar::handleGetMonthLength(int32_t extendedYear, int32_t month) const
2836 {
2837 return handleComputeMonthStart(extendedYear, month+1, TRUE) -
2838 handleComputeMonthStart(extendedYear, month, TRUE);
2839 }
2840
2841 int32_t Calendar::handleGetYearLength(int32_t eyear) const {
2842 return handleComputeMonthStart(eyear+1, 0, FALSE) -
2843 handleComputeMonthStart(eyear, 0, FALSE);
2844 }
2845
2846 int32_t
2847 Calendar::getActualMaximum(UCalendarDateFields field, UErrorCode& status) const
2848 {
2849 int32_t result;
2850 switch (field) {
2851 case UCAL_DATE:
2852 {
2853 if(U_FAILURE(status)) return 0;
2854 Calendar *cal = clone();
2855 if(!cal) { status = U_MEMORY_ALLOCATION_ERROR; return 0; }
2856 cal->prepareGetActual(field,FALSE,status);
2857 result = handleGetMonthLength(cal->get(UCAL_EXTENDED_YEAR, status), cal->get(UCAL_MONTH, status));
2858 delete cal;
2859 }
2860 break;
2861
2862 case UCAL_DAY_OF_YEAR:
2863 {
2864 if(U_FAILURE(status)) return 0;
2865 Calendar *cal = clone();
2866 if(!cal) { status = U_MEMORY_ALLOCATION_ERROR; return 0; }
2867 cal->prepareGetActual(field,FALSE,status);
2868 result = handleGetYearLength(cal->get(UCAL_EXTENDED_YEAR, status));
2869 delete cal;
2870 }
2871 break;
2872
2873 case UCAL_DAY_OF_WEEK:
2874 case UCAL_AM_PM:
2875 case UCAL_HOUR:
2876 case UCAL_HOUR_OF_DAY:
2877 case UCAL_MINUTE:
2878 case UCAL_SECOND:
2879 case UCAL_MILLISECOND:
2880 case UCAL_ZONE_OFFSET:
2881 case UCAL_DST_OFFSET:
2882 case UCAL_DOW_LOCAL:
2883 case UCAL_JULIAN_DAY:
2884 case UCAL_MILLISECONDS_IN_DAY:
2885 // These fields all have fixed minima/maxima
2886 result = getMaximum(field);
2887 break;
2888
2889 default:
2890 // For all other fields, do it the hard way....
2891 result = getActualHelper(field, getLeastMaximum(field), getMaximum(field),status);
2892 break;
2893 }
2894 return result;
2895 }
2896
2897
2898 /**
2899 * Prepare this calendar for computing the actual minimum or maximum.
2900 * This method modifies this calendar's fields; it is called on a
2901 * temporary calendar.
2902 *
2903 * <p>Rationale: The semantics of getActualXxx() is to return the
2904 * maximum or minimum value that the given field can take, taking into
2905 * account other relevant fields. In general these other fields are
2906 * larger fields. For example, when computing the actual maximum
2907 * DATE, the current value of DATE itself is ignored,
2908 * as is the value of any field smaller.
2909 *
2910 * <p>The time fields all have fixed minima and maxima, so we don't
2911 * need to worry about them. This also lets us set the
2912 * MILLISECONDS_IN_DAY to zero to erase any effects the time fields
2913 * might have when computing date fields.
2914 *
2915 * <p>DAY_OF_WEEK is adjusted specially for the WEEK_OF_MONTH and
2916 * WEEK_OF_YEAR fields to ensure that they are computed correctly.
2917 * @internal
2918 */
2919 void Calendar::prepareGetActual(UCalendarDateFields field, UBool isMinimum, UErrorCode &status)
2920 {
2921 set(UCAL_MILLISECONDS_IN_DAY, 0);
2922
2923 switch (field) {
2924 case UCAL_YEAR:
2925 case UCAL_EXTENDED_YEAR:
2926 set(UCAL_DAY_OF_YEAR, getGreatestMinimum(UCAL_DAY_OF_YEAR));
2927 break;
2928
2929 case UCAL_YEAR_WOY:
2930 set(UCAL_WEEK_OF_YEAR, getGreatestMinimum(UCAL_WEEK_OF_YEAR));
2931
2932 case UCAL_MONTH:
2933 set(UCAL_DATE, getGreatestMinimum(UCAL_DATE));
2934 break;
2935
2936 case UCAL_DAY_OF_WEEK_IN_MONTH:
2937 // For dowim, the maximum occurs for the DOW of the first of the
2938 // month.
2939 set(UCAL_DATE, 1);
2940 set(UCAL_DAY_OF_WEEK, get(UCAL_DAY_OF_WEEK, status)); // Make this user set
2941 break;
2942
2943 case UCAL_WEEK_OF_MONTH:
2944 case UCAL_WEEK_OF_YEAR:
2945 // If we're counting weeks, set the day of the week to either the
2946 // first or last localized DOW. We know the last week of a month
2947 // or year will contain the first day of the week, and that the
2948 // first week will contain the last DOW.
2949 {
2950 int32_t dow = fFirstDayOfWeek;
2951 if (isMinimum) {
2952 dow = (dow + 6) % 7; // set to last DOW
2953 if (dow < UCAL_SUNDAY) {
2954 dow += 7;
2955 }
2956 }
2957 #if defined (U_DEBUG_CAL)
2958 fprintf(stderr, "prepareGetActualHelper(WOM/WOY) - dow=%d\n", dow);
2959 #endif
2960 set(UCAL_DAY_OF_WEEK, dow);
2961 }
2962 break;
2963 default:
2964 break;
2965 }
2966
2967 // Do this last to give it the newest time stamp
2968 set(field, getGreatestMinimum(field));
2969 }
2970
2971 int32_t Calendar::getActualHelper(UCalendarDateFields field, int32_t startValue, int32_t endValue, UErrorCode &status) const
2972 {
2973 #if defined (U_DEBUG_CAL)
2974 fprintf(stderr, "getActualHelper(%d,%d .. %d, %s)\n", field, startValue, endValue, u_errorName(status));
2975 #endif
2976 if (startValue == endValue) {
2977 // if we know that the maximum value is always the same, just return it
2978 return startValue;
2979 }
2980
2981 int32_t delta = (endValue > startValue) ? 1 : -1;
2982
2983 // clone the calendar so we don't mess with the real one, and set it to
2984 // accept anything for the field values
2985 if(U_FAILURE(status)) return startValue;
2986 Calendar *work = clone();
2987 if(!work) { status = U_MEMORY_ALLOCATION_ERROR; return startValue; }
2988 work->setLenient(TRUE);
2989 work->prepareGetActual(field, delta < 0, status);
2990
2991 // now try each value from the start to the end one by one until
2992 // we get a value that normalizes to another value. The last value that
2993 // normalizes to itself is the actual maximum for the current date
2994 work->set(field, startValue);
2995
2996 // prepareGetActual sets the first day of week in the same week with
2997 // the first day of a month. Unlike WEEK_OF_YEAR, week number for the
2998 // week which contains days from both previous and current month is
2999 // not unique. For example, last several days in the previous month
3000 // is week 5, and the rest of week is week 1.
3001 int32_t result = startValue;
3002 if (work->get(field, status) != startValue
3003 && field != UCAL_WEEK_OF_MONTH && delta > 0 || U_FAILURE(status)) {
3004 #if defined (U_DEBUG_CAL)
3005 fprintf(stderr, "getActualHelper(fld %d) - got %d (not %d) - %s\n", field, work->get(field,status), startValue, u_errorName(status));
3006 #endif
3007 } else {
3008 do {
3009 startValue += delta;
3010 work->add(field, delta, status);
3011 if (work->get(field, status) != startValue || U_FAILURE(status)) {
3012 #if defined (U_DEBUG_CAL)
3013 fprintf(stderr, "getActualHelper(fld %d) - got %d (not %d), BREAK - %s\n", field, work->get(field,status), startValue, u_errorName(status));
3014 #endif
3015 break;
3016 }
3017 result = startValue;
3018 } while (startValue != endValue);
3019 }
3020 delete work;
3021 #if defined (U_DEBUG_CAL)
3022 fprintf(stderr, "getActualHelper(%d) = %d\n", field, result);
3023 #endif
3024 return result;
3025 }
3026
3027
3028
3029
3030 // -------------------------------------
3031
3032 void
3033 Calendar::setWeekCountData(const Locale& desiredLocale, const char *type, UErrorCode& status)
3034 {
3035 // Read the week count data from the resource bundle. This should
3036 // have the form:
3037 //
3038 // DateTimeElements:intvector {
3039 // 1, // first day of week
3040 // 1 // min days in week
3041 // }
3042 // Both have a range of 1..7
3043
3044
3045 if (U_FAILURE(status)) return;
3046
3047 fFirstDayOfWeek = UCAL_SUNDAY;
3048 fMinimalDaysInFirstWeek = 1;
3049
3050 CalendarData calData(desiredLocale, type, status);
3051 // If the resource data doesn't seem to be present at all, then use last-resort
3052 // hard-coded data.
3053 UResourceBundle *dateTimeElements = calData.getByKey(gDateTimeElements, status);
3054
3055 if (U_FAILURE(status))
3056 {
3057 #if defined (U_DEBUG_CALDATA)
3058 fprintf(stderr, " Failure loading dateTimeElements = %s\n", u_errorName(status));
3059 #endif
3060 status = U_USING_FALLBACK_WARNING;
3061 return;
3062 }
3063
3064 U_LOCALE_BASED(locBased, *this);
3065 locBased.setLocaleIDs(ures_getLocaleByType(dateTimeElements, ULOC_VALID_LOCALE, &status),
3066 ures_getLocaleByType(dateTimeElements, ULOC_ACTUAL_LOCALE, &status));
3067 if (U_SUCCESS(status)) {
3068 #if defined (U_DEBUG_CAL)
3069 fprintf(stderr, " Valid=%s, Actual=%s\n", validLocale, actualLocale);
3070 #endif
3071 int32_t arrLen;
3072 const int32_t *dateTimeElementsArr = ures_getIntVector(dateTimeElements, &arrLen, &status);
3073
3074 if(U_SUCCESS(status) && arrLen == 2
3075 && 1 <= dateTimeElementsArr[0] && dateTimeElementsArr[0] <= 7
3076 && 1 <= dateTimeElementsArr[1] && dateTimeElementsArr[1] <= 7)
3077 {
3078 fFirstDayOfWeek = (UCalendarDaysOfWeek)dateTimeElementsArr[0];
3079 fMinimalDaysInFirstWeek = (uint8_t)dateTimeElementsArr[1];
3080 }
3081 else {
3082 status = U_INVALID_FORMAT_ERROR;
3083 }
3084 }
3085
3086 // do NOT close dateTimeElements
3087 }
3088
3089 /**
3090 * Recompute the time and update the status fields isTimeSet
3091 * and areFieldsSet. Callers should check isTimeSet and only
3092 * call this method if isTimeSet is false.
3093 */
3094 void
3095 Calendar::updateTime(UErrorCode& status)
3096 {
3097 computeTime(status);
3098 if(U_FAILURE(status))
3099 return;
3100
3101 // If we are lenient, we need to recompute the fields to normalize
3102 // the values. Also, if we haven't set all the fields yet (i.e.,
3103 // in a newly-created object), we need to fill in the fields. [LIU]
3104 if (isLenient() || ! fAreAllFieldsSet)
3105 fAreFieldsSet = FALSE;
3106
3107 fIsTimeSet = TRUE;
3108 fAreFieldsVirtuallySet = FALSE;
3109 }
3110
3111 Locale
3112 Calendar::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
3113 U_LOCALE_BASED(locBased, *this);
3114 return locBased.getLocale(type, status);
3115 }
3116
3117 const char *
3118 Calendar::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
3119 U_LOCALE_BASED(locBased, *this);
3120 return locBased.getLocaleID(type, status);
3121 }
3122
3123 // Deprecated function. This doesn't need to be inline.
3124 void
3125 Calendar::internalSet(EDateFields field, int32_t value)
3126 {
3127 internalSet((UCalendarDateFields) field, value);
3128 }
3129
3130 U_NAMESPACE_END
3131
3132 #endif /* #if !UCONFIG_NO_FORMATTING */
3133
3134
3135 //eof