2 ********************************************************************************
3 * Copyright (C) 1997-2008, 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 #ifndef U_HIDE_DEPRECATED_API
176 ERA
, // Example: 0..1
177 YEAR
, // Example: 1..big number
178 MONTH
, // Example: 0..11
179 WEEK_OF_YEAR
, // Example: 1..53
180 WEEK_OF_MONTH
, // Example: 1..4
181 DATE
, // Example: 1..31
182 DAY_OF_YEAR
, // Example: 1..365
183 DAY_OF_WEEK
, // Example: 1..7
184 DAY_OF_WEEK_IN_MONTH
, // Example: 1..4, may be specified as -1
185 AM_PM
, // Example: 0..1
186 HOUR
, // Example: 0..11
187 HOUR_OF_DAY
, // Example: 0..23
188 MINUTE
, // Example: 0..59
189 SECOND
, // Example: 0..59
190 MILLISECOND
, // Example: 0..999
191 ZONE_OFFSET
, // Example: -12*U_MILLIS_PER_HOUR..12*U_MILLIS_PER_HOUR
192 DST_OFFSET
, // Example: 0 or U_MILLIS_PER_HOUR
193 YEAR_WOY
, // 'Y' Example: 1..big number - Year of Week of Year
194 DOW_LOCAL
, // 'e' Example: 1..7 - Day of Week / Localized
201 FIELD_COUNT
= UCAL_FIELD_COUNT
// See ucal.h for other fields.
202 #endif /* U_HIDE_DEPRECATED_API */
206 * Useful constant for days of week. Note: Calendar day-of-week is 1-based. Clients
207 * who create locale resources for the field of first-day-of-week should be aware of
208 * this. For instance, in US locale, first-day-of-week is set to 1, i.e., SUNDAY.
209 * @deprecated ICU 2.6. Use C enum UCalendarDaysOfWeek defined in ucal.h
212 #ifndef U_HIDE_DEPRECATED_API
220 #endif /* U_HIDE_DEPRECATED_API */
224 * Useful constants for month. Note: Calendar month is 0-based.
225 * @deprecated ICU 2.6. Use C enum UCalendarMonths defined in ucal.h
228 #ifndef U_HIDE_DEPRECATED_API
242 #endif /* U_HIDE_DEPRECATED_API */
246 * Useful constants for hour in 12-hour clock. Used in GregorianCalendar.
247 * @deprecated ICU 2.6. Use C enum UCalendarAMPMs defined in ucal.h
250 #ifndef U_HIDE_DEPRECATED_API
253 #endif /* U_HIDE_DEPRECATED_API */
263 * Create and return a polymorphic copy of this calendar.
265 * @return a polymorphic copy of this calendar.
268 virtual Calendar
* clone(void) const = 0;
271 * Creates a Calendar using the default timezone and locale. Clients are responsible
272 * for deleting the object returned.
274 * @param success Indicates the success/failure of Calendar creation. Filled in
275 * with U_ZERO_ERROR if created successfully, set to a failure result
276 * otherwise. U_MISSING_RESOURCE_ERROR will be returned if the resource data
277 * requests a calendar type which has not been installed.
278 * @return A Calendar if created successfully. NULL otherwise.
281 static Calendar
* U_EXPORT2
createInstance(UErrorCode
& success
);
284 * Creates a Calendar using the given timezone and the default locale.
285 * The Calendar takes ownership of zoneToAdopt; the
286 * client must not delete it.
288 * @param zoneToAdopt The given timezone to be adopted.
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(TimeZone
* zoneToAdopt
, UErrorCode
& success
);
298 * Creates a Calendar using the given timezone and the default locale. The TimeZone
299 * is _not_ adopted; the client is still responsible for deleting it.
301 * @param zone The timezone.
302 * @param success Indicates the success/failure of Calendar creation. Filled in
303 * with U_ZERO_ERROR if created successfully, set to a failure result
305 * @return A Calendar if created successfully. NULL otherwise.
308 static Calendar
* U_EXPORT2
createInstance(const TimeZone
& zone
, UErrorCode
& success
);
311 * Creates a Calendar using the default timezone and the given locale.
313 * @param aLocale The given locale.
314 * @param success Indicates the success/failure of Calendar creation. Filled in
315 * with U_ZERO_ERROR if created successfully, set to a failure result
317 * @return A Calendar if created successfully. NULL otherwise.
320 static Calendar
* U_EXPORT2
createInstance(const Locale
& aLocale
, UErrorCode
& success
);
323 * Creates a Calendar using the given timezone and given locale.
324 * The Calendar takes ownership of zoneToAdopt; the
325 * client must not delete it.
327 * @param zoneToAdopt The given timezone to be adopted.
328 * @param aLocale The given locale.
329 * @param success Indicates the success/failure of Calendar creation. Filled in
330 * with U_ZERO_ERROR if created successfully, set to a failure result
332 * @return A Calendar if created successfully. NULL otherwise.
335 static Calendar
* U_EXPORT2
createInstance(TimeZone
* zoneToAdopt
, const Locale
& aLocale
, UErrorCode
& success
);
338 * Gets a Calendar using the given timezone and given locale. The TimeZone
339 * is _not_ adopted; the client is still responsible for deleting it.
341 * @param zoneToAdopt The given timezone to be adopted.
342 * @param aLocale The given locale.
343 * @param success Indicates the success/failure of Calendar creation. Filled in
344 * with U_ZERO_ERROR if created successfully, set to a failure result
346 * @return A Calendar if created successfully. NULL otherwise.
349 static Calendar
* U_EXPORT2
createInstance(const TimeZone
& zoneToAdopt
, const Locale
& aLocale
, UErrorCode
& success
);
352 * Returns a list of the locales for which Calendars are installed.
354 * @param count Number of locales returned.
355 * @return An array of Locale objects representing the set of locales for which
356 * Calendars are installed. The system retains ownership of this list;
357 * the caller must NOT delete it. Does not include user-registered Calendars.
360 static const Locale
* U_EXPORT2
getAvailableLocales(int32_t& count
);
363 * Returns the current UTC (GMT) time measured in milliseconds since 0:00:00 on 1/1/70
364 * (derived from the system time).
366 * @return The current UTC time in milliseconds.
369 static UDate U_EXPORT2
getNow(void);
372 * Gets this Calendar's time as milliseconds. May involve recalculation of time due
373 * to previous calls to set time field values. The time specified is non-local UTC
374 * (GMT) time. Although this method is const, this object may actually be changed
375 * (semantically const).
377 * @param status Output param set to success/failure code on exit. If any value
378 * previously set in the time field is invalid or restricted by
379 * leniency, this will be set to an error status.
380 * @return The current time in UTC (GMT) time, or zero if the operation
384 inline UDate
getTime(UErrorCode
& status
) const { return getTimeInMillis(status
); }
387 * Sets this Calendar's current time with the given UDate. The time specified should
388 * be in non-local UTC (GMT) time.
390 * @param date The given UDate in UTC (GMT) time.
391 * @param status Output param set to success/failure code on exit. If any value
392 * set in the time field is invalid or restricted by
393 * leniency, this will be set to an error status.
396 inline void setTime(UDate date
, UErrorCode
& status
) { setTimeInMillis(date
, status
); }
399 * Compares the equality of two Calendar objects. Objects of different subclasses
400 * are considered unequal. This comparison is very exacting; two Calendar objects
401 * must be in exactly the same state to be considered equal. To compare based on the
402 * represented time, use equals() instead.
404 * @param that The Calendar object to be compared with.
405 * @return True if the given Calendar is the same as this Calendar; false
409 virtual UBool
operator==(const Calendar
& that
) const;
412 * Compares the inequality of two Calendar objects.
414 * @param that The Calendar object to be compared with.
415 * @return True if the given Calendar is not the same as this Calendar; false
419 UBool
operator!=(const Calendar
& that
) const {return !operator==(that
);}
422 * Returns TRUE if the given Calendar object is equivalent to this
423 * one. An equivalent Calendar will behave exactly as this one
424 * does, but it may be set to a different time. By contrast, for
425 * the operator==() method to return TRUE, the other Calendar must
426 * be set to the same time.
428 * @param other the Calendar to be compared with this Calendar
431 virtual UBool
isEquivalentTo(const Calendar
& other
) const;
434 * Compares the Calendar time, whereas Calendar::operator== compares the equality of
437 * @param when The Calendar to be compared with this Calendar. Although this is a
438 * const parameter, the object may be modified physically
439 * (semantically const).
440 * @param status Output param set to success/failure code on exit. If any value
441 * previously set in the time field is invalid or restricted by
442 * leniency, this will be set to an error status.
443 * @return True if the current time of this Calendar is equal to the time of
444 * Calendar when; false otherwise.
447 UBool
equals(const Calendar
& when
, UErrorCode
& status
) const;
450 * Returns true if this Calendar's current time is before "when"'s current time.
452 * @param when The Calendar to be compared with this Calendar. Although this is a
453 * const parameter, the object may be modified physically
454 * (semantically const).
455 * @param status Output param set to success/failure code on exit. If any value
456 * previously set in the time field is invalid or restricted by
457 * leniency, this will be set to an error status.
458 * @return True if the current time of this Calendar is before the time of
459 * Calendar when; false otherwise.
462 UBool
before(const Calendar
& when
, UErrorCode
& status
) const;
465 * Returns true if this Calendar's current time is after "when"'s current time.
467 * @param when The Calendar to be compared with this Calendar. Although this is a
468 * const parameter, the object may be modified physically
469 * (semantically const).
470 * @param status Output param set to success/failure code on exit. If any value
471 * previously set in the time field is invalid or restricted by
472 * leniency, this will be set to an error status.
473 * @return True if the current time of this Calendar is after the time of
474 * Calendar when; false otherwise.
477 UBool
after(const Calendar
& when
, UErrorCode
& status
) const;
480 * UDate Arithmetic function. Adds the specified (signed) amount of time to the given
481 * time field, based on the calendar's rules. For example, to subtract 5 days from
482 * the current time of the calendar, call add(Calendar::DATE, -5). When adding on
483 * the month or Calendar::MONTH field, other fields like date might conflict and
484 * need to be changed. For instance, adding 1 month on the date 01/31/96 will result
487 * @param field Specifies which date field to modify.
488 * @param amount The amount of time to be added to the field, in the natural unit
489 * for that field (e.g., days for the day fields, hours for the hour
491 * @param status Output param set to success/failure code on exit. If any value
492 * previously set in the time field is invalid or restricted by
493 * leniency, this will be set to an error status.
494 * @deprecated ICU 2.6. use add(UCalendarDateFields field, int32_t amount, UErrorCode& status) instead.
496 virtual void add(EDateFields field
, int32_t amount
, UErrorCode
& status
);
499 * UDate Arithmetic function. Adds the specified (signed) amount of time to the given
500 * time field, based on the calendar's rules. For example, to subtract 5 days from
501 * the current time of the calendar, call add(Calendar::DATE, -5). When adding on
502 * the month or Calendar::MONTH field, other fields like date might conflict and
503 * need to be changed. For instance, adding 1 month on the date 01/31/96 will result
506 * @param field Specifies which date field to modify.
507 * @param amount The amount of time to be added to the field, in the natural unit
508 * for that field (e.g., days for the day fields, hours for the hour
510 * @param status Output param set to success/failure code on exit. If any value
511 * previously set in the time field is invalid or restricted by
512 * leniency, this will be set to an error status.
515 virtual void add(UCalendarDateFields field
, int32_t amount
, UErrorCode
& status
);
518 * Time Field Rolling function. Rolls (up/down) a single unit of time on the given
519 * time field. For example, to roll the current date up by one day, call
520 * roll(Calendar::DATE, true). When rolling on the year or Calendar::YEAR field, it
521 * will roll the year value in the range between getMinimum(Calendar::YEAR) and the
522 * value returned by getMaximum(Calendar::YEAR). When rolling on the month or
523 * Calendar::MONTH field, other fields like date might conflict and, need to be
524 * changed. For instance, rolling the month up on the date 01/31/96 will result in
525 * 02/29/96. Rolling up always means rolling forward in time; e.g., rolling the year
526 * up on "100 BC" will result in "99 BC", for Gregorian calendar. When rolling on the
527 * hour-in-day or Calendar::HOUR_OF_DAY field, it will roll the hour value in the range
528 * between 0 and 23, which is zero-based.
530 * NOTE: Do not use this method -- use roll(EDateFields, int, UErrorCode&) instead.
532 * @param field The time field.
533 * @param up Indicates if the value of the specified time field is to be rolled
534 * up or rolled down. Use true if rolling up, false otherwise.
535 * @param status Output param set to success/failure code on exit. If any value
536 * previously set in the time field is invalid or restricted by
537 * leniency, this will be set to an error status.
538 * @deprecated ICU 2.6. Use roll(UCalendarDateFields field, UBool up, UErrorCode& status) instead.
540 inline void roll(EDateFields field
, UBool up
, UErrorCode
& status
);
543 * Time Field Rolling function. Rolls (up/down) a single unit of time on the given
544 * time field. For example, to roll the current date up by one day, call
545 * roll(Calendar::DATE, true). When rolling on the year or Calendar::YEAR field, it
546 * will roll the year value in the range between getMinimum(Calendar::YEAR) and the
547 * value returned by getMaximum(Calendar::YEAR). When rolling on the month or
548 * Calendar::MONTH field, other fields like date might conflict and, need to be
549 * changed. For instance, rolling the month up on the date 01/31/96 will result in
550 * 02/29/96. Rolling up always means rolling forward in time; e.g., rolling the year
551 * up on "100 BC" will result in "99 BC", for Gregorian calendar. When rolling on the
552 * hour-in-day or Calendar::HOUR_OF_DAY field, it will roll the hour value in the range
553 * between 0 and 23, which is zero-based.
555 * NOTE: Do not use this method -- use roll(UCalendarDateFields, int, UErrorCode&) instead.
557 * @param field The time field.
558 * @param up Indicates if the value of the specified time field is to be rolled
559 * up or rolled down. Use true if rolling up, false otherwise.
560 * @param status Output param set to success/failure code on exit. If any value
561 * previously set in the time field is invalid or restricted by
562 * leniency, this will be set to an error status.
565 inline void roll(UCalendarDateFields field
, UBool up
, UErrorCode
& status
);
568 * Time Field Rolling function. Rolls by the given amount on the given
569 * time field. For example, to roll the current date up by one day, call
570 * roll(Calendar::DATE, +1, status). When rolling on the month or
571 * Calendar::MONTH field, other fields like date might conflict and, need to be
572 * changed. For instance, rolling the month up on the date 01/31/96 will result in
573 * 02/29/96. Rolling by a positive value always means rolling forward in time;
574 * e.g., rolling the year by +1 on "100 BC" will result in "99 BC", for Gregorian
575 * calendar. When rolling on the hour-in-day or Calendar::HOUR_OF_DAY field, it will
576 * roll the hour value in the range between 0 and 23, which is zero-based.
578 * The only difference between roll() and add() is that roll() does not change
579 * the value of more significant fields when it reaches the minimum or maximum
580 * of its range, whereas add() does.
582 * @param field The time field.
583 * @param amount Indicates amount to roll.
584 * @param status Output param set to success/failure code on exit. If any value
585 * previously set in the time field is invalid, this will be set to
587 * @deprecated ICU 2.6. Use roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) instead.
589 virtual void roll(EDateFields field
, int32_t amount
, UErrorCode
& status
);
592 * Time Field Rolling function. Rolls by the given amount on the given
593 * time field. For example, to roll the current date up by one day, call
594 * roll(Calendar::DATE, +1, status). When rolling on the month or
595 * Calendar::MONTH field, other fields like date might conflict and, need to be
596 * changed. For instance, rolling the month up on the date 01/31/96 will result in
597 * 02/29/96. Rolling by a positive value always means rolling forward in time;
598 * e.g., rolling the year by +1 on "100 BC" will result in "99 BC", for Gregorian
599 * calendar. When rolling on the hour-in-day or Calendar::HOUR_OF_DAY field, it will
600 * roll the hour value in the range between 0 and 23, which is zero-based.
602 * The only difference between roll() and add() is that roll() does not change
603 * the value of more significant fields when it reaches the minimum or maximum
604 * of its range, whereas add() does.
606 * @param field The time field.
607 * @param amount Indicates amount to roll.
608 * @param status Output param set to success/failure code on exit. If any value
609 * previously set in the time field is invalid, this will be set to
613 virtual void roll(UCalendarDateFields field
, int32_t amount
, UErrorCode
& status
);
616 * Return the difference between the given time and the time this
617 * calendar object is set to. If this calendar is set
618 * <em>before</em> the given time, the returned value will be
619 * positive. If this calendar is set <em>after</em> the given
620 * time, the returned value will be negative. The
621 * <code>field</code> parameter specifies the units of the return
622 * value. For example, if <code>fieldDifference(when,
623 * Calendar::MONTH)</code> returns 3, then this calendar is set to
624 * 3 months before <code>when</code>, and possibly some addition
625 * time less than one month.
627 * <p>As a side effect of this call, this calendar is advanced
628 * toward <code>when</code> by the given amount. That is, calling
629 * this method has the side effect of calling <code>add(field,
630 * n)</code>, where <code>n</code> is the return value.
632 * <p>Usage: To use this method, call it first with the largest
633 * field of interest, then with progressively smaller fields. For
637 * int y = cal->fieldDifference(when, Calendar::YEAR, err);
638 * int m = cal->fieldDifference(when, Calendar::MONTH, err);
639 * int d = cal->fieldDifference(when, Calendar::DATE, err);</pre>
641 * computes the difference between <code>cal</code> and
642 * <code>when</code> in years, months, and days.
644 * <p>Note: <code>fieldDifference()</code> is
645 * <em>asymmetrical</em>. That is, in the following code:
648 * cal->setTime(date1, err);
649 * int m1 = cal->fieldDifference(date2, Calendar::MONTH, err);
650 * int d1 = cal->fieldDifference(date2, Calendar::DATE, err);
651 * cal->setTime(date2, err);
652 * int m2 = cal->fieldDifference(date1, Calendar::MONTH, err);
653 * int d2 = cal->fieldDifference(date1, Calendar::DATE, err);</pre>
655 * one might expect that <code>m1 == -m2 && d1 == -d2</code>.
656 * However, this is not generally the case, because of
657 * irregularities in the underlying calendar system (e.g., the
658 * Gregorian calendar has a varying number of days per month).
660 * @param when the date to compare this calendar's time to
661 * @param field the field in which to compute the result
662 * @param status Output param set to success/failure code on exit. If any value
663 * previously set in the time field is invalid, this will be set to
665 * @return the difference, either positive or negative, between
666 * this calendar's time and <code>when</code>, in terms of
667 * <code>field</code>.
668 * @deprecated ICU 2.6. Use fieldDifference(UDate when, UCalendarDateFields field, UErrorCode& status).
670 virtual int32_t fieldDifference(UDate when
, EDateFields field
, UErrorCode
& status
);
673 * Return the difference between the given time and the time this
674 * calendar object is set to. If this calendar is set
675 * <em>before</em> the given time, the returned value will be
676 * positive. If this calendar is set <em>after</em> the given
677 * time, the returned value will be negative. The
678 * <code>field</code> parameter specifies the units of the return
679 * value. For example, if <code>fieldDifference(when,
680 * Calendar::MONTH)</code> returns 3, then this calendar is set to
681 * 3 months before <code>when</code>, and possibly some addition
682 * time less than one month.
684 * <p>As a side effect of this call, this calendar is advanced
685 * toward <code>when</code> by the given amount. That is, calling
686 * this method has the side effect of calling <code>add(field,
687 * n)</code>, where <code>n</code> is the return value.
689 * <p>Usage: To use this method, call it first with the largest
690 * field of interest, then with progressively smaller fields. For
694 * int y = cal->fieldDifference(when, Calendar::YEAR, err);
695 * int m = cal->fieldDifference(when, Calendar::MONTH, err);
696 * int d = cal->fieldDifference(when, Calendar::DATE, err);</pre>
698 * computes the difference between <code>cal</code> and
699 * <code>when</code> in years, months, and days.
701 * <p>Note: <code>fieldDifference()</code> is
702 * <em>asymmetrical</em>. That is, in the following code:
705 * cal->setTime(date1, err);
706 * int m1 = cal->fieldDifference(date2, Calendar::MONTH, err);
707 * int d1 = cal->fieldDifference(date2, Calendar::DATE, err);
708 * cal->setTime(date2, err);
709 * int m2 = cal->fieldDifference(date1, Calendar::MONTH, err);
710 * int d2 = cal->fieldDifference(date1, Calendar::DATE, err);</pre>
712 * one might expect that <code>m1 == -m2 && d1 == -d2</code>.
713 * However, this is not generally the case, because of
714 * irregularities in the underlying calendar system (e.g., the
715 * Gregorian calendar has a varying number of days per month).
717 * @param when the date to compare this calendar's time to
718 * @param field the field in which to compute the result
719 * @param status Output param set to success/failure code on exit. If any value
720 * previously set in the time field is invalid, this will be set to
722 * @return the difference, either positive or negative, between
723 * this calendar's time and <code>when</code>, in terms of
724 * <code>field</code>.
727 virtual int32_t fieldDifference(UDate when
, UCalendarDateFields field
, UErrorCode
& status
);
730 * Sets the calendar's time zone to be the one passed in. The Calendar takes ownership
731 * of the TimeZone; the caller is no longer responsible for deleting it. If the
732 * given time zone is NULL, this function has no effect.
734 * @param value The given time zone.
737 void adoptTimeZone(TimeZone
* value
);
740 * Sets the calendar's time zone to be the same as the one passed in. The TimeZone
741 * passed in is _not_ adopted; the client is still responsible for deleting it.
743 * @param zone The given time zone.
746 void setTimeZone(const TimeZone
& zone
);
749 * Returns a reference to the time zone owned by this calendar. The returned reference
750 * is only valid until clients make another call to adoptTimeZone or setTimeZone,
751 * or this Calendar is destroyed.
753 * @return The time zone object associated with this calendar.
756 const TimeZone
& getTimeZone(void) const;
759 * Returns the time zone owned by this calendar. The caller owns the returned object
760 * and must delete it when done. After this call, the new time zone associated
761 * with this Calendar is the default TimeZone as returned by TimeZone::createDefault().
763 * @return The time zone object which was associated with this calendar.
766 TimeZone
* orphanTimeZone(void);
769 * Queries if the current date for this Calendar is in Daylight Savings Time.
771 * @param status Fill-in parameter which receives the status of this operation.
772 * @return True if the current date for this Calendar is in Daylight Savings Time,
776 virtual UBool
inDaylightTime(UErrorCode
& status
) const = 0;
779 * Specifies whether or not date/time interpretation is to be lenient. With lenient
780 * interpretation, a date such as "February 942, 1996" will be treated as being
781 * equivalent to the 941st day after February 1, 1996. With strict interpretation,
782 * such dates will cause an error when computing time from the time field values
783 * representing the dates.
785 * @param lenient True specifies date/time interpretation to be lenient.
787 * @see DateFormat#setLenient
790 void setLenient(UBool lenient
);
793 * Tells whether date/time interpretation is to be lenient.
795 * @return True tells that date/time interpretation is to be lenient.
798 UBool
isLenient(void) const;
801 * Sets what the first day of the week is; e.g., Sunday in US, Monday in France.
803 * @param value The given first day of the week.
804 * @deprecated ICU 2.6. Use setFirstDayOfWeek(UCalendarDaysOfWeek value) instead.
806 void setFirstDayOfWeek(EDaysOfWeek value
);
809 * Sets what the first day of the week is; e.g., Sunday in US, Monday in France.
811 * @param value The given first day of the week.
814 void setFirstDayOfWeek(UCalendarDaysOfWeek value
);
817 * Gets what the first day of the week is; e.g., Sunday in US, Monday in France.
819 * @return The first day of the week.
820 * @deprecated ICU 2.6 use the overload with error code
822 EDaysOfWeek
getFirstDayOfWeek(void) const;
825 * Gets what the first day of the week is; e.g., Sunday in US, Monday in France.
827 * @param status error code
828 * @return The first day of the week.
831 UCalendarDaysOfWeek
getFirstDayOfWeek(UErrorCode
&status
) const;
834 * Sets what the minimal days required in the first week of the year are; For
835 * example, if the first week is defined as one that contains the first day of the
836 * first month of a year, call the method with value 1. If it must be a full week,
839 * @param value The given minimal days required in the first week of the year.
842 void setMinimalDaysInFirstWeek(uint8_t value
);
845 * Gets what the minimal days required in the first week of the year are; e.g., if
846 * the first week is defined as one that contains the first day of the first month
847 * of a year, getMinimalDaysInFirstWeek returns 1. If the minimal days required must
848 * be a full week, getMinimalDaysInFirstWeek returns 7.
850 * @return The minimal days required in the first week of the year.
853 uint8_t getMinimalDaysInFirstWeek(void) const;
856 * Gets the minimum value for the given time field. e.g., for Gregorian
859 * @param field The given time field.
860 * @return The minimum value for the given time field.
861 * @deprecated ICU 2.6. Use getMinimum(UCalendarDateFields field) instead.
863 virtual int32_t getMinimum(EDateFields field
) const;
866 * Gets the minimum value for the given time field. e.g., for Gregorian
869 * @param field The given time field.
870 * @return The minimum value for the given time field.
873 virtual int32_t getMinimum(UCalendarDateFields field
) const;
876 * Gets the maximum value for the given time field. e.g. for Gregorian DAY_OF_MONTH,
879 * @param field The given time field.
880 * @return The maximum value for the given time field.
881 * @deprecated ICU 2.6. Use getMaximum(UCalendarDateFields field) instead.
883 virtual int32_t getMaximum(EDateFields field
) const;
886 * Gets the maximum value for the given time field. e.g. for Gregorian DAY_OF_MONTH,
889 * @param field The given time field.
890 * @return The maximum value for the given time field.
893 virtual int32_t getMaximum(UCalendarDateFields field
) const;
896 * Gets the highest minimum value for the given field if varies. Otherwise same as
897 * getMinimum(). For Gregorian, no difference.
899 * @param field The given time field.
900 * @return The highest minimum value for the given time field.
901 * @deprecated ICU 2.6. Use getGreatestMinimum(UCalendarDateFields field) instead.
903 virtual int32_t getGreatestMinimum(EDateFields field
) const;
906 * Gets the highest minimum value for the given field if varies. Otherwise same as
907 * getMinimum(). For Gregorian, no difference.
909 * @param field The given time field.
910 * @return The highest minimum value for the given time field.
913 virtual int32_t getGreatestMinimum(UCalendarDateFields field
) const;
916 * Gets the lowest maximum value for the given field if varies. Otherwise same as
917 * getMaximum(). e.g., for Gregorian DAY_OF_MONTH, 28.
919 * @param field The given time field.
920 * @return The lowest maximum value for the given time field.
921 * @deprecated ICU 2.6. Use getLeastMaximum(UCalendarDateFields field) instead.
923 virtual int32_t getLeastMaximum(EDateFields field
) const;
926 * Gets the lowest maximum value for the given field if varies. Otherwise same as
927 * getMaximum(). e.g., for Gregorian DAY_OF_MONTH, 28.
929 * @param field The given time field.
930 * @return The lowest maximum value for the given time field.
933 virtual int32_t getLeastMaximum(UCalendarDateFields field
) const;
936 * Return the minimum value that this field could have, given the current date.
937 * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().
939 * The version of this function on Calendar uses an iterative algorithm to determine the
940 * actual minimum value for the field. There is almost always a more efficient way to
941 * accomplish this (in most cases, you can simply return getMinimum()). GregorianCalendar
942 * overrides this function with a more efficient implementation.
944 * @param field the field to determine the minimum of
945 * @param status Fill-in parameter which receives the status of this operation.
946 * @return the minimum of the given field for the current date of this Calendar
947 * @deprecated ICU 2.6. Use getActualMinimum(UCalendarDateFields field, UErrorCode& status) instead.
949 int32_t getActualMinimum(EDateFields field
, UErrorCode
& status
) const;
952 * Return the minimum value that this field could have, given the current date.
953 * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().
955 * The version of this function on Calendar uses an iterative algorithm to determine the
956 * actual minimum value for the field. There is almost always a more efficient way to
957 * accomplish this (in most cases, you can simply return getMinimum()). GregorianCalendar
958 * overrides this function with a more efficient implementation.
960 * @param field the field to determine the minimum of
961 * @param status Fill-in parameter which receives the status of this operation.
962 * @return the minimum of the given field for the current date of this Calendar
965 virtual int32_t getActualMinimum(UCalendarDateFields field
, UErrorCode
& status
) const;
968 * Return the maximum value that this field could have, given the current date.
969 * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual
970 * maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar,
971 * for some years the actual maximum for MONTH is 12, and for others 13.
973 * The version of this function on Calendar uses an iterative algorithm to determine the
974 * actual maximum value for the field. There is almost always a more efficient way to
975 * accomplish this (in most cases, you can simply return getMaximum()). GregorianCalendar
976 * overrides this function with a more efficient implementation.
978 * @param field the field to determine the maximum of
979 * @param status Fill-in parameter which receives the status of this operation.
980 * @return the maximum of the given field for the current date of this Calendar
981 * @deprecated ICU 2.6. Use getActualMaximum(UCalendarDateFields field, UErrorCode& status) instead.
983 int32_t getActualMaximum(EDateFields field
, UErrorCode
& status
) const;
986 * Return the maximum value that this field could have, given the current date.
987 * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual
988 * maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar,
989 * for some years the actual maximum for MONTH is 12, and for others 13.
991 * The version of this function on Calendar uses an iterative algorithm to determine the
992 * actual maximum value for the field. There is almost always a more efficient way to
993 * accomplish this (in most cases, you can simply return getMaximum()). GregorianCalendar
994 * overrides this function with a more efficient implementation.
996 * @param field the field to determine the maximum of
997 * @param status Fill-in parameter which receives the status of this operation.
998 * @return the maximum of the given field for the current date of this Calendar
1001 virtual int32_t getActualMaximum(UCalendarDateFields field
, UErrorCode
& status
) const;
1004 * Gets the value for a given time field. Recalculate the current time field values
1005 * if the time value has been changed by a call to setTime(). Return zero for unset
1006 * fields if any fields have been explicitly set by a call to set(). To force a
1007 * recomputation of all fields regardless of the previous state, call complete().
1008 * This method is semantically const, but may alter the object in memory.
1010 * @param field The given time field.
1011 * @param status Fill-in parameter which receives the status of the operation.
1012 * @return The value for the given time field, or zero if the field is unset,
1013 * and set() has been called for any other field.
1014 * @deprecated ICU 2.6. Use get(UCalendarDateFields field, UErrorCode& status) instead.
1016 int32_t get(EDateFields field
, UErrorCode
& status
) const;
1019 * Gets the value for a given time field. Recalculate the current time field values
1020 * if the time value has been changed by a call to setTime(). Return zero for unset
1021 * fields if any fields have been explicitly set by a call to set(). To force a
1022 * recomputation of all fields regardless of the previous state, call complete().
1023 * This method is semantically const, but may alter the object in memory.
1025 * @param field The given time field.
1026 * @param status Fill-in parameter which receives the status of the operation.
1027 * @return The value for the given time field, or zero if the field is unset,
1028 * and set() has been called for any other field.
1031 int32_t get(UCalendarDateFields field
, UErrorCode
& status
) const;
1034 * Determines if the given time field has a value set. This can affect in the
1035 * resolving of time in Calendar. Unset fields have a value of zero, by definition.
1037 * @param field The given time field.
1038 * @return True if the given time field has a value set; false otherwise.
1039 * @deprecated ICU 2.6. Use isSet(UCalendarDateFields field) instead.
1041 UBool
isSet(EDateFields field
) const;
1044 * Determines if the given time field has a value set. This can affect in the
1045 * resolving of time in Calendar. Unset fields have a value of zero, by definition.
1047 * @param field The given time field.
1048 * @return True if the given time field has a value set; false otherwise.
1051 UBool
isSet(UCalendarDateFields field
) const;
1054 * Sets the given time field with the given value.
1056 * @param field The given time field.
1057 * @param value The value to be set for the given time field.
1058 * @deprecated ICU 2.6. Use set(UCalendarDateFields field, int32_t value) instead.
1060 void set(EDateFields field
, int32_t value
);
1063 * Sets the given time field with the given value.
1065 * @param field The given time field.
1066 * @param value The value to be set for the given time field.
1069 void set(UCalendarDateFields field
, int32_t value
);
1072 * Sets the values for the fields YEAR, MONTH, and DATE. Other field values are
1073 * retained; call clear() first if this is not desired.
1075 * @param year The value used to set the YEAR time field.
1076 * @param month The value used to set the MONTH time field. Month value is 0-based.
1077 * e.g., 0 for January.
1078 * @param date The value used to set the DATE time field.
1081 void set(int32_t year
, int32_t month
, int32_t date
);
1084 * Sets the values for the fields YEAR, MONTH, DATE, HOUR_OF_DAY, and MINUTE. Other
1085 * field values are retained; call clear() first if this is not desired.
1087 * @param year The value used to set the YEAR time field.
1088 * @param month The value used to set the MONTH time field. Month value is
1089 * 0-based. E.g., 0 for January.
1090 * @param date The value used to set the DATE time field.
1091 * @param hour The value used to set the HOUR_OF_DAY time field.
1092 * @param minute The value used to set the MINUTE time field.
1095 void set(int32_t year
, int32_t month
, int32_t date
, int32_t hour
, int32_t minute
);
1098 * Sets the values for the fields YEAR, MONTH, DATE, HOUR_OF_DAY, MINUTE, and SECOND.
1099 * Other field values are retained; call clear() first if this is not desired.
1101 * @param year The value used to set the YEAR time field.
1102 * @param month The value used to set the MONTH time field. Month value is
1103 * 0-based. E.g., 0 for January.
1104 * @param date The value used to set the DATE time field.
1105 * @param hour The value used to set the HOUR_OF_DAY time field.
1106 * @param minute The value used to set the MINUTE time field.
1107 * @param second The value used to set the SECOND time field.
1110 void set(int32_t year
, int32_t month
, int32_t date
, int32_t hour
, int32_t minute
, int32_t second
);
1113 * Clears the values of all the time fields, making them both unset and assigning
1114 * them a value of zero. The field values will be determined during the next
1115 * resolving of time into time fields.
1121 * Clears the value in the given time field, both making it unset and assigning it a
1122 * value of zero. This field value will be determined during the next resolving of
1123 * time into time fields.
1125 * @param field The time field to be cleared.
1126 * @deprecated ICU 2.6. Use clear(UCalendarDateFields field) instead.
1128 void clear(EDateFields field
);
1131 * Clears the value in the given time field, both making it unset and assigning it a
1132 * value of zero. This field value will be determined during the next resolving of
1133 * time into time fields.
1135 * @param field The time field to be cleared.
1138 void clear(UCalendarDateFields field
);
1141 * Returns a unique class ID POLYMORPHICALLY. Pure virtual method. This method is to
1142 * implement a simple version of RTTI, since not all C++ compilers support genuine
1143 * RTTI. Polymorphic operator==() and clone() methods call this method.
1145 * Concrete subclasses of Calendar must implement getDynamicClassID() and also a
1146 * static method and data member:
1148 * static UClassID getStaticClassID() { return (UClassID)&fgClassID; }
1149 * static char fgClassID;
1151 * @return The class ID for this object. All objects of a given class have the
1152 * same class ID. Objects of other classes have different class IDs.
1155 virtual UClassID
getDynamicClassID(void) const = 0;
1158 * Returns the resource key string used for this calendar type.
1159 * For example, prepending "Eras_" to this string could return "Eras_japanese"
1160 * or "Eras_gregorian".
1162 * @returns static string, for example, "gregorian" or "japanese"
1165 virtual const char * getType() const = 0;
1170 * Constructs a Calendar with the default time zone as returned by
1171 * TimeZone::createInstance(), and the default locale.
1173 * @param success Indicates the status of Calendar object construction. Returns
1174 * U_ZERO_ERROR if constructed successfully.
1177 Calendar(UErrorCode
& success
);
1182 * @param source Calendar object to be copied from
1185 Calendar(const Calendar
& source
);
1188 * Default assignment operator
1190 * @param right Calendar object to be copied
1193 Calendar
& operator=(const Calendar
& right
);
1196 * Constructs a Calendar with the given time zone and locale. Clients are no longer
1197 * responsible for deleting the given time zone object after it's adopted.
1199 * @param zone The given time zone.
1200 * @param aLocale The given locale.
1201 * @param success Indicates the status of Calendar object construction. Returns
1202 * U_ZERO_ERROR if constructed successfully.
1205 Calendar(TimeZone
* zone
, const Locale
& aLocale
, UErrorCode
& success
);
1208 * Constructs a Calendar with the given time zone and locale.
1210 * @param zone The given time zone.
1211 * @param aLocale The given locale.
1212 * @param success Indicates the status of Calendar object construction. Returns
1213 * U_ZERO_ERROR if constructed successfully.
1216 Calendar(const TimeZone
& zone
, const Locale
& aLocale
, UErrorCode
& success
);
1219 * Converts Calendar's time field values to GMT as milliseconds.
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 computeTime(UErrorCode
& status
);
1229 * Converts GMT as milliseconds to time field values. This allows you to sync up the
1230 * time field values with a new time that is set for the calendar. This method
1231 * does NOT recompute the time first; to recompute the time, then the fields, use
1232 * the method complete().
1234 * @param status Output param set to success/failure code on exit. If any value
1235 * previously set in the time field is invalid or restricted by
1236 * leniency, this will be set to an error status.
1239 virtual void computeFields(UErrorCode
& status
);
1242 * Gets this Calendar's current time as a long.
1244 * @param status Output param set to success/failure code on exit. If any value
1245 * previously set in the time field is invalid or restricted by
1246 * leniency, this will be set to an error status.
1247 * @return the current time as UTC milliseconds from the epoch.
1250 double getTimeInMillis(UErrorCode
& status
) const;
1253 * Sets this Calendar's current time from the given long value.
1254 * @param millis the new time in UTC milliseconds from the epoch.
1255 * @param status Output param set to success/failure code on exit. If any value
1256 * previously set in the time field is invalid or restricted by
1257 * leniency, this will be set to an error status.
1260 void setTimeInMillis( double millis
, UErrorCode
& status
);
1263 * Recomputes the current time from currently set fields, and then fills in any
1264 * unset fields in the time field list.
1266 * @param status Output param set to success/failure code on exit. If any value
1267 * previously set in the time field is invalid or restricted by
1268 * leniency, this will be set to an error status.
1271 void complete(UErrorCode
& status
);
1274 * Gets the value for a given time field. Subclasses can use this function to get
1275 * field values without forcing recomputation of time.
1277 * @param field The given time field.
1278 * @return The value for the given time field.
1279 * @deprecated ICU 2.6. Use internalGet(UCalendarDateFields field) instead.
1281 inline int32_t internalGet(EDateFields field
) const {return fFields
[field
];}
1284 * Gets the value for a given time field. Subclasses can use this function to get
1285 * field values without forcing recomputation of time. If the field's stamp is UNSET,
1286 * the defaultValue is used.
1288 * @param field The given time field.
1289 * @param defaultValue a default value used if the field is unset.
1290 * @return The value for the given time field.
1293 inline int32_t internalGet(UCalendarDateFields field
, int32_t defaultValue
) const {return fStamp
[field
]>kUnset
? fFields
[field
] : defaultValue
;}
1296 * Gets the value for a given time field. Subclasses can use this function to get
1297 * field values without forcing recomputation of time.
1299 * @param field The given time field.
1300 * @return The value for the given time field.
1303 inline int32_t internalGet(UCalendarDateFields field
) const {return fFields
[field
];}
1306 * Sets the value for a given time field. This is a fast internal method for
1307 * subclasses. It does not affect the areFieldsInSync, isTimeSet, or areAllFieldsSet
1310 * @param field The given time field.
1311 * @param value The value for the given time field.
1312 * @deprecated ICU 2.6. Use internalSet(UCalendarDateFields field, int32_t value) instead.
1314 void internalSet(EDateFields field
, int32_t value
);
1317 * Sets the value for a given time field. This is a fast internal method for
1318 * subclasses. It does not affect the areFieldsInSync, isTimeSet, or areAllFieldsSet
1321 * @param field The given time field.
1322 * @param value The value for the given time field.
1325 inline void internalSet(UCalendarDateFields field
, int32_t value
);
1328 * Prepare this calendar for computing the actual minimum or maximum.
1329 * This method modifies this calendar's fields; it is called on a
1330 * temporary calendar.
1333 virtual void prepareGetActual(UCalendarDateFields field
, UBool isMinimum
, UErrorCode
&status
);
1336 * Limit enums. Not in sync with UCalendarLimitType (refers to internal fields).
1340 UCAL_LIMIT_MINIMUM
= 0,
1341 UCAL_LIMIT_GREATEST_MINIMUM
,
1342 UCAL_LIMIT_LEAST_MAXIMUM
,
1348 * Subclass API for defining limits of different types.
1349 * Subclasses must implement this method to return limits for the
1356 * UCAL_WEEK_OF_MONTH
1357 * UCAL_DATE (DAY_OF_MONTH on Java)
1359 * UCAL_DAY_OF_WEEK_IN_MONTH
1361 * UCAL_EXTENDED_YEAR</pre>
1363 * @param field one of the above field numbers
1364 * @param limitType one of <code>MINIMUM</code>, <code>GREATEST_MINIMUM</code>,
1365 * <code>LEAST_MAXIMUM</code>, or <code>MAXIMUM</code>
1368 virtual int32_t handleGetLimit(UCalendarDateFields field
, ELimitType limitType
) const = 0;
1371 * Return a limit for a field.
1372 * @param field the field, from <code>0..UCAL_MAX_FIELD</code>
1373 * @param limitType the type specifier for the limit
1377 virtual int32_t getLimit(UCalendarDateFields field
, ELimitType limitType
) const;
1381 * Return the Julian day number of day before the first day of the
1382 * given month in the given extended year. Subclasses should override
1383 * this method to implement their calendar system.
1384 * @param eyear the extended year
1385 * @param month the zero-based month, or 0 if useMonth is false
1386 * @param useMonth if false, compute the day before the first day of
1387 * the given year, otherwise, compute the day before the first day of
1389 * @return the Julian day number of the day before the first
1390 * day of the given month and year
1393 virtual int32_t handleComputeMonthStart(int32_t eyear
, int32_t month
,
1394 UBool useMonth
) const = 0;
1397 * Return the number of days in the given month of the given extended
1398 * year of this calendar system. Subclasses should override this
1399 * method if they can provide a more correct or more efficient
1400 * implementation than the default implementation in Calendar.
1403 virtual int32_t handleGetMonthLength(int32_t extendedYear
, int32_t month
) const ;
1406 * Return the number of days in the given extended year of this
1407 * calendar system. Subclasses should override this method if they can
1408 * provide a more correct or more efficient implementation than the
1409 * default implementation in Calendar.
1412 virtual int32_t handleGetYearLength(int32_t eyear
) const;
1416 * Return the extended year defined by the current fields. This will
1417 * use the UCAL_EXTENDED_YEAR field or the UCAL_YEAR and supra-year fields (such
1418 * as UCAL_ERA) specific to the calendar system, depending on which set of
1420 * @return the extended year
1423 virtual int32_t handleGetExtendedYear() = 0;
1426 * Subclasses may override this. This method calls
1427 * handleGetMonthLength() to obtain the calendar-specific month
1429 * @param bestField which field to use to calculate the date
1430 * @return julian day specified by calendar fields.
1433 virtual int32_t handleComputeJulianDay(UCalendarDateFields bestField
);
1436 * Subclasses must override this to convert from week fields
1437 * (YEAR_WOY and WEEK_OF_YEAR) to an extended year in the case
1438 * where YEAR, EXTENDED_YEAR are not set.
1439 * The Calendar implementation assumes yearWoy is in extended gregorian form
1441 * @return the extended year, UCAL_EXTENDED_YEAR
1443 virtual int32_t handleGetExtendedYearFromWeekFields(int32_t yearWoy
, int32_t woy
);
1446 * Compute the Julian day from fields. Will determine whether to use
1447 * the JULIAN_DAY field directly, or other fields.
1448 * @return the julian day
1451 int32_t computeJulianDay();
1454 * Compute the milliseconds in the day from the fields. This is a
1455 * value from 0 to 23:59:59.999 inclusive, unless fields are out of
1456 * range, in which case it can be an arbitrary value. This value
1457 * reflects local zone wall time.
1460 int32_t computeMillisInDay();
1463 * This method can assume EXTENDED_YEAR has been set.
1464 * @param millis milliseconds of the date fields
1465 * @param millisInDay milliseconds of the time fields; may be out
1467 * @param ec Output param set to failure code on function return
1468 * when this function fails.
1471 int32_t computeZoneOffset(double millis
, int32_t millisInDay
, UErrorCode
&ec
);
1475 * Determine the best stamp in a range.
1476 * @param start first enum to look at
1477 * @param end last enum to look at
1478 * @param bestSoFar stamp prior to function call
1479 * @return the stamp value of the best stamp
1482 int32_t newestStamp(UCalendarDateFields start
, UCalendarDateFields end
, int32_t bestSoFar
) const;
1485 * Values for field resolution tables
1486 * @see #resolveFields
1490 /** Marker for end of resolve set (row or group). */
1492 /** 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. */
1497 * Precedence table for Dates
1498 * @see #resolveFields
1501 static const UFieldResolutionTable kDatePrecedence
[];
1504 * Precedence table for Year
1505 * @see #resolveFields
1508 static const UFieldResolutionTable kYearPrecedence
[];
1511 * Precedence table for Day of Week
1512 * @see #resolveFields
1515 static const UFieldResolutionTable kDOWPrecedence
[];
1518 * Given a precedence table, return the newest field combination in
1519 * the table, or UCAL_FIELD_COUNT if none is found.
1521 * <p>The precedence table is a 3-dimensional array of integers. It
1522 * may be thought of as an array of groups. Each group is an array of
1523 * lines. Each line is an array of field numbers. Within a line, if
1524 * all fields are set, then the time stamp of the line is taken to be
1525 * the stamp of the most recently set field. If any field of a line is
1526 * unset, then the line fails to match. Within a group, the line with
1527 * the newest time stamp is selected. The first field of the line is
1528 * returned to indicate which line matched.
1530 * <p>In some cases, it may be desirable to map a line to field that
1531 * whose stamp is NOT examined. For example, if the best field is
1532 * DAY_OF_WEEK then the DAY_OF_WEEK_IN_MONTH algorithm may be used. In
1533 * order to do this, insert the value <code>kResolveRemap | F</code> at
1534 * the start of the line, where <code>F</code> is the desired return
1535 * field value. This field will NOT be examined; it only determines
1536 * the return value if the other fields in the line are the newest.
1538 * <p>If all lines of a group contain at least one unset field, then no
1539 * line will match, and the group as a whole will fail to match. In
1540 * that case, the next group will be processed. If all groups fail to
1541 * match, then UCAL_FIELD_COUNT is returned.
1544 UCalendarDateFields
resolveFields(const UFieldResolutionTable
*precedenceTable
);
1550 virtual const UFieldResolutionTable
* getFieldResolutionTable() const;
1553 * Return the field that is newer, either defaultField, or
1554 * alternateField. If neither is newer or neither is set, return defaultField.
1557 UCalendarDateFields
newerField(UCalendarDateFields defaultField
, UCalendarDateFields alternateField
) const;
1562 * Helper function for calculating limits by trial and error
1563 * @param field The field being investigated
1564 * @param startValue starting (least max) value of field
1565 * @param endValue ending (greatest max) value of field
1566 * @param status return type
1569 int32_t getActualHelper(UCalendarDateFields field
, int32_t startValue
, int32_t endValue
, UErrorCode
&status
) const;
1574 * The flag which indicates if the current time is set in the calendar.
1580 * True if the fields are in sync with the currently set time of this Calendar.
1581 * If false, then the next attempt to get the value of a field will
1582 * force a recomputation of all fields from the current value of the time
1585 * This should really be named areFieldsInSync, but the old name is retained
1586 * for backward compatibility.
1589 UBool fAreFieldsSet
;
1592 * True if all of the fields have been set. This is initially false, and set to
1593 * true by computeFields().
1596 UBool fAreAllFieldsSet
;
1599 * True if all fields have been virtually set, but have not yet been
1600 * computed. This occurs only in setTimeInMillis(). A calendar set
1601 * to this state will compute all fields from the time if it becomes
1602 * necessary, but otherwise will delay such computation.
1605 UBool fAreFieldsVirtuallySet
;
1608 * Get the current time without recomputing.
1610 * @return the current time without recomputing.
1613 UDate
internalGetTime(void) const { return fTime
; }
1616 * Set the current time without affecting flags or fields.
1618 * @param time The time to be set
1619 * @return the current time without recomputing.
1622 void internalSetTime(UDate time
) { fTime
= time
; }
1625 * The time fields containing values into which the millis is computed.
1628 int32_t fFields
[UCAL_FIELD_COUNT
];
1631 * The flags which tell if a specified time field for the calendar is set.
1632 * @deprecated ICU 2.8 use (fStamp[n]!=kUnset)
1634 UBool fIsSet
[UCAL_FIELD_COUNT
];
1636 /** Special values of stamp[]
1646 * Pseudo-time-stamps which specify when each field was set. There
1647 * are two special values, UNSET and INTERNALLY_SET. Values from
1648 * MINIMUM_USER_SET to Integer.MAX_VALUE are legal user set values.
1651 int32_t fStamp
[UCAL_FIELD_COUNT
];
1654 * Subclasses may override this method to compute several fields
1655 * specific to each calendar system. These are:
1662 * <li>EXTENDED_YEAR</ul>
1664 * Subclasses can refer to the DAY_OF_WEEK and DOW_LOCAL fields, which
1665 * will be set when this method is called. Subclasses can also call
1666 * the getGregorianXxx() methods to obtain Gregorian calendar
1667 * equivalents for the given Julian day.
1669 * <p>In addition, subclasses should compute any subclass-specific
1670 * fields, that is, fields from BASE_FIELD_COUNT to
1671 * getFieldCount() - 1.
1673 * <p>The default implementation in <code>Calendar</code> implements
1674 * a pure proleptic Gregorian calendar.
1677 virtual void handleComputeFields(int32_t julianDay
, UErrorCode
&status
);
1680 * Return the extended year on the Gregorian calendar as computed by
1681 * <code>computeGregorianFields()</code>.
1684 int32_t getGregorianYear() const {
1685 return fGregorianYear
;
1689 * Return the month (0-based) on the Gregorian calendar as computed by
1690 * <code>computeGregorianFields()</code>.
1693 int32_t getGregorianMonth() const {
1694 return fGregorianMonth
;
1698 * Return the day of year (1-based) on the Gregorian calendar as
1699 * computed by <code>computeGregorianFields()</code>.
1702 int32_t getGregorianDayOfYear() const {
1703 return fGregorianDayOfYear
;
1707 * Return the day of month (1-based) on the Gregorian calendar as
1708 * computed by <code>computeGregorianFields()</code>.
1711 int32_t getGregorianDayOfMonth() const {
1712 return fGregorianDayOfMonth
;
1716 * Called by computeJulianDay. Returns the default month (0-based) for the year,
1717 * taking year and era into account. Defaults to 0 for Gregorian, which doesn't care.
1721 virtual int32_t getDefaultMonthInYear() ;
1725 * Called by computeJulianDay. Returns the default day (1-based) for the month,
1726 * taking currently-set year and era into account. Defaults to 1 for Gregorian.
1729 virtual int32_t getDefaultDayInMonth(int32_t /*month*/);
1731 //-------------------------------------------------------------------------
1732 // Protected utility methods for use by subclasses. These are very handy
1733 // for implementing add, roll, and computeFields.
1734 //-------------------------------------------------------------------------
1737 * Adjust the specified field so that it is within
1738 * the allowable range for the date to which this calendar is set.
1739 * For example, in a Gregorian calendar pinning the {@link #UCalendarDateFields DAY_OF_MONTH}
1740 * field for a calendar set to April 31 would cause it to be set
1743 * <b>Subclassing:</b>
1745 * This utility method is intended for use by subclasses that need to implement
1746 * their own overrides of {@link #roll roll} and {@link #add add}.
1749 * <code>pinField</code> is implemented in terms of
1750 * {@link #getActualMinimum getActualMinimum}
1751 * and {@link #getActualMaximum getActualMaximum}. If either of those methods uses
1752 * a slow, iterative algorithm for a particular field, it would be
1753 * unwise to attempt to call <code>pinField</code> for that field. If you
1754 * really do need to do so, you should override this method to do
1755 * something more efficient for that field.
1757 * @param field The calendar field whose value should be pinned.
1758 * @param status Output param set to failure code on function return
1759 * when this function fails.
1761 * @see #getActualMinimum
1762 * @see #getActualMaximum
1765 virtual void pinField(UCalendarDateFields field
, UErrorCode
& status
);
1768 * Return the week number of a day, within a period. This may be the week number in
1769 * a year or the week number in a month. Usually this will be a value >= 1, but if
1770 * some initial days of the period are excluded from week 1, because
1771 * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek} is > 1, then
1772 * the week number will be zero for those
1773 * initial days. This method requires the day number and day of week for some
1774 * known date in the period in order to determine the day of week
1775 * on the desired day.
1777 * <b>Subclassing:</b>
1779 * This method is intended for use by subclasses in implementing their
1780 * {@link #computeTime computeTime} and/or {@link #computeFields computeFields} methods.
1781 * It is often useful in {@link #getActualMinimum getActualMinimum} and
1782 * {@link #getActualMaximum getActualMaximum} as well.
1784 * This variant is handy for computing the week number of some other
1785 * day of a period (often the first or last day of the period) when its day
1786 * of the week is not known but the day number and day of week for some other
1787 * day in the period (e.g. the current date) <em>is</em> known.
1789 * @param desiredDay The {@link #UCalendarDateFields DAY_OF_YEAR} or
1790 * {@link #UCalendarDateFields DAY_OF_MONTH} whose week number is desired.
1791 * Should be 1 for the first day of the period.
1793 * @param dayOfPeriod The {@link #UCalendarDateFields DAY_OF_YEAR}
1794 * or {@link #UCalendarDateFields DAY_OF_MONTH} for a day in the period whose
1795 * {@link #UCalendarDateFields DAY_OF_WEEK} is specified by the
1796 * <code>knownDayOfWeek</code> parameter.
1797 * Should be 1 for first day of period.
1799 * @param dayOfWeek The {@link #UCalendarDateFields DAY_OF_WEEK} for the day
1800 * corresponding to the <code>knownDayOfPeriod</code> parameter.
1801 * 1-based with 1=Sunday.
1803 * @return The week number (one-based), or zero if the day falls before
1804 * the first week because
1805 * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek}
1810 int32_t weekNumber(int32_t desiredDay
, int32_t dayOfPeriod
, int32_t dayOfWeek
);
1814 * Return the week number of a day, within a period. This may be the week number in
1815 * a year, or the week number in a month. Usually this will be a value >= 1, but if
1816 * some initial days of the period are excluded from week 1, because
1817 * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek} is > 1,
1818 * then the week number will be zero for those
1819 * initial days. This method requires the day of week for the given date in order to
1820 * determine the result.
1822 * <b>Subclassing:</b>
1824 * This method is intended for use by subclasses in implementing their
1825 * {@link #computeTime computeTime} and/or {@link #computeFields computeFields} methods.
1826 * It is often useful in {@link #getActualMinimum getActualMinimum} and
1827 * {@link #getActualMaximum getActualMaximum} as well.
1829 * @param dayOfPeriod The {@link #UCalendarDateFields DAY_OF_YEAR} or
1830 * {@link #UCalendarDateFields DAY_OF_MONTH} whose week number is desired.
1831 * Should be 1 for the first day of the period.
1833 * @param dayOfWeek The {@link #UCalendarDateFields DAY_OF_WEEK} for the day
1834 * corresponding to the <code>dayOfPeriod</code> parameter.
1835 * 1-based with 1=Sunday.
1837 * @return The week number (one-based), or zero if the day falls before
1838 * the first week because
1839 * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek}
1843 inline int32_t weekNumber(int32_t dayOfPeriod
, int32_t dayOfWeek
);
1846 * returns the local DOW, valid range 0..6
1849 int32_t getLocalDOW();
1854 * The next available value for fStamp[]
1856 int32_t fNextStamp
;// = MINIMUM_USER_STAMP;
1859 * The current time set for the calendar.
1869 * Time zone affects the time calculation done by Calendar. Calendar subclasses use
1870 * the time zone data to produce the local time.
1875 * Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent. They are
1876 * used to figure out the week count for a specific date for a given locale. These
1877 * must be set when a Calendar is constructed. For example, in US locale,
1878 * firstDayOfWeek is SUNDAY; minimalDaysInFirstWeek is 1. They are used to figure
1879 * out the week count for a specific date for a given locale. These must be set when
1880 * a Calendar is constructed.
1882 UCalendarDaysOfWeek fFirstDayOfWeek
;
1883 uint8_t fMinimalDaysInFirstWeek
;
1886 * Sets firstDayOfWeek and minimalDaysInFirstWeek. Called at Calendar construction
1889 * @param desiredLocale The given locale.
1890 * @param type The calendar type identifier, e.g: gregorian, buddhist, etc.
1891 * @param success Indicates the status of setting the week count data from
1892 * the resource for the given locale. Returns U_ZERO_ERROR if
1893 * constructed successfully.
1895 void setWeekCountData(const Locale
& desiredLocale
, const char *type
, UErrorCode
& success
);
1898 * Recompute the time and update the status fields isTimeSet
1899 * and areFieldsSet. Callers should check isTimeSet and only
1900 * call this method if isTimeSet is false.
1902 * @param status Output param set to success/failure code on exit. If any value
1903 * previously set in the time field is invalid or restricted by
1904 * leniency, this will be set to an error status.
1906 void updateTime(UErrorCode
& status
);
1909 * The Gregorian year, as computed by computeGregorianFields() and
1910 * returned by getGregorianYear().
1911 * @see #computeGregorianFields
1913 int32_t fGregorianYear
;
1916 * The Gregorian month, as computed by computeGregorianFields() and
1917 * returned by getGregorianMonth().
1918 * @see #computeGregorianFields
1920 int32_t fGregorianMonth
;
1923 * The Gregorian day of the year, as computed by
1924 * computeGregorianFields() and returned by getGregorianDayOfYear().
1925 * @see #computeGregorianFields
1927 int32_t fGregorianDayOfYear
;
1930 * The Gregorian day of the month, as computed by
1931 * computeGregorianFields() and returned by getGregorianDayOfMonth().
1932 * @see #computeGregorianFields
1934 int32_t fGregorianDayOfMonth
;
1939 * Compute the Gregorian calendar year, month, and day of month from
1940 * the given Julian day. These values are not stored in fields, but in
1941 * member variables gregorianXxx. Also compute the DAY_OF_WEEK and
1944 void computeGregorianAndDOWFields(int32_t julianDay
, UErrorCode
&ec
);
1949 * Compute the Gregorian calendar year, month, and day of month from the
1950 * Julian day. These values are not stored in fields, but in member
1951 * variables gregorianXxx. They are used for time zone computations and by
1952 * subclasses that are Gregorian derivatives. Subclasses may call this
1953 * method to perform a Gregorian calendar millis->fields computation.
1954 * To perform a Gregorian calendar fields->millis computation, call
1955 * computeGregorianMonthStart().
1956 * @see #computeGregorianMonthStart
1958 void computeGregorianFields(int32_t julianDay
, UErrorCode
&ec
);
1963 * Compute the fields WEEK_OF_YEAR, YEAR_WOY, WEEK_OF_MONTH,
1964 * DAY_OF_WEEK_IN_MONTH, and DOW_LOCAL from EXTENDED_YEAR, YEAR,
1965 * DAY_OF_WEEK, and DAY_OF_YEAR. The latter fields are computed by the
1966 * subclass based on the calendar system.
1968 * <p>The YEAR_WOY field is computed simplistically. It is equal to YEAR
1969 * most of the time, but at the year boundary it may be adjusted to YEAR-1
1970 * or YEAR+1 to reflect the overlap of a week into an adjacent year. In
1971 * this case, a simple increment or decrement is performed on YEAR, even
1972 * though this may yield an invalid YEAR value. For instance, if the YEAR
1973 * is part of a calendar system with an N-year cycle field CYCLE, then
1974 * incrementing the YEAR may involve incrementing CYCLE and setting YEAR
1975 * back to 0 or 1. This is not handled by this code, and in fact cannot be
1976 * simply handled without having subclasses define an entire parallel set of
1977 * fields for fields larger than or equal to a year. This additional
1978 * complexity is not warranted, since the intention of the YEAR_WOY field is
1979 * to support ISO 8601 notation, so it will typically be used with a
1980 * proleptic Gregorian calendar, which has no field larger than a year.
1982 void computeWeekFields(UErrorCode
&ec
);
1986 * Ensure that each field is within its valid range by calling {@link
1987 * #validateField(int, int&)} on each field that has been set. This method
1988 * should only be called if this calendar is not lenient.
1990 * @see #validateField(int, int&)
1993 void validateFields(UErrorCode
&status
);
1996 * Validate a single field of this calendar. Subclasses should
1997 * override this method to validate any calendar-specific fields.
1998 * Generic fields can be handled by
1999 * <code>Calendar.validateField()</code>.
2000 * @see #validateField(int, int, int, int&)
2003 virtual void validateField(UCalendarDateFields field
, UErrorCode
&status
);
2006 * Validate a single field of this calendar given its minimum and
2007 * maximum allowed value. If the field is out of range,
2008 * <code>U_ILLEGAL_ARGUMENT_ERROR</code> will be set. Subclasses may
2009 * use this method in their implementation of {@link
2010 * #validateField(int, int&)}.
2013 void validateField(UCalendarDateFields field
, int32_t min
, int32_t max
, UErrorCode
& status
);
2017 * Convert a quasi Julian date to the day of the week. The Julian date used here is
2018 * not a true Julian date, since it is measured from midnight, not noon. Return
2019 * value is one-based.
2021 * @param julian The given Julian date number.
2022 * @return Day number from 1..7 (SUN..SAT).
2025 static uint8_t julianDayToDayOfWeek(double julian
);
2028 char validLocale
[ULOC_FULLNAME_CAPACITY
];
2029 char actualLocale
[ULOC_FULLNAME_CAPACITY
];
2032 #if !UCONFIG_NO_SERVICE
2034 * INTERNAL FOR 2.6 -- Registration.
2038 * Return a StringEnumeration over the locales available at the time of the call,
2039 * including registered locales.
2040 * @return a StringEnumeration over the locales available at the time of the call
2043 static StringEnumeration
* getAvailableLocales(void);
2046 * Register a new Calendar factory. The factory will be adopted.
2048 * @param toAdopt the factory instance to be adopted
2049 * @param status the in/out status code, no special meanings are assigned
2050 * @return a registry key that can be used to unregister this factory
2053 static URegistryKey
registerFactory(ICUServiceFactory
* toAdopt
, UErrorCode
& status
);
2056 * Unregister a previously-registered CalendarFactory using the key returned from the
2057 * register call. Key becomes invalid after a successful call and should not be used again.
2058 * The CalendarFactory corresponding to the key will be deleted.
2060 * @param key the registry key returned by a previous call to registerFactory
2061 * @param status the in/out status code, no special meanings are assigned
2062 * @return TRUE if the factory for the key was successfully unregistered
2065 static UBool
unregister(URegistryKey key
, UErrorCode
& status
);
2068 * Multiple Calendar Implementation
2071 friend class CalendarFactory
;
2074 * Multiple Calendar Implementation
2077 friend class CalendarService
;
2080 * Multiple Calendar Implementation
2083 friend class DefaultCalendarFactory
;
2084 #endif /* !UCONFIG_NO_SERVICE */
2088 * @return TRUE if this calendar has a default century (i.e. 03 -> 2003)
2090 virtual UBool
haveDefaultCentury() const = 0;
2094 * @return the start of the default century, as a UDate
2096 virtual UDate
defaultCenturyStart() const = 0;
2099 * @return the beginning year of the default century, as a year
2101 virtual int32_t defaultCenturyStartYear() const = 0;
2103 /** Get the locale for this calendar object. You can choose between valid and actual locale.
2104 * @param type type of the locale we're looking for (valid or actual)
2105 * @param status error code for the operation
2106 * @return the locale
2109 Locale
getLocale(ULocDataLocaleType type
, UErrorCode
&status
) const;
2111 /** Get the locale for this calendar object. You can choose between valid and actual locale.
2112 * @param type type of the locale we're looking for (valid or actual)
2113 * @param status error code for the operation
2114 * @return the locale
2117 const char* getLocaleID(ULocDataLocaleType type
, UErrorCode
&status
) const;
2121 // -------------------------------------
2124 Calendar::createInstance(TimeZone
* zone
, UErrorCode
& errorCode
)
2126 // since the Locale isn't specified, use the default locale
2127 return createInstance(zone
, Locale::getDefault(), errorCode
);
2130 // -------------------------------------
2133 Calendar::roll(UCalendarDateFields field
, UBool up
, UErrorCode
& status
)
2135 roll(field
, (int32_t)(up
? +1 : -1), status
);
2139 Calendar::roll(EDateFields field
, UBool up
, UErrorCode
& status
)
2141 roll((UCalendarDateFields
) field
, up
, status
);
2145 // -------------------------------------
2148 * Fast method for subclasses. The caller must maintain fUserSetDSTOffset and
2149 * fUserSetZoneOffset, as well as the isSet[] array.
2153 Calendar::internalSet(UCalendarDateFields field
, int32_t value
)
2155 fFields
[field
] = value
;
2156 fStamp
[field
] = kInternallySet
;
2157 fIsSet
[field
] = TRUE
; // Remove later
2160 inline int32_t Calendar::weekNumber(int32_t dayOfPeriod
, int32_t dayOfWeek
)
2162 return weekNumber(dayOfPeriod
, dayOfPeriod
, dayOfWeek
);
2168 #endif /* #if !UCONFIG_NO_FORMATTING */