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