2 ********************************************************************************
3 * Copyright (C) 1997-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 04/22/97 aliu Expanded and corrected comments and other header
14 * 05/01/97 aliu Made equals(), before(), after() arguments const.
15 * 05/20/97 aliu Replaced fAreFieldsSet with fAreFieldsInSync and
17 * 07/27/98 stephen Sync up with JDK 1.2
18 * 11/15/99 weiv added YEAR_WOY and DOW_LOCAL
20 * 8/19/2002 srl Removed Javaisms
21 * 11/07/2003 srl Update, clean up documentation.
22 ********************************************************************************
28 #include "unicode/utypes.h"
32 * \brief C++ API: Calendar object
34 #if !UCONFIG_NO_FORMATTING
36 #include "unicode/uobject.h"
37 #include "unicode/locid.h"
38 #include "unicode/timezone.h"
39 #include "unicode/ucal.h"
40 #include "unicode/umisc.h"
44 class ICUServiceFactory
;
49 typedef int32_t UFieldResolutionTable
[12][8];
52 * <code>Calendar</code> is an abstract base class for converting between
53 * a <code>UDate</code> object and a set of integer fields such as
54 * <code>YEAR</code>, <code>MONTH</code>, <code>DAY</code>, <code>HOUR</code>,
55 * and so on. (A <code>UDate</code> object represents a specific instant in
56 * time with millisecond precision. See UDate
57 * for information about the <code>UDate</code> class.)
60 * Subclasses of <code>Calendar</code> interpret a <code>UDate</code>
61 * according to the rules of a specific calendar system.
62 * The most commonly used subclass of <code>Calendar</code> is
63 * <code>GregorianCalendar</code>. Other subclasses could represent
64 * the various types of lunar calendars in use in many parts of the world.
67 * <b>NOTE</b>: (ICU 2.6) The subclass interface should be considered unstable
71 * Like other locale-sensitive classes, <code>Calendar</code> provides a
72 * static method, <code>createInstance</code>, for getting a generally useful
73 * object of this type. <code>Calendar</code>'s <code>createInstance</code> method
74 * returns the appropriate <code>Calendar</code> subclass whose
75 * time fields have been initialized with the current date and time:
76 * \htmlonly<blockquote>\endhtmlonly
78 * Calendar *rightNow = Calendar::createInstance(errCode);
80 * \htmlonly</blockquote>\endhtmlonly
83 * A <code>Calendar</code> object can produce all the time field values
84 * needed to implement the date-time formatting for a particular language
85 * and calendar style (for example, Japanese-Gregorian, Japanese-Traditional).
88 * When computing a <code>UDate</code> from time fields, two special circumstances
89 * may arise: there may be insufficient information to compute the
90 * <code>UDate</code> (such as only year and month but no day in the month),
91 * or there may be inconsistent information (such as "Tuesday, July 15, 1996"
92 * -- July 15, 1996 is actually a Monday).
95 * <strong>Insufficient information.</strong> The calendar will use default
96 * information to specify the missing fields. This may vary by calendar; for
97 * the Gregorian calendar, the default for a field is the same as that of the
98 * start of the epoch: i.e., YEAR = 1970, MONTH = JANUARY, DATE = 1, etc.
101 * <strong>Inconsistent information.</strong> If fields conflict, the calendar
102 * will give preference to fields set more recently. For example, when
103 * determining the day, the calendar will look for one of the following
104 * combinations of fields. The most recent combination, as determined by the
105 * most recently set single field, will be used.
107 * \htmlonly<blockquote>\endhtmlonly
109 * MONTH + DAY_OF_MONTH
110 * MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
111 * MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
113 * DAY_OF_WEEK + WEEK_OF_YEAR
115 * \htmlonly</blockquote>\endhtmlonly
117 * For the time of day:
119 * \htmlonly<blockquote>\endhtmlonly
124 * \htmlonly</blockquote>\endhtmlonly
127 * <strong>Note:</strong> for some non-Gregorian calendars, different
128 * fields may be necessary for complete disambiguation. For example, a full
129 * specification of the historial Arabic astronomical calendar requires year,
130 * month, day-of-month <em>and</em> day-of-week in some cases.
133 * <strong>Note:</strong> There are certain possible ambiguities in
134 * interpretation of certain singular times, which are resolved in the
137 * <li> 24:00:00 "belongs" to the following day. That is,
138 * 23:59 on Dec 31, 1969 < 24:00 on Jan 1, 1970 < 24:01:00 on Jan 1, 1970
140 * <li> Although historically not precise, midnight also belongs to "am",
141 * and noon belongs to "pm", so on the same day,
142 * 12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm
146 * The date or time format strings are not part of the definition of a
147 * calendar, as those must be modifiable or overridable by the user at
148 * runtime. Use {@link DateFormat}
152 * <code>Calendar</code> provides an API for field "rolling", where fields
153 * can be incremented or decremented, but wrap around. For example, rolling the
154 * month up in the date <code>December 12, <b>1996</b></code> results in
155 * <code>January 12, <b>1996</b></code>.
158 * <code>Calendar</code> also provides a date arithmetic function for
159 * adding the specified (signed) amount of time to a particular time field.
160 * For example, subtracting 5 days from the date <code>September 12, 1996</code>
161 * results in <code>September 7, 1996</code>.
165 class U_I18N_API Calendar
: public UObject
{
169 * Field IDs for date and time. Used to specify date/time fields. ERA is calendar
170 * specific. Example ranges given are for illustration only; see specific Calendar
171 * subclasses for actual ranges.
172 * @deprecated ICU 2.6. Use C enum UCalendarDateFields defined in ucal.h
175 ERA
, // Example: 0..1
176 YEAR
, // Example: 1..big number
177 MONTH
, // Example: 0..11
178 WEEK_OF_YEAR
, // Example: 1..53
179 WEEK_OF_MONTH
, // Example: 1..4
180 DATE
, // Example: 1..31
181 DAY_OF_YEAR
, // Example: 1..365
182 DAY_OF_WEEK
, // Example: 1..7
183 DAY_OF_WEEK_IN_MONTH
, // Example: 1..4, may be specified as -1
184 AM_PM
, // Example: 0..1
185 HOUR
, // Example: 0..11
186 HOUR_OF_DAY
, // Example: 0..23
187 MINUTE
, // Example: 0..59
188 SECOND
, // Example: 0..59
189 MILLISECOND
, // Example: 0..999
190 ZONE_OFFSET
, // Example: -12*U_MILLIS_PER_HOUR..12*U_MILLIS_PER_HOUR
191 DST_OFFSET
, // Example: 0 or U_MILLIS_PER_HOUR
192 YEAR_WOY
, // 'Y' Example: 1..big number - Year of Week of Year
193 DOW_LOCAL
, // 'e' Example: 1..7 - Day of Week / Localized
195 FIELD_COUNT
= UCAL_FIELD_COUNT
// See ucal.h for other fields.
199 * Useful constant for days of week. Note: Calendar day-of-week is 1-based. Clients
200 * who create locale resources for the field of first-day-of-week should be aware of
201 * this. For instance, in US locale, first-day-of-week is set to 1, i.e., SUNDAY.
202 * @deprecated ICU 2.6. Use C enum UCalendarDaysOfWeek defined in ucal.h
215 * Useful constants for month. Note: Calendar month is 0-based.
216 * @deprecated ICU 2.6. Use C enum UCalendarMonths defined in ucal.h
235 * Useful constants for hour in 12-hour clock. Used in GregorianCalendar.
236 * @deprecated ICU 2.6. Use C enum UCalendarAMPMs defined in ucal.h
250 * Create and return a polymorphic copy of this calendar.
252 * @return a polymorphic copy of this calendar.
255 virtual Calendar
* clone(void) const = 0;
258 * Creates a Calendar using the default timezone and locale. Clients are responsible
259 * for deleting the object returned.
261 * @param success Indicates the success/failure of Calendar creation. Filled in
262 * with U_ZERO_ERROR if created successfully, set to a failure result
263 * otherwise. U_MISSING_RESOURCE_ERROR will be returned if the resource data
264 * requests a calendar type which has not been installed.
265 * @return A Calendar if created successfully. NULL otherwise.
268 static Calendar
* U_EXPORT2
createInstance(UErrorCode
& success
);
271 * Creates a Calendar using the given timezone and the default locale.
272 * The Calendar takes ownership of zoneToAdopt; the
273 * client must not delete it.
275 * @param zoneToAdopt The given timezone to be adopted.
276 * @param success Indicates the success/failure of Calendar creation. Filled in
277 * with U_ZERO_ERROR if created successfully, set to a failure result
279 * @return A Calendar if created successfully. NULL otherwise.
282 static Calendar
* U_EXPORT2
createInstance(TimeZone
* zoneToAdopt
, UErrorCode
& success
);
285 * Creates a Calendar using the given timezone and the default locale. The TimeZone
286 * is _not_ adopted; the client is still responsible for deleting it.
288 * @param zone The timezone.
289 * @param success Indicates the success/failure of Calendar creation. Filled in
290 * with U_ZERO_ERROR if created successfully, set to a failure result
292 * @return A Calendar if created successfully. NULL otherwise.
295 static Calendar
* U_EXPORT2
createInstance(const TimeZone
& zone
, UErrorCode
& success
);
298 * Creates a Calendar using the default timezone and the given locale.
300 * @param aLocale The given locale.
301 * @param success Indicates the success/failure of Calendar creation. Filled in
302 * with U_ZERO_ERROR if created successfully, set to a failure result
304 * @return A Calendar if created successfully. NULL otherwise.
307 static Calendar
* U_EXPORT2
createInstance(const Locale
& aLocale
, UErrorCode
& success
);
310 * Creates a Calendar using the given timezone and given locale.
311 * The Calendar takes ownership of zoneToAdopt; the
312 * client must not delete it.
314 * @param zoneToAdopt The given timezone to be adopted.
315 * @param aLocale The given locale.
316 * @param success Indicates the success/failure of Calendar creation. Filled in
317 * with U_ZERO_ERROR if created successfully, set to a failure result
319 * @return A Calendar if created successfully. NULL otherwise.
322 static Calendar
* U_EXPORT2
createInstance(TimeZone
* zoneToAdopt
, const Locale
& aLocale
, UErrorCode
& success
);
325 * Gets a Calendar using the given timezone and given locale. The TimeZone
326 * is _not_ adopted; the client is still responsible for deleting it.
328 * @param zoneToAdopt The given timezone to be adopted.
329 * @param aLocale The given locale.
330 * @param success Indicates the success/failure of Calendar creation. Filled in
331 * with U_ZERO_ERROR if created successfully, set to a failure result
333 * @return A Calendar if created successfully. NULL otherwise.
336 static Calendar
* U_EXPORT2
createInstance(const TimeZone
& zoneToAdopt
, const Locale
& aLocale
, UErrorCode
& success
);
339 * Returns a list of the locales for which Calendars are installed.
341 * @param count Number of locales returned.
342 * @return An array of Locale objects representing the set of locales for which
343 * Calendars are installed. The system retains ownership of this list;
344 * the caller must NOT delete it. Does not include user-registered Calendars.
347 static const Locale
* U_EXPORT2
getAvailableLocales(int32_t& count
);
350 * Returns the current UTC (GMT) time measured in milliseconds since 0:00:00 on 1/1/70
351 * (derived from the system time).
353 * @return The current UTC time in milliseconds.
356 static UDate U_EXPORT2
getNow(void);
359 * Gets this Calendar's time as milliseconds. May involve recalculation of time due
360 * to previous calls to set time field values. The time specified is non-local UTC
361 * (GMT) time. Although this method is const, this object may actually be changed
362 * (semantically const).
364 * @param status Output param set to success/failure code on exit. If any value
365 * previously set in the time field is invalid or restricted by
366 * leniency, this will be set to an error status.
367 * @return The current time in UTC (GMT) time, or zero if the operation
371 inline UDate
getTime(UErrorCode
& status
) const { return getTimeInMillis(status
); }
374 * Sets this Calendar's current time with the given UDate. The time specified should
375 * be in non-local UTC (GMT) time.
377 * @param date The given UDate in UTC (GMT) time.
378 * @param status Output param set to success/failure code on exit. If any value
379 * set in the time field is invalid or restricted by
380 * leniency, this will be set to an error status.
383 inline void setTime(UDate date
, UErrorCode
& status
) { setTimeInMillis(date
, status
); }
386 * Compares the equality of two Calendar objects. Objects of different subclasses
387 * are considered unequal. This comparison is very exacting; two Calendar objects
388 * must be in exactly the same state to be considered equal. To compare based on the
389 * represented time, use equals() instead.
391 * @param that The Calendar object to be compared with.
392 * @return True if the given Calendar is the same as this Calendar; false
396 virtual UBool
operator==(const Calendar
& that
) const;
399 * Compares the inequality of two Calendar objects.
401 * @param that The Calendar object to be compared with.
402 * @return True if the given Calendar is not the same as this Calendar; false
406 UBool
operator!=(const Calendar
& that
) const {return !operator==(that
);}
409 * Returns TRUE if the given Calendar object is equivalent to this
410 * one. An equivalent Calendar will behave exactly as this one
411 * does, but it may be set to a different time. By contrast, for
412 * the operator==() method to return TRUE, the other Calendar must
413 * be set to the same time.
415 * @param other the Calendar to be compared with this Calendar
418 virtual UBool
isEquivalentTo(const Calendar
& other
) const;
421 * Compares the Calendar time, whereas Calendar::operator== compares the equality of
424 * @param when The Calendar to be compared with this Calendar. Although this is a
425 * const parameter, the object may be modified physically
426 * (semantically const).
427 * @param status Output param set to success/failure code on exit. If any value
428 * previously set in the time field is invalid or restricted by
429 * leniency, this will be set to an error status.
430 * @return True if the current time of this Calendar is equal to the time of
431 * Calendar when; false otherwise.
434 UBool
equals(const Calendar
& when
, UErrorCode
& status
) const;
437 * Returns true if this Calendar's current time is before "when"'s current time.
439 * @param when The Calendar to be compared with this Calendar. Although this is a
440 * const parameter, the object may be modified physically
441 * (semantically const).
442 * @param status Output param set to success/failure code on exit. If any value
443 * previously set in the time field is invalid or restricted by
444 * leniency, this will be set to an error status.
445 * @return True if the current time of this Calendar is before the time of
446 * Calendar when; false otherwise.
449 UBool
before(const Calendar
& when
, UErrorCode
& status
) const;
452 * Returns true if this Calendar's current time is after "when"'s current time.
454 * @param when The Calendar to be compared with this Calendar. Although this is a
455 * const parameter, the object may be modified physically
456 * (semantically const).
457 * @param status Output param set to success/failure code on exit. If any value
458 * previously set in the time field is invalid or restricted by
459 * leniency, this will be set to an error status.
460 * @return True if the current time of this Calendar is after the time of
461 * Calendar when; false otherwise.
464 UBool
after(const Calendar
& when
, UErrorCode
& status
) const;
467 * UDate Arithmetic function. Adds the specified (signed) amount of time to the given
468 * time field, based on the calendar's rules. For example, to subtract 5 days from
469 * the current time of the calendar, call add(Calendar::DATE, -5). When adding on
470 * the month or Calendar::MONTH field, other fields like date might conflict and
471 * need to be changed. For instance, adding 1 month on the date 01/31/96 will result
474 * @param field Specifies which date field to modify.
475 * @param amount The amount of time to be added to the field, in the natural unit
476 * for that field (e.g., days for the day fields, hours for the hour
478 * @param status Output param set to success/failure code on exit. If any value
479 * previously set in the time field is invalid or restricted by
480 * leniency, this will be set to an error status.
481 * @deprecated ICU 2.6. use add(UCalendarDateFields field, int32_t amount, UErrorCode& status) instead.
483 virtual void add(EDateFields field
, int32_t amount
, UErrorCode
& status
);
486 * UDate Arithmetic function. Adds the specified (signed) amount of time to the given
487 * time field, based on the calendar's rules. For example, to subtract 5 days from
488 * the current time of the calendar, call add(Calendar::DATE, -5). When adding on
489 * the month or Calendar::MONTH field, other fields like date might conflict and
490 * need to be changed. For instance, adding 1 month on the date 01/31/96 will result
493 * @param field Specifies which date field to modify.
494 * @param amount The amount of time to be added to the field, in the natural unit
495 * for that field (e.g., days for the day fields, hours for the hour
497 * @param status Output param set to success/failure code on exit. If any value
498 * previously set in the time field is invalid or restricted by
499 * leniency, this will be set to an error status.
502 virtual void add(UCalendarDateFields field
, int32_t amount
, UErrorCode
& status
);
505 * Time Field Rolling function. Rolls (up/down) a single unit of time on the given
506 * time field. For example, to roll the current date up by one day, call
507 * roll(Calendar::DATE, true). When rolling on the year or Calendar::YEAR field, it
508 * will roll the year value in the range between getMinimum(Calendar::YEAR) and the
509 * value returned by getMaximum(Calendar::YEAR). When rolling on the month or
510 * Calendar::MONTH field, other fields like date might conflict and, need to be
511 * changed. For instance, rolling the month up on the date 01/31/96 will result in
512 * 02/29/96. Rolling up always means rolling forward in time; e.g., rolling the year
513 * up on "100 BC" will result in "99 BC", for Gregorian calendar. When rolling on the
514 * hour-in-day or Calendar::HOUR_OF_DAY field, it will roll the hour value in the range
515 * between 0 and 23, which is zero-based.
517 * NOTE: Do not use this method -- use roll(EDateFields, int, UErrorCode&) instead.
519 * @param field The time field.
520 * @param up Indicates if the value of the specified time field is to be rolled
521 * up or rolled down. Use true if rolling up, false otherwise.
522 * @param status Output param set to success/failure code on exit. If any value
523 * previously set in the time field is invalid or restricted by
524 * leniency, this will be set to an error status.
525 * @deprecated ICU 2.6. Use roll(UCalendarDateFields field, UBool up, UErrorCode& status) instead.
527 inline void roll(EDateFields field
, UBool up
, UErrorCode
& status
);
530 * Time Field Rolling function. Rolls (up/down) a single unit of time on the given
531 * time field. For example, to roll the current date up by one day, call
532 * roll(Calendar::DATE, true). When rolling on the year or Calendar::YEAR field, it
533 * will roll the year value in the range between getMinimum(Calendar::YEAR) and the
534 * value returned by getMaximum(Calendar::YEAR). When rolling on the month or
535 * Calendar::MONTH field, other fields like date might conflict and, need to be
536 * changed. For instance, rolling the month up on the date 01/31/96 will result in
537 * 02/29/96. Rolling up always means rolling forward in time; e.g., rolling the year
538 * up on "100 BC" will result in "99 BC", for Gregorian calendar. When rolling on the
539 * hour-in-day or Calendar::HOUR_OF_DAY field, it will roll the hour value in the range
540 * between 0 and 23, which is zero-based.
542 * NOTE: Do not use this method -- use roll(UCalendarDateFields, int, UErrorCode&) instead.
544 * @param field The time field.
545 * @param up Indicates if the value of the specified time field is to be rolled
546 * up or rolled down. Use true if rolling up, false otherwise.
547 * @param status Output param set to success/failure code on exit. If any value
548 * previously set in the time field is invalid or restricted by
549 * leniency, this will be set to an error status.
552 inline void roll(UCalendarDateFields field
, UBool up
, UErrorCode
& status
);
555 * Time Field Rolling function. Rolls by the given amount on the given
556 * time field. For example, to roll the current date up by one day, call
557 * roll(Calendar::DATE, +1, status). When rolling on the month or
558 * Calendar::MONTH field, other fields like date might conflict and, need to be
559 * changed. For instance, rolling the month up on the date 01/31/96 will result in
560 * 02/29/96. Rolling by a positive value always means rolling forward in time;
561 * e.g., rolling the year by +1 on "100 BC" will result in "99 BC", for Gregorian
562 * calendar. When rolling on the hour-in-day or Calendar::HOUR_OF_DAY field, it will
563 * roll the hour value in the range between 0 and 23, which is zero-based.
565 * The only difference between roll() and add() is that roll() does not change
566 * the value of more significant fields when it reaches the minimum or maximum
567 * of its range, whereas add() does.
569 * @param field The time field.
570 * @param amount Indicates amount to roll.
571 * @param status Output param set to success/failure code on exit. If any value
572 * previously set in the time field is invalid, this will be set to
574 * @deprecated ICU 2.6. Use roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) instead.
576 virtual void roll(EDateFields field
, int32_t amount
, UErrorCode
& status
);
579 * Time Field Rolling function. Rolls by the given amount on the given
580 * time field. For example, to roll the current date up by one day, call
581 * roll(Calendar::DATE, +1, status). When rolling on the month or
582 * Calendar::MONTH field, other fields like date might conflict and, need to be
583 * changed. For instance, rolling the month up on the date 01/31/96 will result in
584 * 02/29/96. Rolling by a positive value always means rolling forward in time;
585 * e.g., rolling the year by +1 on "100 BC" will result in "99 BC", for Gregorian
586 * calendar. When rolling on the hour-in-day or Calendar::HOUR_OF_DAY field, it will
587 * roll the hour value in the range between 0 and 23, which is zero-based.
589 * The only difference between roll() and add() is that roll() does not change
590 * the value of more significant fields when it reaches the minimum or maximum
591 * of its range, whereas add() does.
593 * @param field The time field.
594 * @param amount Indicates amount to roll.
595 * @param status Output param set to success/failure code on exit. If any value
596 * previously set in the time field is invalid, this will be set to
600 virtual void roll(UCalendarDateFields field
, int32_t amount
, UErrorCode
& status
);
603 * Return the difference between the given time and the time this
604 * calendar object is set to. If this calendar is set
605 * <em>before</em> the given time, the returned value will be
606 * positive. If this calendar is set <em>after</em> the given
607 * time, the returned value will be negative. The
608 * <code>field</code> parameter specifies the units of the return
609 * value. For example, if <code>fieldDifference(when,
610 * Calendar::MONTH)</code> returns 3, then this calendar is set to
611 * 3 months before <code>when</code>, and possibly some addition
612 * time less than one month.
614 * <p>As a side effect of this call, this calendar is advanced
615 * toward <code>when</code> by the given amount. That is, calling
616 * this method has the side effect of calling <code>add(field,
617 * n)</code>, where <code>n</code> is the return value.
619 * <p>Usage: To use this method, call it first with the largest
620 * field of interest, then with progressively smaller fields. For
624 * int y = cal->fieldDifference(when, Calendar::YEAR, err);
625 * int m = cal->fieldDifference(when, Calendar::MONTH, err);
626 * int d = cal->fieldDifference(when, Calendar::DATE, err);</pre>
628 * computes the difference between <code>cal</code> and
629 * <code>when</code> in years, months, and days.
631 * <p>Note: <code>fieldDifference()</code> is
632 * <em>asymmetrical</em>. That is, in the following code:
635 * cal->setTime(date1, err);
636 * int m1 = cal->fieldDifference(date2, Calendar::MONTH, err);
637 * int d1 = cal->fieldDifference(date2, Calendar::DATE, err);
638 * cal->setTime(date2, err);
639 * int m2 = cal->fieldDifference(date1, Calendar::MONTH, err);
640 * int d2 = cal->fieldDifference(date1, Calendar::DATE, err);</pre>
642 * one might expect that <code>m1 == -m2 && d1 == -d2</code>.
643 * However, this is not generally the case, because of
644 * irregularities in the underlying calendar system (e.g., the
645 * Gregorian calendar has a varying number of days per month).
647 * @param when the date to compare this calendar's time to
648 * @param field the field in which to compute the result
649 * @param status Output param set to success/failure code on exit. If any value
650 * previously set in the time field is invalid, this will be set to
652 * @return the difference, either positive or negative, between
653 * this calendar's time and <code>when</code>, in terms of
654 * <code>field</code>.
655 * @deprecated ICU 2.6. Use fieldDifference(UDate when, UCalendarDateFields field, UErrorCode& status).
657 virtual int32_t fieldDifference(UDate when
, EDateFields field
, UErrorCode
& status
);
660 * Return the difference between the given time and the time this
661 * calendar object is set to. If this calendar is set
662 * <em>before</em> the given time, the returned value will be
663 * positive. If this calendar is set <em>after</em> the given
664 * time, the returned value will be negative. The
665 * <code>field</code> parameter specifies the units of the return
666 * value. For example, if <code>fieldDifference(when,
667 * Calendar::MONTH)</code> returns 3, then this calendar is set to
668 * 3 months before <code>when</code>, and possibly some addition
669 * time less than one month.
671 * <p>As a side effect of this call, this calendar is advanced
672 * toward <code>when</code> by the given amount. That is, calling
673 * this method has the side effect of calling <code>add(field,
674 * n)</code>, where <code>n</code> is the return value.
676 * <p>Usage: To use this method, call it first with the largest
677 * field of interest, then with progressively smaller fields. For
681 * int y = cal->fieldDifference(when, Calendar::YEAR, err);
682 * int m = cal->fieldDifference(when, Calendar::MONTH, err);
683 * int d = cal->fieldDifference(when, Calendar::DATE, err);</pre>
685 * computes the difference between <code>cal</code> and
686 * <code>when</code> in years, months, and days.
688 * <p>Note: <code>fieldDifference()</code> is
689 * <em>asymmetrical</em>. That is, in the following code:
692 * cal->setTime(date1, err);
693 * int m1 = cal->fieldDifference(date2, Calendar::MONTH, err);
694 * int d1 = cal->fieldDifference(date2, Calendar::DATE, err);
695 * cal->setTime(date2, err);
696 * int m2 = cal->fieldDifference(date1, Calendar::MONTH, err);
697 * int d2 = cal->fieldDifference(date1, Calendar::DATE, err);</pre>
699 * one might expect that <code>m1 == -m2 && d1 == -d2</code>.
700 * However, this is not generally the case, because of
701 * irregularities in the underlying calendar system (e.g., the
702 * Gregorian calendar has a varying number of days per month).
704 * @param when the date to compare this calendar's time to
705 * @param field the field in which to compute the result
706 * @param status Output param set to success/failure code on exit. If any value
707 * previously set in the time field is invalid, this will be set to
709 * @return the difference, either positive or negative, between
710 * this calendar's time and <code>when</code>, in terms of
711 * <code>field</code>.
714 virtual int32_t fieldDifference(UDate when
, UCalendarDateFields field
, UErrorCode
& status
);
717 * Sets the calendar's time zone to be the one passed in. The Calendar takes ownership
718 * of the TimeZone; the caller is no longer responsible for deleting it. If the
719 * given time zone is NULL, this function has no effect.
721 * @param value The given time zone.
724 void adoptTimeZone(TimeZone
* value
);
727 * Sets the calendar's time zone to be the same as the one passed in. The TimeZone
728 * passed in is _not_ adopted; the client is still responsible for deleting it.
730 * @param zone The given time zone.
733 void setTimeZone(const TimeZone
& zone
);
736 * Returns a reference to the time zone owned by this calendar. The returned reference
737 * is only valid until clients make another call to adoptTimeZone or setTimeZone,
738 * or this Calendar is destroyed.
740 * @return The time zone object associated with this calendar.
743 const TimeZone
& getTimeZone(void) const;
746 * Returns the time zone owned by this calendar. The caller owns the returned object
747 * and must delete it when done. After this call, the new time zone associated
748 * with this Calendar is the default TimeZone as returned by TimeZone::createDefault().
750 * @return The time zone object which was associated with this calendar.
753 TimeZone
* orphanTimeZone(void);
756 * Queries if the current date for this Calendar is in Daylight Savings Time.
758 * @param status Fill-in parameter which receives the status of this operation.
759 * @return True if the current date for this Calendar is in Daylight Savings Time,
763 virtual UBool
inDaylightTime(UErrorCode
& status
) const = 0;
766 * Specifies whether or not date/time interpretation is to be lenient. With lenient
767 * interpretation, a date such as "February 942, 1996" will be treated as being
768 * equivalent to the 941st day after February 1, 1996. With strict interpretation,
769 * such dates will cause an error when computing time from the time field values
770 * representing the dates.
772 * @param lenient True specifies date/time interpretation to be lenient.
774 * @see DateFormat#setLenient
777 void setLenient(UBool lenient
);
780 * Tells whether date/time interpretation is to be lenient.
782 * @return True tells that date/time interpretation is to be lenient.
785 UBool
isLenient(void) const;
788 * Sets what the first day of the week is; e.g., Sunday in US, Monday in France.
790 * @param value The given first day of the week.
791 * @deprecated ICU 2.6. Use setFirstDayOfWeek(UCalendarDaysOfWeek value) instead.
793 void setFirstDayOfWeek(EDaysOfWeek value
);
796 * Sets what the first day of the week is; e.g., Sunday in US, Monday in France.
798 * @param value The given first day of the week.
801 void setFirstDayOfWeek(UCalendarDaysOfWeek value
);
804 * Gets what the first day of the week is; e.g., Sunday in US, Monday in France.
806 * @return The first day of the week.
807 * @deprecated ICU 2.6 use the overload with error code
809 EDaysOfWeek
getFirstDayOfWeek(void) const;
812 * Gets what the first day of the week is; e.g., Sunday in US, Monday in France.
814 * @param status error code
815 * @return The first day of the week.
818 UCalendarDaysOfWeek
getFirstDayOfWeek(UErrorCode
&status
) const;
821 * Sets what the minimal days required in the first week of the year are; For
822 * example, if the first week is defined as one that contains the first day of the
823 * first month of a year, call the method with value 1. If it must be a full week,
826 * @param value The given minimal days required in the first week of the year.
829 void setMinimalDaysInFirstWeek(uint8_t value
);
832 * Gets what the minimal days required in the first week of the year are; e.g., if
833 * the first week is defined as one that contains the first day of the first month
834 * of a year, getMinimalDaysInFirstWeek returns 1. If the minimal days required must
835 * be a full week, getMinimalDaysInFirstWeek returns 7.
837 * @return The minimal days required in the first week of the year.
840 uint8_t getMinimalDaysInFirstWeek(void) const;
843 * Gets the minimum value for the given time field. e.g., for Gregorian
846 * @param field The given time field.
847 * @return The minimum value for the given time field.
848 * @deprecated ICU 2.6. Use getMinimum(UCalendarDateFields field) instead.
850 virtual int32_t getMinimum(EDateFields field
) const;
853 * Gets the minimum value for the given time field. e.g., for Gregorian
856 * @param field The given time field.
857 * @return The minimum value for the given time field.
860 virtual int32_t getMinimum(UCalendarDateFields field
) const;
863 * Gets the maximum value for the given time field. e.g. for Gregorian DAY_OF_MONTH,
866 * @param field The given time field.
867 * @return The maximum value for the given time field.
868 * @deprecated ICU 2.6. Use getMaximum(UCalendarDateFields field) instead.
870 virtual int32_t getMaximum(EDateFields field
) const;
873 * Gets the maximum value for the given time field. e.g. for Gregorian DAY_OF_MONTH,
876 * @param field The given time field.
877 * @return The maximum value for the given time field.
880 virtual int32_t getMaximum(UCalendarDateFields field
) const;
883 * Gets the highest minimum value for the given field if varies. Otherwise same as
884 * getMinimum(). For Gregorian, no difference.
886 * @param field The given time field.
887 * @return The highest minimum value for the given time field.
888 * @deprecated ICU 2.6. Use getGreatestMinimum(UCalendarDateFields field) instead.
890 virtual int32_t getGreatestMinimum(EDateFields field
) const;
893 * Gets the highest minimum value for the given field if varies. Otherwise same as
894 * getMinimum(). For Gregorian, no difference.
896 * @param field The given time field.
897 * @return The highest minimum value for the given time field.
900 virtual int32_t getGreatestMinimum(UCalendarDateFields field
) const;
903 * Gets the lowest maximum value for the given field if varies. Otherwise same as
904 * getMaximum(). e.g., for Gregorian DAY_OF_MONTH, 28.
906 * @param field The given time field.
907 * @return The lowest maximum value for the given time field.
908 * @deprecated ICU 2.6. Use getLeastMaximum(UCalendarDateFields field) instead.
910 virtual int32_t getLeastMaximum(EDateFields field
) const;
913 * Gets the lowest maximum value for the given field if varies. Otherwise same as
914 * getMaximum(). e.g., for Gregorian DAY_OF_MONTH, 28.
916 * @param field The given time field.
917 * @return The lowest maximum value for the given time field.
920 virtual int32_t getLeastMaximum(UCalendarDateFields field
) const;
923 * Return the minimum value that this field could have, given the current date.
924 * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().
926 * The version of this function on Calendar uses an iterative algorithm to determine the
927 * actual minimum value for the field. There is almost always a more efficient way to
928 * accomplish this (in most cases, you can simply return getMinimum()). GregorianCalendar
929 * overrides this function with a more efficient implementation.
931 * @param field the field to determine the minimum of
932 * @param status Fill-in parameter which receives the status of this operation.
933 * @return the minimum of the given field for the current date of this Calendar
934 * @deprecated ICU 2.6. Use getActualMinimum(UCalendarDateFields field, UErrorCode& status) instead.
936 int32_t getActualMinimum(EDateFields field
, UErrorCode
& status
) const;
939 * Return the minimum value that this field could have, given the current date.
940 * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().
942 * The version of this function on Calendar uses an iterative algorithm to determine the
943 * actual minimum value for the field. There is almost always a more efficient way to
944 * accomplish this (in most cases, you can simply return getMinimum()). GregorianCalendar
945 * overrides this function with a more efficient implementation.
947 * @param field the field to determine the minimum of
948 * @param status Fill-in parameter which receives the status of this operation.
949 * @return the minimum of the given field for the current date of this Calendar
952 int32_t getActualMinimum(UCalendarDateFields field
, UErrorCode
& status
) const;
955 * Return the maximum value that this field could have, given the current date.
956 * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual
957 * maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar,
958 * for some years the actual maximum for MONTH is 12, and for others 13.
960 * The version of this function on Calendar uses an iterative algorithm to determine the
961 * actual maximum value for the field. There is almost always a more efficient way to
962 * accomplish this (in most cases, you can simply return getMaximum()). GregorianCalendar
963 * overrides this function with a more efficient implementation.
965 * @param field the field to determine the maximum of
966 * @param status Fill-in parameter which receives the status of this operation.
967 * @return the maximum of the given field for the current date of this Calendar
968 * @deprecated ICU 2.6. Use getActualMaximum(UCalendarDateFields field, UErrorCode& status) instead.
970 int32_t getActualMaximum(EDateFields field
, UErrorCode
& status
) const;
973 * Return the maximum value that this field could have, given the current date.
974 * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual
975 * maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar,
976 * for some years the actual maximum for MONTH is 12, and for others 13.
978 * The version of this function on Calendar uses an iterative algorithm to determine the
979 * actual maximum value for the field. There is almost always a more efficient way to
980 * accomplish this (in most cases, you can simply return getMaximum()). GregorianCalendar
981 * overrides this function with a more efficient implementation.
983 * @param field the field to determine the maximum of
984 * @param status Fill-in parameter which receives the status of this operation.
985 * @return the maximum of the given field for the current date of this Calendar
988 int32_t getActualMaximum(UCalendarDateFields field
, UErrorCode
& status
) const;
991 * Gets the value for a given time field. Recalculate the current time field values
992 * if the time value has been changed by a call to setTime(). Return zero for unset
993 * fields if any fields have been explicitly set by a call to set(). To force a
994 * recomputation of all fields regardless of the previous state, call complete().
995 * This method is semantically const, but may alter the object in memory.
997 * @param field The given time field.
998 * @param status Fill-in parameter which receives the status of the operation.
999 * @return The value for the given time field, or zero if the field is unset,
1000 * and set() has been called for any other field.
1001 * @deprecated ICU 2.6. Use get(UCalendarDateFields field, UErrorCode& status) instead.
1003 int32_t get(EDateFields field
, UErrorCode
& status
) const;
1006 * Gets the value for a given time field. Recalculate the current time field values
1007 * if the time value has been changed by a call to setTime(). Return zero for unset
1008 * fields if any fields have been explicitly set by a call to set(). To force a
1009 * recomputation of all fields regardless of the previous state, call complete().
1010 * This method is semantically const, but may alter the object in memory.
1012 * @param field The given time field.
1013 * @param status Fill-in parameter which receives the status of the operation.
1014 * @return The value for the given time field, or zero if the field is unset,
1015 * and set() has been called for any other field.
1018 int32_t get(UCalendarDateFields field
, UErrorCode
& status
) const;
1021 * Determines if the given time field has a value set. This can affect in the
1022 * resolving of time in Calendar. Unset fields have a value of zero, by definition.
1024 * @param field The given time field.
1025 * @return True if the given time field has a value set; false otherwise.
1026 * @deprecated ICU 2.6. Use isSet(UCalendarDateFields field) instead.
1028 UBool
isSet(EDateFields field
) const;
1031 * Determines if the given time field has a value set. This can affect in the
1032 * resolving of time in Calendar. Unset fields have a value of zero, by definition.
1034 * @param field The given time field.
1035 * @return True if the given time field has a value set; false otherwise.
1038 UBool
isSet(UCalendarDateFields field
) const;
1041 * Sets the given time field with the given value.
1043 * @param field The given time field.
1044 * @param value The value to be set for the given time field.
1045 * @deprecated ICU 2.6. Use set(UCalendarDateFields field, int32_t value) instead.
1047 void set(EDateFields field
, int32_t value
);
1050 * Sets the given time field with the given value.
1052 * @param field The given time field.
1053 * @param value The value to be set for the given time field.
1056 void set(UCalendarDateFields field
, int32_t value
);
1059 * Sets the values for the fields YEAR, MONTH, and DATE. Other field values are
1060 * retained; call clear() first if this is not desired.
1062 * @param year The value used to set the YEAR time field.
1063 * @param month The value used to set the MONTH time field. Month value is 0-based.
1064 * e.g., 0 for January.
1065 * @param date The value used to set the DATE time field.
1068 void set(int32_t year
, int32_t month
, int32_t date
);
1071 * Sets the values for the fields YEAR, MONTH, DATE, HOUR_OF_DAY, and MINUTE. Other
1072 * field values are retained; call clear() first if this is not desired.
1074 * @param year The value used to set the YEAR time field.
1075 * @param month The value used to set the MONTH time field. Month value is
1076 * 0-based. E.g., 0 for January.
1077 * @param date The value used to set the DATE time field.
1078 * @param hour The value used to set the HOUR_OF_DAY time field.
1079 * @param minute The value used to set the MINUTE time field.
1082 void set(int32_t year
, int32_t month
, int32_t date
, int32_t hour
, int32_t minute
);
1085 * Sets the values for the fields YEAR, MONTH, DATE, HOUR_OF_DAY, MINUTE, and SECOND.
1086 * Other field values are retained; call clear() first if this is not desired.
1088 * @param year The value used to set the YEAR time field.
1089 * @param month The value used to set the MONTH time field. Month value is
1090 * 0-based. E.g., 0 for January.
1091 * @param date The value used to set the DATE time field.
1092 * @param hour The value used to set the HOUR_OF_DAY time field.
1093 * @param minute The value used to set the MINUTE time field.
1094 * @param second The value used to set the SECOND time field.
1097 void set(int32_t year
, int32_t month
, int32_t date
, int32_t hour
, int32_t minute
, int32_t second
);
1100 * Clears the values of all the time fields, making them both unset and assigning
1101 * them a value of zero. The field values will be determined during the next
1102 * resolving of time into time fields.
1108 * Clears the value in the given time field, both making it unset and assigning it a
1109 * value of zero. This field value will be determined during the next resolving of
1110 * time into time fields.
1112 * @param field The time field to be cleared.
1113 * @deprecated ICU 2.6. Use clear(UCalendarDateFields field) instead.
1115 void clear(EDateFields field
);
1118 * Clears the value in the given time field, both making it unset and assigning it a
1119 * value of zero. This field value will be determined during the next resolving of
1120 * time into time fields.
1122 * @param field The time field to be cleared.
1125 void clear(UCalendarDateFields field
);
1128 * Returns a unique class ID POLYMORPHICALLY. Pure virtual method. This method is to
1129 * implement a simple version of RTTI, since not all C++ compilers support genuine
1130 * RTTI. Polymorphic operator==() and clone() methods call this method.
1132 * Concrete subclasses of Calendar must implement getDynamicClassID() and also a
1133 * static method and data member:
1135 * static UClassID getStaticClassID() { return (UClassID)&fgClassID; }
1136 * static char fgClassID;
1138 * @return The class ID for this object. All objects of a given class have the
1139 * same class ID. Objects of other classes have different class IDs.
1142 virtual UClassID
getDynamicClassID(void) const = 0;
1145 * Returns the resource key string used for this calendar type.
1146 * For example, prepending "Eras_" to this string could return "Eras_japanese"
1147 * or "Eras_gregorian".
1149 * @returns static string, for example, "gregorian" or "japanese"
1152 virtual const char * getType() const = 0;
1157 * Constructs a Calendar with the default time zone as returned by
1158 * TimeZone::createInstance(), and the default locale.
1160 * @param success Indicates the status of Calendar object construction. Returns
1161 * U_ZERO_ERROR if constructed successfully.
1164 Calendar(UErrorCode
& success
);
1169 * @param source Calendar object to be copied from
1172 Calendar(const Calendar
& source
);
1175 * Default assignment operator
1177 * @param right Calendar object to be copied
1180 Calendar
& operator=(const Calendar
& right
);
1183 * Constructs a Calendar with the given time zone and locale. Clients are no longer
1184 * responsible for deleting the given time zone object after it's adopted.
1186 * @param zone The given time zone.
1187 * @param aLocale The given locale.
1188 * @param success Indicates the status of Calendar object construction. Returns
1189 * U_ZERO_ERROR if constructed successfully.
1192 Calendar(TimeZone
* zone
, const Locale
& aLocale
, UErrorCode
& success
);
1195 * Constructs a Calendar with the given time zone and locale.
1197 * @param zone The given time zone.
1198 * @param aLocale The given locale.
1199 * @param success Indicates the status of Calendar object construction. Returns
1200 * U_ZERO_ERROR if constructed successfully.
1203 Calendar(const TimeZone
& zone
, const Locale
& aLocale
, UErrorCode
& success
);
1206 * Converts Calendar's time field values to GMT as milliseconds.
1208 * @param status Output param set to success/failure code on exit. If any value
1209 * previously set in the time field is invalid or restricted by
1210 * leniency, this will be set to an error status.
1213 virtual void computeTime(UErrorCode
& status
);
1216 * Converts GMT as milliseconds to time field values. This allows you to sync up the
1217 * time field values with a new time that is set for the calendar. This method
1218 * does NOT recompute the time first; to recompute the time, then the fields, use
1219 * the method complete().
1221 * @param status Output param set to success/failure code on exit. If any value
1222 * previously set in the time field is invalid or restricted by
1223 * leniency, this will be set to an error status.
1226 virtual void computeFields(UErrorCode
& status
);
1229 * Gets this Calendar's current time as a long.
1231 * @param status Output param set to success/failure code on exit. If any value
1232 * previously set in the time field is invalid or restricted by
1233 * leniency, this will be set to an error status.
1234 * @return the current time as UTC milliseconds from the epoch.
1237 double getTimeInMillis(UErrorCode
& status
) const;
1240 * Sets this Calendar's current time from the given long value.
1241 * @param millis the new time in UTC milliseconds from the epoch.
1242 * @param status Output param set to success/failure code on exit. If any value
1243 * previously set in the time field is invalid or restricted by
1244 * leniency, this will be set to an error status.
1247 void setTimeInMillis( double millis
, UErrorCode
& status
);
1250 * Recomputes the current time from currently set fields, and then fills in any
1251 * unset fields in the time field list.
1253 * @param status Output param set to success/failure code on exit. If any value
1254 * previously set in the time field is invalid or restricted by
1255 * leniency, this will be set to an error status.
1258 void complete(UErrorCode
& status
);
1261 * Gets the value for a given time field. Subclasses can use this function to get
1262 * field values without forcing recomputation of time.
1264 * @param field The given time field.
1265 * @return The value for the given time field.
1266 * @deprecated ICU 2.6. Use internalGet(UCalendarDateFields field) instead.
1268 inline int32_t internalGet(EDateFields field
) const {return fFields
[field
];}
1271 * Gets the value for a given time field. Subclasses can use this function to get
1272 * field values without forcing recomputation of time. If the field's stamp is UNSET,
1273 * the defaultValue is used.
1275 * @param field The given time field.
1276 * @param defaultValue a default value used if the field is unset.
1277 * @return The value for the given time field.
1280 inline int32_t internalGet(UCalendarDateFields field
, int32_t defaultValue
) const {return fStamp
[field
]>kUnset
? fFields
[field
] : defaultValue
;}
1283 * Gets the value for a given time field. Subclasses can use this function to get
1284 * field values without forcing recomputation of time.
1286 * @param field The given time field.
1287 * @return The value for the given time field.
1290 inline int32_t internalGet(UCalendarDateFields field
) const {return fFields
[field
];}
1293 * Sets the value for a given time field. This is a fast internal method for
1294 * subclasses. It does not affect the areFieldsInSync, isTimeSet, or areAllFieldsSet
1297 * @param field The given time field.
1298 * @param value The value for the given time field.
1299 * @deprecated ICU 2.6. Use internalSet(UCalendarDateFields field, int32_t value) instead.
1301 void internalSet(EDateFields field
, int32_t value
);
1304 * Sets the value for a given time field. This is a fast internal method for
1305 * subclasses. It does not affect the areFieldsInSync, isTimeSet, or areAllFieldsSet
1308 * @param field The given time field.
1309 * @param value The value for the given time field.
1312 inline void internalSet(UCalendarDateFields field
, int32_t value
);
1315 * Prepare this calendar for computing the actual minimum or maximum.
1316 * This method modifies this calendar's fields; it is called on a
1317 * temporary calendar.
1320 virtual void prepareGetActual(UCalendarDateFields field
, UBool isMinimum
, UErrorCode
&status
);
1323 * Limit enums. Not in sync with UCalendarLimitType (refers to internal fields).
1327 UCAL_LIMIT_MINIMUM
= 0,
1328 UCAL_LIMIT_GREATEST_MINIMUM
,
1329 UCAL_LIMIT_LEAST_MAXIMUM
,
1335 * Subclass API for defining limits of different types.
1336 * Subclasses must implement this method to return limits for the
1343 * UCAL_WEEK_OF_MONTH
1344 * UCAL_DATE (DAY_OF_MONTH on Java)
1346 * UCAL_DAY_OF_WEEK_IN_MONTH
1348 * UCAL_EXTENDED_YEAR</pre>
1350 * @param field one of the above field numbers
1351 * @param limitType one of <code>MINIMUM</code>, <code>GREATEST_MINIMUM</code>,
1352 * <code>LEAST_MAXIMUM</code>, or <code>MAXIMUM</code>
1355 virtual int32_t handleGetLimit(UCalendarDateFields field
, ELimitType limitType
) const = 0;
1358 * Return a limit for a field.
1359 * @param field the field, from <code>0..UCAL_MAX_FIELD</code>
1360 * @param limitType the type specifier for the limit
1364 virtual int32_t getLimit(UCalendarDateFields field
, ELimitType limitType
) const;
1368 * Return the Julian day number of day before the first day of the
1369 * given month in the given extended year. Subclasses should override
1370 * this method to implement their calendar system.
1371 * @param eyear the extended year
1372 * @param month the zero-based month, or 0 if useMonth is false
1373 * @param useMonth if false, compute the day before the first day of
1374 * the given year, otherwise, compute the day before the first day of
1376 * @return the Julian day number of the day before the first
1377 * day of the given month and year
1380 virtual int32_t handleComputeMonthStart(int32_t eyear
, int32_t month
,
1381 UBool useMonth
) const = 0;
1384 * Return the number of days in the given month of the given extended
1385 * year of this calendar system. Subclasses should override this
1386 * method if they can provide a more correct or more efficient
1387 * implementation than the default implementation in Calendar.
1390 virtual int32_t handleGetMonthLength(int32_t extendedYear
, int32_t month
) const ;
1393 * Return the number of days in the given extended year of this
1394 * calendar system. Subclasses should override this method if they can
1395 * provide a more correct or more efficient implementation than the
1396 * default implementation in Calendar.
1399 virtual int32_t handleGetYearLength(int32_t eyear
) const;
1403 * Return the extended year defined by the current fields. This will
1404 * use the UCAL_EXTENDED_YEAR field or the UCAL_YEAR and supra-year fields (such
1405 * as UCAL_ERA) specific to the calendar system, depending on which set of
1407 * @return the extended year
1410 virtual int32_t handleGetExtendedYear() = 0;
1413 * Subclasses may override this. This method calls
1414 * handleGetMonthLength() to obtain the calendar-specific month
1416 * @param bestField which field to use to calculate the date
1417 * @return julian day specified by calendar fields.
1420 virtual int32_t handleComputeJulianDay(UCalendarDateFields bestField
);
1423 * Subclasses must override this to convert from week fields
1424 * (YEAR_WOY and WEEK_OF_YEAR) to an extended year in the case
1425 * where YEAR, EXTENDED_YEAR are not set.
1426 * The Calendar implementation assumes yearWoy is in extended gregorian form
1428 * @return the extended year, UCAL_EXTENDED_YEAR
1430 virtual int32_t handleGetExtendedYearFromWeekFields(int32_t yearWoy
, int32_t woy
);
1433 * Compute the Julian day from fields. Will determine whether to use
1434 * the JULIAN_DAY field directly, or other fields.
1435 * @return the julian day
1438 int32_t computeJulianDay();
1441 * Compute the milliseconds in the day from the fields. This is a
1442 * value from 0 to 23:59:59.999 inclusive, unless fields are out of
1443 * range, in which case it can be an arbitrary value. This value
1444 * reflects local zone wall time.
1447 int32_t computeMillisInDay();
1450 * This method can assume EXTENDED_YEAR has been set.
1451 * @param millis milliseconds of the date fields
1452 * @param millisInDay milliseconds of the time fields; may be out
1454 * @param ec Output param set to failure code on function return
1455 * when this function fails.
1458 int32_t computeZoneOffset(double millis
, int32_t millisInDay
, UErrorCode
&ec
);
1462 * Determine the best stamp in a range.
1463 * @param start first enum to look at
1464 * @param end last enum to look at
1465 * @param bestSoFar stamp prior to function call
1466 * @return the stamp value of the best stamp
1469 int32_t newestStamp(UCalendarDateFields start
, UCalendarDateFields end
, int32_t bestSoFar
) const;
1472 * Values for field resolution tables
1473 * @see #resolveFields
1477 /** Marker for end of resolve set (row or group). */
1479 /** Value to be bitwised "ORed" against resolve table field values for remapping. Example: (UCAL_DATE | kResolveRemap) in 1st column will cause 'UCAL_DATE' to be returned, but will not examine the value of UCAL_DATE. */
1484 * Precedence table for Dates
1485 * @see #resolveFields
1488 static const UFieldResolutionTable kDatePrecedence
[];
1491 * Precedence table for Year
1492 * @see #resolveFields
1495 static const UFieldResolutionTable kYearPrecedence
[];
1498 * Precedence table for Day of Week
1499 * @see #resolveFields
1502 static const UFieldResolutionTable kDOWPrecedence
[];
1505 * Given a precedence table, return the newest field combination in
1506 * the table, or UCAL_FIELD_COUNT if none is found.
1508 * <p>The precedence table is a 3-dimensional array of integers. It
1509 * may be thought of as an array of groups. Each group is an array of
1510 * lines. Each line is an array of field numbers. Within a line, if
1511 * all fields are set, then the time stamp of the line is taken to be
1512 * the stamp of the most recently set field. If any field of a line is
1513 * unset, then the line fails to match. Within a group, the line with
1514 * the newest time stamp is selected. The first field of the line is
1515 * returned to indicate which line matched.
1517 * <p>In some cases, it may be desirable to map a line to field that
1518 * whose stamp is NOT examined. For example, if the best field is
1519 * DAY_OF_WEEK then the DAY_OF_WEEK_IN_MONTH algorithm may be used. In
1520 * order to do this, insert the value <code>kResolveRemap | F</code> at
1521 * the start of the line, where <code>F</code> is the desired return
1522 * field value. This field will NOT be examined; it only determines
1523 * the return value if the other fields in the line are the newest.
1525 * <p>If all lines of a group contain at least one unset field, then no
1526 * line will match, and the group as a whole will fail to match. In
1527 * that case, the next group will be processed. If all groups fail to
1528 * match, then UCAL_FIELD_COUNT is returned.
1531 UCalendarDateFields
resolveFields(const UFieldResolutionTable
*precedenceTable
);
1537 virtual const UFieldResolutionTable
* getFieldResolutionTable() const;
1540 * Return the field that is newer, either defaultField, or
1541 * alternateField. If neither is newer or neither is set, return defaultField.
1544 UCalendarDateFields
newerField(UCalendarDateFields defaultField
, UCalendarDateFields alternateField
) const;
1549 * Helper function for calculating limits by trial and error
1550 * @param field The field being investigated
1551 * @param startValue starting (least max) value of field
1552 * @param endValue ending (greatest max) value of field
1553 * @param status return type
1556 int32_t getActualHelper(UCalendarDateFields field
, int32_t startValue
, int32_t endValue
, UErrorCode
&status
) const;
1561 * The flag which indicates if the current time is set in the calendar.
1567 * True if the fields are in sync with the currently set time of this Calendar.
1568 * If false, then the next attempt to get the value of a field will
1569 * force a recomputation of all fields from the current value of the time
1572 * This should really be named areFieldsInSync, but the old name is retained
1573 * for backward compatibility.
1576 UBool fAreFieldsSet
;
1579 * True if all of the fields have been set. This is initially false, and set to
1580 * true by computeFields().
1583 UBool fAreAllFieldsSet
;
1586 * True if all fields have been virtually set, but have not yet been
1587 * computed. This occurs only in setTimeInMillis(). A calendar set
1588 * to this state will compute all fields from the time if it becomes
1589 * necessary, but otherwise will delay such computation.
1592 UBool fAreFieldsVirtuallySet
;
1595 * Get the current time without recomputing.
1597 * @return the current time without recomputing.
1600 UDate
internalGetTime(void) const { return fTime
; }
1603 * Set the current time without affecting flags or fields.
1605 * @param time The time to be set
1606 * @return the current time without recomputing.
1609 void internalSetTime(UDate time
) { fTime
= time
; }
1612 * The time fields containing values into which the millis is computed.
1615 int32_t fFields
[UCAL_FIELD_COUNT
];
1618 * The flags which tell if a specified time field for the calendar is set.
1619 * @deprecated ICU 2.8 use (fStamp[n]!=kUnset)
1621 UBool fIsSet
[UCAL_FIELD_COUNT
];
1623 /** Special values of stamp[]
1633 * Pseudo-time-stamps which specify when each field was set. There
1634 * are two special values, UNSET and INTERNALLY_SET. Values from
1635 * MINIMUM_USER_SET to Integer.MAX_VALUE are legal user set values.
1638 int32_t fStamp
[UCAL_FIELD_COUNT
];
1641 * Subclasses may override this method to compute several fields
1642 * specific to each calendar system. These are:
1649 * <li>EXTENDED_YEAR</ul>
1651 * Subclasses can refer to the DAY_OF_WEEK and DOW_LOCAL fields, which
1652 * will be set when this method is called. Subclasses can also call
1653 * the getGregorianXxx() methods to obtain Gregorian calendar
1654 * equivalents for the given Julian day.
1656 * <p>In addition, subclasses should compute any subclass-specific
1657 * fields, that is, fields from BASE_FIELD_COUNT to
1658 * getFieldCount() - 1.
1660 * <p>The default implementation in <code>Calendar</code> implements
1661 * a pure proleptic Gregorian calendar.
1664 virtual void handleComputeFields(int32_t julianDay
, UErrorCode
&status
);
1667 * Return the extended year on the Gregorian calendar as computed by
1668 * <code>computeGregorianFields()</code>.
1669 * @see #computeGregorianFields
1672 int32_t getGregorianYear() const {
1673 return fGregorianYear
;
1677 * Return the month (0-based) on the Gregorian calendar as computed by
1678 * <code>computeGregorianFields()</code>.
1679 * @see #computeGregorianFields
1682 int32_t getGregorianMonth() const {
1683 return fGregorianMonth
;
1687 * Return the day of year (1-based) on the Gregorian calendar as
1688 * computed by <code>computeGregorianFields()</code>.
1689 * @see #computeGregorianFields
1692 int32_t getGregorianDayOfYear() const {
1693 return fGregorianDayOfYear
;
1697 * Return the day of month (1-based) on the Gregorian calendar as
1698 * computed by <code>computeGregorianFields()</code>.
1699 * @see #computeGregorianFields
1702 int32_t getGregorianDayOfMonth() const {
1703 return fGregorianDayOfMonth
;
1707 * Called by computeJulianDay. Returns the default month (0-based) for the year,
1708 * taking year and era into account. Defaults to 0 for Gregorian, which doesn't care.
1712 virtual int32_t getDefaultMonthInYear() ;
1716 * Called by computeJulianDay. Returns the default day (1-based) for the month,
1717 * taking currently-set year and era into account. Defaults to 1 for Gregorian.
1720 virtual int32_t getDefaultDayInMonth(int32_t /*month*/);
1722 //-------------------------------------------------------------------------
1723 // Protected utility methods for use by subclasses. These are very handy
1724 // for implementing add, roll, and computeFields.
1725 //-------------------------------------------------------------------------
1728 * Adjust the specified field so that it is within
1729 * the allowable range for the date to which this calendar is set.
1730 * For example, in a Gregorian calendar pinning the {@link #UCalendarDateFields DAY_OF_MONTH}
1731 * field for a calendar set to April 31 would cause it to be set
1734 * <b>Subclassing:</b>
1736 * This utility method is intended for use by subclasses that need to implement
1737 * their own overrides of {@link #roll roll} and {@link #add add}.
1740 * <code>pinField</code> is implemented in terms of
1741 * {@link #getActualMinimum getActualMinimum}
1742 * and {@link #getActualMaximum getActualMaximum}. If either of those methods uses
1743 * a slow, iterative algorithm for a particular field, it would be
1744 * unwise to attempt to call <code>pinField</code> for that field. If you
1745 * really do need to do so, you should override this method to do
1746 * something more efficient for that field.
1748 * @param field The calendar field whose value should be pinned.
1749 * @param status Output param set to failure code on function return
1750 * when this function fails.
1752 * @see #getActualMinimum
1753 * @see #getActualMaximum
1756 virtual void pinField(UCalendarDateFields field
, UErrorCode
& status
);
1759 * Return the week number of a day, within a period. This may be the week number in
1760 * a year or the week number in a month. Usually this will be a value >= 1, but if
1761 * some initial days of the period are excluded from week 1, because
1762 * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek} is > 1, then
1763 * the week number will be zero for those
1764 * initial days. This method requires the day number and day of week for some
1765 * known date in the period in order to determine the day of week
1766 * on the desired day.
1768 * <b>Subclassing:</b>
1770 * This method is intended for use by subclasses in implementing their
1771 * {@link #computeTime computeTime} and/or {@link #computeFields computeFields} methods.
1772 * It is often useful in {@link #getActualMinimum getActualMinimum} and
1773 * {@link #getActualMaximum getActualMaximum} as well.
1775 * This variant is handy for computing the week number of some other
1776 * day of a period (often the first or last day of the period) when its day
1777 * of the week is not known but the day number and day of week for some other
1778 * day in the period (e.g. the current date) <em>is</em> known.
1780 * @param desiredDay The {@link #UCalendarDateFields DAY_OF_YEAR} or
1781 * {@link #UCalendarDateFields DAY_OF_MONTH} whose week number is desired.
1782 * Should be 1 for the first day of the period.
1784 * @param dayOfPeriod The {@link #UCalendarDateFields DAY_OF_YEAR}
1785 * or {@link #UCalendarDateFields DAY_OF_MONTH} for a day in the period whose
1786 * {@link #UCalendarDateFields DAY_OF_WEEK} is specified by the
1787 * <code>knownDayOfWeek</code> parameter.
1788 * Should be 1 for first day of period.
1790 * @param dayOfWeek The {@link #UCalendarDateFields DAY_OF_WEEK} for the day
1791 * corresponding to the <code>knownDayOfPeriod</code> parameter.
1792 * 1-based with 1=Sunday.
1794 * @return The week number (one-based), or zero if the day falls before
1795 * the first week because
1796 * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek}
1801 int32_t weekNumber(int32_t desiredDay
, int32_t dayOfPeriod
, int32_t dayOfWeek
);
1805 * Return the week number of a day, within a period. This may be the week number in
1806 * a year, or the week number in a month. Usually this will be a value >= 1, but if
1807 * some initial days of the period are excluded from week 1, because
1808 * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek} is > 1,
1809 * then the week number will be zero for those
1810 * initial days. This method requires the day of week for the given date in order to
1811 * determine the result.
1813 * <b>Subclassing:</b>
1815 * This method is intended for use by subclasses in implementing their
1816 * {@link #computeTime computeTime} and/or {@link #computeFields computeFields} methods.
1817 * It is often useful in {@link #getActualMinimum getActualMinimum} and
1818 * {@link #getActualMaximum getActualMaximum} as well.
1820 * @param dayOfPeriod The {@link #UCalendarDateFields DAY_OF_YEAR} or
1821 * {@link #UCalendarDateFields DAY_OF_MONTH} whose week number is desired.
1822 * Should be 1 for the first day of the period.
1824 * @param dayOfWeek The {@link #UCalendarDateFields DAY_OF_WEEK} for the day
1825 * corresponding to the <code>dayOfPeriod</code> parameter.
1826 * 1-based with 1=Sunday.
1828 * @return The week number (one-based), or zero if the day falls before
1829 * the first week because
1830 * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek}
1834 inline int32_t weekNumber(int32_t dayOfPeriod
, int32_t dayOfWeek
);
1837 * returns the local DOW, valid range 0..6
1840 int32_t getLocalDOW();
1845 * The next available value for fStamp[]
1847 int32_t fNextStamp
;// = MINIMUM_USER_STAMP;
1850 * The current time set for the calendar.
1860 * Time zone affects the time calculation done by Calendar. Calendar subclasses use
1861 * the time zone data to produce the local time.
1866 * Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent. They are
1867 * used to figure out the week count for a specific date for a given locale. These
1868 * must be set when a Calendar is constructed. For example, in US locale,
1869 * firstDayOfWeek is SUNDAY; minimalDaysInFirstWeek is 1. They are used to figure
1870 * out the week count for a specific date for a given locale. These must be set when
1871 * a Calendar is constructed.
1873 UCalendarDaysOfWeek fFirstDayOfWeek
;
1874 uint8_t fMinimalDaysInFirstWeek
;
1877 * Sets firstDayOfWeek and minimalDaysInFirstWeek. Called at Calendar construction
1880 * @param desiredLocale The given locale.
1881 * @param type The calendar type identifier, e.g: gregorian, buddhist, etc.
1882 * @param success Indicates the status of setting the week count data from
1883 * the resource for the given locale. Returns U_ZERO_ERROR if
1884 * constructed successfully.
1886 void setWeekCountData(const Locale
& desiredLocale
, const char *type
, UErrorCode
& success
);
1889 * Recompute the time and update the status fields isTimeSet
1890 * and areFieldsSet. Callers should check isTimeSet and only
1891 * call this method if isTimeSet is false.
1893 * @param status Output param set to success/failure code on exit. If any value
1894 * previously set in the time field is invalid or restricted by
1895 * leniency, this will be set to an error status.
1897 void updateTime(UErrorCode
& status
);
1900 * The Gregorian year, as computed by computeGregorianFields() and
1901 * returned by getGregorianYear().
1903 int32_t fGregorianYear
;
1906 * The Gregorian month, as computed by computeGregorianFields() and
1907 * returned by getGregorianMonth().
1909 int32_t fGregorianMonth
;
1912 * The Gregorian day of the year, as computed by
1913 * computeGregorianFields() and returned by getGregorianDayOfYear().
1915 int32_t fGregorianDayOfYear
;
1918 * The Gregorian day of the month, as computed by
1919 * computeGregorianFields() and returned by getGregorianDayOfMonth().
1921 int32_t fGregorianDayOfMonth
;
1926 * Compute the Gregorian calendar year, month, and day of month from
1927 * the given Julian day. These values are not stored in fields, but in
1928 * member variables gregorianXxx. Also compute the DAY_OF_WEEK and
1931 void computeGregorianAndDOWFields(int32_t julianDay
, UErrorCode
&ec
);
1934 * Compute the Gregorian calendar year, month, and day of month from the
1935 * Julian day. These values are not stored in fields, but in member
1936 * variables gregorianXxx. They are used for time zone computations and by
1937 * subclasses that are Gregorian derivatives. Subclasses may call this
1938 * method to perform a Gregorian calendar millis->fields computation.
1939 * To perform a Gregorian calendar fields->millis computation, call
1940 * computeGregorianMonthStart().
1941 * @see #computeGregorianMonthStart
1943 void computeGregorianFields(int32_t julianDay
, UErrorCode
&ec
);
1946 * Compute the fields WEEK_OF_YEAR, YEAR_WOY, WEEK_OF_MONTH,
1947 * DAY_OF_WEEK_IN_MONTH, and DOW_LOCAL from EXTENDED_YEAR, YEAR,
1948 * DAY_OF_WEEK, and DAY_OF_YEAR. The latter fields are computed by the
1949 * subclass based on the calendar system.
1951 * <p>The YEAR_WOY field is computed simplistically. It is equal to YEAR
1952 * most of the time, but at the year boundary it may be adjusted to YEAR-1
1953 * or YEAR+1 to reflect the overlap of a week into an adjacent year. In
1954 * this case, a simple increment or decrement is performed on YEAR, even
1955 * though this may yield an invalid YEAR value. For instance, if the YEAR
1956 * is part of a calendar system with an N-year cycle field CYCLE, then
1957 * incrementing the YEAR may involve incrementing CYCLE and setting YEAR
1958 * back to 0 or 1. This is not handled by this code, and in fact cannot be
1959 * simply handled without having subclasses define an entire parallel set of
1960 * fields for fields larger than or equal to a year. This additional
1961 * complexity is not warranted, since the intention of the YEAR_WOY field is
1962 * to support ISO 8601 notation, so it will typically be used with a
1963 * proleptic Gregorian calendar, which has no field larger than a year.
1965 void computeWeekFields(UErrorCode
&ec
);
1969 * Ensure that each field is within its valid range by calling {@link
1970 * #validateField(int, int&)} on each field that has been set. This method
1971 * should only be called if this calendar is not lenient.
1973 * @see #validateField(int, int&)
1976 void validateFields(UErrorCode
&status
);
1979 * Validate a single field of this calendar. Subclasses should
1980 * override this method to validate any calendar-specific fields.
1981 * Generic fields can be handled by
1982 * <code>Calendar.validateField()</code>.
1983 * @see #validateField(int, int, int, int&)
1986 virtual void validateField(UCalendarDateFields field
, UErrorCode
&status
);
1989 * Validate a single field of this calendar given its minimum and
1990 * maximum allowed value. If the field is out of range,
1991 * <code>U_ILLEGAL_ARGUMENT_ERROR</code> will be set. Subclasses may
1992 * use this method in their implementation of {@link
1993 * #validateField(int, int&)}.
1996 void validateField(UCalendarDateFields field
, int32_t min
, int32_t max
, UErrorCode
& status
);
2000 * Convert a quasi Julian date to the day of the week. The Julian date used here is
2001 * not a true Julian date, since it is measured from midnight, not noon. Return
2002 * value is one-based.
2004 * @param julian The given Julian date number.
2005 * @return Day number from 1..7 (SUN..SAT).
2008 static uint8_t julianDayToDayOfWeek(double julian
);
2011 char validLocale
[ULOC_FULLNAME_CAPACITY
];
2012 char actualLocale
[ULOC_FULLNAME_CAPACITY
];
2015 #if !UCONFIG_NO_SERVICE
2017 * INTERNAL FOR 2.6 -- Registration.
2021 * Return a StringEnumeration over the locales available at the time of the call,
2022 * including registered locales.
2023 * @return a StringEnumeration over the locales available at the time of the call
2026 static StringEnumeration
* getAvailableLocales(void);
2029 * Register a new Calendar factory. The factory will be adopted.
2031 * @param toAdopt the factory instance to be adopted
2032 * @param status the in/out status code, no special meanings are assigned
2033 * @return a registry key that can be used to unregister this factory
2036 static URegistryKey
registerFactory(ICUServiceFactory
* toAdopt
, UErrorCode
& status
);
2039 * Unregister a previously-registered CalendarFactory using the key returned from the
2040 * register call. Key becomes invalid after a successful call and should not be used again.
2041 * The CalendarFactory corresponding to the key will be deleted.
2043 * @param key the registry key returned by a previous call to registerFactory
2044 * @param status the in/out status code, no special meanings are assigned
2045 * @return TRUE if the factory for the key was successfully unregistered
2048 static UBool
unregister(URegistryKey key
, UErrorCode
& status
);
2051 * Multiple Calendar Implementation
2054 friend class CalendarFactory
;
2057 * Multiple Calendar Implementation
2060 friend class CalendarService
;
2063 * Multiple Calendar Implementation
2066 friend class DefaultCalendarFactory
;
2067 #endif /* !UCONFIG_NO_SERVICE */
2071 * @return TRUE if this calendar has a default century (i.e. 03 -> 2003)
2073 virtual UBool
haveDefaultCentury() const = 0;
2077 * @return the start of the default century, as a UDate
2079 virtual UDate
defaultCenturyStart() const = 0;
2082 * @return the beginning year of the default century, as a year
2084 virtual int32_t defaultCenturyStartYear() const = 0;
2086 /** Get the locale for this calendar object. You can choose between valid and actual locale.
2087 * @param type type of the locale we're looking for (valid or actual)
2088 * @param status error code for the operation
2089 * @return the locale
2092 Locale
getLocale(ULocDataLocaleType type
, UErrorCode
&status
) const;
2094 /** Get the locale for this calendar object. You can choose between valid and actual locale.
2095 * @param type type of the locale we're looking for (valid or actual)
2096 * @param status error code for the operation
2097 * @return the locale
2100 const char* getLocaleID(ULocDataLocaleType type
, UErrorCode
&status
) const;
2104 // -------------------------------------
2107 Calendar::createInstance(TimeZone
* zone
, UErrorCode
& errorCode
)
2109 // since the Locale isn't specified, use the default locale
2110 return createInstance(zone
, Locale::getDefault(), errorCode
);
2113 // -------------------------------------
2116 Calendar::roll(UCalendarDateFields field
, UBool up
, UErrorCode
& status
)
2118 roll(field
, (int32_t)(up
? +1 : -1), status
);
2122 Calendar::roll(EDateFields field
, UBool up
, UErrorCode
& status
)
2124 roll((UCalendarDateFields
) field
, up
, status
);
2128 // -------------------------------------
2131 * Fast method for subclasses. The caller must maintain fUserSetDSTOffset and
2132 * fUserSetZoneOffset, as well as the isSet[] array.
2136 Calendar::internalSet(UCalendarDateFields field
, int32_t value
)
2138 fFields
[field
] = value
;
2139 fStamp
[field
] = kInternallySet
;
2140 fIsSet
[field
] = TRUE
; // Remove later
2144 Calendar::internalSet(EDateFields field
, int32_t value
)
2146 internalSet((UCalendarDateFields
) field
, value
);
2149 inline int32_t Calendar::weekNumber(int32_t dayOfPeriod
, int32_t dayOfWeek
)
2151 return weekNumber(dayOfPeriod
, dayOfPeriod
, dayOfWeek
);
2157 #endif /* #if !UCONFIG_NO_FORMATTING */