]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/calendar.h
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / calendar.h
CommitLineData
b75a7d8f
A
1/*
2********************************************************************************
73c04bcf 3* Copyright (C) 1997-2006, International Business Machines
b75a7d8f
A
4* Corporation and others. All Rights Reserved.
5********************************************************************************
6*
7* File CALENDAR.H
8*
9* Modification History:
10*
11* Date Name Description
12* 04/22/97 aliu Expanded and corrected comments and other header
13* contents.
14* 05/01/97 aliu Made equals(), before(), after() arguments const.
15* 05/20/97 aliu Replaced fAreFieldsSet with fAreFieldsInSync and
16* fAreAllFieldsSet.
17* 07/27/98 stephen Sync up with JDK 1.2
18* 11/15/99 weiv added YEAR_WOY and DOW_LOCAL
19* to EDateFields
20* 8/19/2002 srl Removed Javaisms
374ca955 21* 11/07/2003 srl Update, clean up documentation.
b75a7d8f
A
22********************************************************************************
23*/
24
25#ifndef CALENDAR_H
26#define CALENDAR_H
27
28#include "unicode/utypes.h"
29
73c04bcf
A
30/**
31 * \file
32 * \brief C++ API: Calendar object
33 */
b75a7d8f
A
34#if !UCONFIG_NO_FORMATTING
35
36#include "unicode/uobject.h"
37#include "unicode/locid.h"
38#include "unicode/timezone.h"
39#include "unicode/ucal.h"
73c04bcf 40#include "unicode/umisc.h"
b75a7d8f
A
41
42U_NAMESPACE_BEGIN
43
44class ICUServiceFactory;
45
374ca955
A
46/**
47 * @internal
48 */
49typedef int32_t UFieldResolutionTable[12][8];
50
b75a7d8f
A
51/**
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.)
58 *
59 * <p>
60 * Subclasses of <code>Calendar</code> interpret a <code>UDate</code>
374ca955 61 * according to the rules of a specific calendar system.
b75a7d8f
A
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.
374ca955 65 *
b75a7d8f
A
66 * <p>
67 * <b>NOTE</b>: (ICU 2.6) The subclass interface should be considered unstable
374ca955 68 * - it WILL change.
b75a7d8f
A
69 *
70 * <p>
71 * Like other locale-sensitive classes, <code>Calendar</code> provides a
72 * static method, <code>createInstance</code>, for getting a generally useful
374ca955 73 * object of this type. <code>Calendar</code>'s <code>createInstance</code> method
b75a7d8f
A
74 * returns the appropriate <code>Calendar</code> subclass whose
75 * time fields have been initialized with the current date and time:
374ca955 76 * \htmlonly<blockquote>\endhtmlonly
b75a7d8f
A
77 * <pre>
78 * Calendar *rightNow = Calendar::createInstance(errCode);
79 * </pre>
374ca955 80 * \htmlonly</blockquote>\endhtmlonly
b75a7d8f
A
81 *
82 * <p>
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).
86 *
87 * <p>
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).
93 *
94 * <p>
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.
99 *
100 * <p>
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.
106 *
374ca955 107 * \htmlonly<blockquote>\endhtmlonly
b75a7d8f
A
108 * <pre>
109 * MONTH + DAY_OF_MONTH
110 * MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
111 * MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
112 * DAY_OF_YEAR
113 * DAY_OF_WEEK + WEEK_OF_YEAR
114 * </pre>
374ca955 115 * \htmlonly</blockquote>\endhtmlonly
b75a7d8f
A
116 *
117 * For the time of day:
118 *
374ca955 119 * \htmlonly<blockquote>\endhtmlonly
b75a7d8f
A
120 * <pre>
121 * HOUR_OF_DAY
122 * AM_PM + HOUR
123 * </pre>
374ca955 124 * \htmlonly</blockquote>\endhtmlonly
b75a7d8f
A
125 *
126 * <p>
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.
131 *
132 * <p>
133 * <strong>Note:</strong> There are certain possible ambiguities in
134 * interpretation of certain singular times, which are resolved in the
135 * following ways:
136 * <ol>
137 * <li> 24:00:00 "belongs" to the following day. That is,
138 * 23:59 on Dec 31, 1969 &lt; 24:00 on Jan 1, 1970 &lt; 24:01:00 on Jan 1, 1970
139 *
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) &lt; 12:01 am, and 12:00 pm (noon) &lt; 12:01 pm
143 * </ol>
144 *
145 * <p>
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}
149 * to format dates.
150 *
151 * <p>
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>.
156 *
157 * <p>
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>.
162 *
163 * @stable ICU 2.0
164 */
165class U_I18N_API Calendar : public UObject {
166public:
167
168 /**
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
173 */
174 enum EDateFields {
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
374ca955
A
192 YEAR_WOY, // 'Y' Example: 1..big number - Year of Week of Year
193 DOW_LOCAL, // 'e' Example: 1..7 - Day of Week / Localized
b75a7d8f 194
374ca955 195 FIELD_COUNT = UCAL_FIELD_COUNT // See ucal.h for other fields.
b75a7d8f
A
196 };
197
198 /**
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
203 */
204 enum EDaysOfWeek {
205 SUNDAY = 1,
206 MONDAY,
207 TUESDAY,
208 WEDNESDAY,
209 THURSDAY,
210 FRIDAY,
211 SATURDAY
212 };
213
214 /**
215 * Useful constants for month. Note: Calendar month is 0-based.
216 * @deprecated ICU 2.6. Use C enum UCalendarMonths defined in ucal.h
217 */
218 enum EMonths {
219 JANUARY,
220 FEBRUARY,
221 MARCH,
222 APRIL,
223 MAY,
224 JUNE,
225 JULY,
226 AUGUST,
227 SEPTEMBER,
228 OCTOBER,
229 NOVEMBER,
230 DECEMBER,
231 UNDECIMBER
232 };
233
234 /**
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
237 */
238 enum EAmpm {
239 AM,
240 PM
241 };
242
243 /**
244 * destructor
245 * @stable ICU 2.0
246 */
247 virtual ~Calendar();
248
249 /**
250 * Create and return a polymorphic copy of this calendar.
251 *
252 * @return a polymorphic copy of this calendar.
253 * @stable ICU 2.0
254 */
255 virtual Calendar* clone(void) const = 0;
256
257 /**
258 * Creates a Calendar using the default timezone and locale. Clients are responsible
259 * for deleting the object returned.
260 *
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.
266 * @stable ICU 2.0
267 */
374ca955 268 static Calendar* U_EXPORT2 createInstance(UErrorCode& success);
b75a7d8f
A
269
270 /**
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.
274 *
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
278 * otherwise.
279 * @return A Calendar if created successfully. NULL otherwise.
280 * @stable ICU 2.0
281 */
374ca955 282 static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, UErrorCode& success);
b75a7d8f
A
283
284 /**
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.
287 *
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
291 * otherwise.
292 * @return A Calendar if created successfully. NULL otherwise.
293 * @stable ICU 2.0
294 */
374ca955 295 static Calendar* U_EXPORT2 createInstance(const TimeZone& zone, UErrorCode& success);
b75a7d8f
A
296
297 /**
298 * Creates a Calendar using the default timezone and the given locale.
299 *
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
303 * otherwise.
304 * @return A Calendar if created successfully. NULL otherwise.
305 * @stable ICU 2.0
306 */
374ca955 307 static Calendar* U_EXPORT2 createInstance(const Locale& aLocale, UErrorCode& success);
b75a7d8f
A
308
309 /**
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.
313 *
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
318 * otherwise.
319 * @return A Calendar if created successfully. NULL otherwise.
320 * @stable ICU 2.0
321 */
374ca955 322 static Calendar* U_EXPORT2 createInstance(TimeZone* zoneToAdopt, const Locale& aLocale, UErrorCode& success);
b75a7d8f
A
323
324 /**
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.
327 *
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
332 * otherwise.
333 * @return A Calendar if created successfully. NULL otherwise.
334 * @stable ICU 2.0
335 */
374ca955 336 static Calendar* U_EXPORT2 createInstance(const TimeZone& zoneToAdopt, const Locale& aLocale, UErrorCode& success);
b75a7d8f
A
337
338 /**
339 * Returns a list of the locales for which Calendars are installed.
340 *
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.
345 * @stable ICU 2.0
346 */
374ca955 347 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
b75a7d8f
A
348
349 /**
374ca955 350 * Returns the current UTC (GMT) time measured in milliseconds since 0:00:00 on 1/1/70
b75a7d8f
A
351 * (derived from the system time).
352 *
353 * @return The current UTC time in milliseconds.
354 * @stable ICU 2.0
355 */
374ca955 356 static UDate U_EXPORT2 getNow(void);
b75a7d8f
A
357
358 /**
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).
363 *
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
368 * failed.
369 * @stable ICU 2.0
370 */
371 inline UDate getTime(UErrorCode& status) const { return getTimeInMillis(status); }
372
373 /**
374 * Sets this Calendar's current time with the given UDate. The time specified should
375 * be in non-local UTC (GMT) time.
376 *
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.
381 * @stable ICU 2.0
382 */
383 inline void setTime(UDate date, UErrorCode& status) { setTimeInMillis(date, status); }
384
385 /**
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.
390 *
391 * @param that The Calendar object to be compared with.
392 * @return True if the given Calendar is the same as this Calendar; false
393 * otherwise.
394 * @stable ICU 2.0
395 */
396 virtual UBool operator==(const Calendar& that) const;
397
398 /**
399 * Compares the inequality of two Calendar objects.
400 *
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
403 * otherwise.
404 * @stable ICU 2.0
405 */
406 UBool operator!=(const Calendar& that) const {return !operator==(that);}
407
408 /**
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.
414 *
374ca955
A
415 * @param other the Calendar to be compared with this Calendar
416 * @stable ICU 2.4
b75a7d8f
A
417 */
418 virtual UBool isEquivalentTo(const Calendar& other) const;
419
420 /**
421 * Compares the Calendar time, whereas Calendar::operator== compares the equality of
422 * Calendar objects.
423 *
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.
432 * @stable ICU 2.0
433 */
434 UBool equals(const Calendar& when, UErrorCode& status) const;
435
436 /**
437 * Returns true if this Calendar's current time is before "when"'s current time.
438 *
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.
447 * @stable ICU 2.0
448 */
449 UBool before(const Calendar& when, UErrorCode& status) const;
450
451 /**
452 * Returns true if this Calendar's current time is after "when"'s current time.
453 *
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.
462 * @stable ICU 2.0
463 */
464 UBool after(const Calendar& when, UErrorCode& status) const;
465
466 /**
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
472 * in 02/29/96.
473 *
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
477 * field.)
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.
482 */
374ca955 483 virtual void add(EDateFields field, int32_t amount, UErrorCode& status);
b75a7d8f
A
484
485 /**
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
491 * in 02/29/96.
492 *
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
496 * field.)
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.
374ca955 500 * @stable ICU 2.6.
b75a7d8f 501 */
374ca955 502 virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode& status);
b75a7d8f
A
503
504 /**
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.
516 * <P>
517 * NOTE: Do not use this method -- use roll(EDateFields, int, UErrorCode&) instead.
518 *
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.
526 */
527 inline void roll(EDateFields field, UBool up, UErrorCode& status);
528
529 /**
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.
541 * <P>
542 * NOTE: Do not use this method -- use roll(UCalendarDateFields, int, UErrorCode&) instead.
543 *
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.
374ca955 550 * @stable ICU 2.6.
b75a7d8f
A
551 */
552 inline void roll(UCalendarDateFields field, UBool up, UErrorCode& status);
553
554 /**
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.
564 * <P>
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.
568 *
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
573 * an error status.
574 * @deprecated ICU 2.6. Use roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) instead.
575 */
374ca955 576 virtual void roll(EDateFields field, int32_t amount, UErrorCode& status);
b75a7d8f
A
577
578 /**
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.
588 * <P>
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.
592 *
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
597 * an error status.
374ca955 598 * @stable ICU 2.6.
b75a7d8f 599 */
374ca955 600 virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode& status);
b75a7d8f
A
601
602 /**
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.
613 *
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.
618 *
619 * <p>Usage: To use this method, call it first with the largest
620 * field of interest, then with progressively smaller fields. For
621 * example:
622 *
623 * <pre>
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>
627 *
628 * computes the difference between <code>cal</code> and
629 * <code>when</code> in years, months, and days.
630 *
631 * <p>Note: <code>fieldDifference()</code> is
632 * <em>asymmetrical</em>. That is, in the following code:
633 *
634 * <pre>
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>
641 *
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).
646 *
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
651 * an error status.
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).
656 */
657 virtual int32_t fieldDifference(UDate when, EDateFields field, UErrorCode& status);
658
659 /**
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.
670 *
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.
675 *
676 * <p>Usage: To use this method, call it first with the largest
677 * field of interest, then with progressively smaller fields. For
678 * example:
679 *
680 * <pre>
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>
684 *
685 * computes the difference between <code>cal</code> and
686 * <code>when</code> in years, months, and days.
687 *
688 * <p>Note: <code>fieldDifference()</code> is
689 * <em>asymmetrical</em>. That is, in the following code:
690 *
691 * <pre>
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>
698 *
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).
703 *
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
708 * an error status.
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>.
374ca955 712 * @stable ICU 2.6.
b75a7d8f
A
713 */
714 virtual int32_t fieldDifference(UDate when, UCalendarDateFields field, UErrorCode& status);
715
716 /**
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.
720 *
721 * @param value The given time zone.
722 * @stable ICU 2.0
723 */
724 void adoptTimeZone(TimeZone* value);
725
726 /**
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.
729 *
730 * @param zone The given time zone.
731 * @stable ICU 2.0
732 */
733 void setTimeZone(const TimeZone& zone);
734
735 /**
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.
739 *
740 * @return The time zone object associated with this calendar.
741 * @stable ICU 2.0
742 */
743 const TimeZone& getTimeZone(void) const;
744
745 /**
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().
749 *
750 * @return The time zone object which was associated with this calendar.
751 * @stable ICU 2.0
752 */
753 TimeZone* orphanTimeZone(void);
754
755 /**
756 * Queries if the current date for this Calendar is in Daylight Savings Time.
757 *
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,
760 * false, otherwise.
761 * @stable ICU 2.0
762 */
763 virtual UBool inDaylightTime(UErrorCode& status) const = 0;
764
765 /**
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.
771 *
772 * @param lenient True specifies date/time interpretation to be lenient.
773 *
774 * @see DateFormat#setLenient
775 * @stable ICU 2.0
776 */
777 void setLenient(UBool lenient);
778
779 /**
780 * Tells whether date/time interpretation is to be lenient.
781 *
782 * @return True tells that date/time interpretation is to be lenient.
783 * @stable ICU 2.0
784 */
785 UBool isLenient(void) const;
786
787 /**
788 * Sets what the first day of the week is; e.g., Sunday in US, Monday in France.
789 *
790 * @param value The given first day of the week.
791 * @deprecated ICU 2.6. Use setFirstDayOfWeek(UCalendarDaysOfWeek value) instead.
792 */
793 void setFirstDayOfWeek(EDaysOfWeek value);
794
795 /**
796 * Sets what the first day of the week is; e.g., Sunday in US, Monday in France.
797 *
798 * @param value The given first day of the week.
374ca955 799 * @stable ICU 2.6.
b75a7d8f
A
800 */
801 void setFirstDayOfWeek(UCalendarDaysOfWeek value);
802
803 /**
804 * Gets what the first day of the week is; e.g., Sunday in US, Monday in France.
805 *
806 * @return The first day of the week.
807 * @deprecated ICU 2.6 use the overload with error code
808 */
809 EDaysOfWeek getFirstDayOfWeek(void) const;
810
811 /**
812 * Gets what the first day of the week is; e.g., Sunday in US, Monday in France.
813 *
814 * @param status error code
815 * @return The first day of the week.
374ca955 816 * @stable ICU 2.6
b75a7d8f
A
817 */
818 UCalendarDaysOfWeek getFirstDayOfWeek(UErrorCode &status) const;
819
820 /**
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,
824 * use value 7.
825 *
826 * @param value The given minimal days required in the first week of the year.
827 * @stable ICU 2.0
828 */
829 void setMinimalDaysInFirstWeek(uint8_t value);
830
831 /**
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.
836 *
837 * @return The minimal days required in the first week of the year.
838 * @stable ICU 2.0
839 */
840 uint8_t getMinimalDaysInFirstWeek(void) const;
841
842 /**
843 * Gets the minimum value for the given time field. e.g., for Gregorian
844 * DAY_OF_MONTH, 1.
845 *
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.
849 */
374ca955 850 virtual int32_t getMinimum(EDateFields field) const;
b75a7d8f
A
851
852 /**
853 * Gets the minimum value for the given time field. e.g., for Gregorian
854 * DAY_OF_MONTH, 1.
855 *
856 * @param field The given time field.
857 * @return The minimum value for the given time field.
374ca955 858 * @stable ICU 2.6.
b75a7d8f 859 */
374ca955 860 virtual int32_t getMinimum(UCalendarDateFields field) const;
b75a7d8f
A
861
862 /**
863 * Gets the maximum value for the given time field. e.g. for Gregorian DAY_OF_MONTH,
864 * 31.
865 *
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.
869 */
374ca955 870 virtual int32_t getMaximum(EDateFields field) const;
b75a7d8f
A
871
872 /**
873 * Gets the maximum value for the given time field. e.g. for Gregorian DAY_OF_MONTH,
874 * 31.
875 *
876 * @param field The given time field.
877 * @return The maximum value for the given time field.
374ca955 878 * @stable ICU 2.6.
b75a7d8f 879 */
374ca955 880 virtual int32_t getMaximum(UCalendarDateFields field) const;
b75a7d8f
A
881
882 /**
883 * Gets the highest minimum value for the given field if varies. Otherwise same as
884 * getMinimum(). For Gregorian, no difference.
885 *
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.
889 */
374ca955 890 virtual int32_t getGreatestMinimum(EDateFields field) const;
b75a7d8f
A
891
892 /**
893 * Gets the highest minimum value for the given field if varies. Otherwise same as
894 * getMinimum(). For Gregorian, no difference.
895 *
896 * @param field The given time field.
897 * @return The highest minimum value for the given time field.
374ca955 898 * @stable ICU 2.6.
b75a7d8f 899 */
374ca955 900 virtual int32_t getGreatestMinimum(UCalendarDateFields field) const;
b75a7d8f
A
901
902 /**
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.
905 *
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.
909 */
374ca955 910 virtual int32_t getLeastMaximum(EDateFields field) const;
b75a7d8f
A
911
912 /**
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.
915 *
916 * @param field The given time field.
917 * @return The lowest maximum value for the given time field.
374ca955 918 * @stable ICU 2.6.
b75a7d8f 919 */
374ca955 920 virtual int32_t getLeastMaximum(UCalendarDateFields field) const;
b75a7d8f
A
921
922 /**
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().
925 *
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.
930 *
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.
935 */
936 int32_t getActualMinimum(EDateFields field, UErrorCode& status) const;
937
938 /**
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().
941 *
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.
946 *
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
374ca955 950 * @stable ICU 2.6.
b75a7d8f
A
951 */
952 int32_t getActualMinimum(UCalendarDateFields field, UErrorCode& status) const;
953
954 /**
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.
959 *
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.
964 *
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.
969 */
970 int32_t getActualMaximum(EDateFields field, UErrorCode& status) const;
971
972 /**
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.
977 *
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.
982 *
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
374ca955 986 * @stable ICU 2.6.
b75a7d8f
A
987 */
988 int32_t getActualMaximum(UCalendarDateFields field, UErrorCode& status) const;
989
990 /**
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.
996 *
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.
1002 */
1003 int32_t get(EDateFields field, UErrorCode& status) const;
1004
1005 /**
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.
1011 *
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.
374ca955 1016 * @stable ICU 2.6.
b75a7d8f
A
1017 */
1018 int32_t get(UCalendarDateFields field, UErrorCode& status) const;
1019
1020 /**
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.
1023 *
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.
1027 */
1028 UBool isSet(EDateFields field) const;
1029
1030 /**
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.
1033 *
1034 * @param field The given time field.
1035 * @return True if the given time field has a value set; false otherwise.
374ca955 1036 * @stable ICU 2.6.
b75a7d8f
A
1037 */
1038 UBool isSet(UCalendarDateFields field) const;
1039
1040 /**
1041 * Sets the given time field with the given value.
1042 *
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.
1046 */
1047 void set(EDateFields field, int32_t value);
1048
1049 /**
1050 * Sets the given time field with the given value.
1051 *
1052 * @param field The given time field.
1053 * @param value The value to be set for the given time field.
374ca955 1054 * @stable ICU 2.6.
b75a7d8f
A
1055 */
1056 void set(UCalendarDateFields field, int32_t value);
1057
1058 /**
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.
1061 *
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.
1066 * @stable ICU 2.0
1067 */
1068 void set(int32_t year, int32_t month, int32_t date);
1069
1070 /**
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.
1073 *
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.
1080 * @stable ICU 2.0
1081 */
1082 void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute);
1083
1084 /**
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.
1087 *
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.
1095 * @stable ICU 2.0
1096 */
1097 void set(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute, int32_t second);
1098
1099 /**
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.
1103 * @stable ICU 2.0
1104 */
1105 void clear(void);
1106
1107 /**
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.
1111 *
1112 * @param field The time field to be cleared.
1113 * @deprecated ICU 2.6. Use clear(UCalendarDateFields field) instead.
1114 */
1115 void clear(EDateFields field);
1116
1117 /**
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.
1121 *
1122 * @param field The time field to be cleared.
374ca955 1123 * @stable ICU 2.6.
b75a7d8f
A
1124 */
1125 void clear(UCalendarDateFields field);
1126
1127 /**
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.
1131 * <P>
1132 * Concrete subclasses of Calendar must implement getDynamicClassID() and also a
1133 * static method and data member:
1134 *
374ca955 1135 * static UClassID getStaticClassID() { return (UClassID)&amp;fgClassID; }
b75a7d8f
A
1136 * static char fgClassID;
1137 *
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.
1140 * @stable ICU 2.0
1141 */
1142 virtual UClassID getDynamicClassID(void) const = 0;
1143
1144 /**
1145 * Returns the resource key string used for this calendar type.
1146 * For example, prepending "Eras_" to this string could return "Eras_japanese"
374ca955 1147 * or "Eras_gregorian".
b75a7d8f
A
1148 *
1149 * @returns static string, for example, "gregorian" or "japanese"
1150 * @internal
1151 */
1152 virtual const char * getType() const = 0;
1153
1154protected:
1155
1156 /**
1157 * Constructs a Calendar with the default time zone as returned by
1158 * TimeZone::createInstance(), and the default locale.
1159 *
1160 * @param success Indicates the status of Calendar object construction. Returns
1161 * U_ZERO_ERROR if constructed successfully.
1162 * @stable ICU 2.0
1163 */
1164 Calendar(UErrorCode& success);
1165
1166 /**
1167 * Copy constructor
1168 *
1169 * @param source Calendar object to be copied from
1170 * @stable ICU 2.0
1171 */
1172 Calendar(const Calendar& source);
1173
1174 /**
1175 * Default assignment operator
1176 *
1177 * @param right Calendar object to be copied
1178 * @stable ICU 2.0
1179 */
1180 Calendar& operator=(const Calendar& right);
1181
1182 /**
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.
1185 *
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.
1190 * @stable ICU 2.0
1191 */
1192 Calendar(TimeZone* zone, const Locale& aLocale, UErrorCode& success);
1193
1194 /**
1195 * Constructs a Calendar with the given time zone and locale.
1196 *
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.
1201 * @stable ICU 2.0
1202 */
1203 Calendar(const TimeZone& zone, const Locale& aLocale, UErrorCode& success);
1204
1205 /**
1206 * Converts Calendar's time field values to GMT as milliseconds.
1207 *
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.
1211 * @stable ICU 2.0
1212 */
374ca955 1213 virtual void computeTime(UErrorCode& status);
b75a7d8f
A
1214
1215 /**
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().
1220 *
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.
1224 * @stable ICU 2.0
1225 */
374ca955 1226 virtual void computeFields(UErrorCode& status);
b75a7d8f
A
1227
1228 /**
1229 * Gets this Calendar's current time as a long.
1230 *
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.
1235 * @stable ICU 2.0
1236 */
1237 double getTimeInMillis(UErrorCode& status) const;
1238
1239 /**
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.
1245 * @stable ICU 2.0
1246 */
1247 void setTimeInMillis( double millis, UErrorCode& status );
1248
1249 /**
1250 * Recomputes the current time from currently set fields, and then fills in any
1251 * unset fields in the time field list.
1252 *
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.
1256 * @stable ICU 2.0
1257 */
1258 void complete(UErrorCode& status);
1259
1260 /**
1261 * Gets the value for a given time field. Subclasses can use this function to get
1262 * field values without forcing recomputation of time.
1263 *
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.
1267 */
1268 inline int32_t internalGet(EDateFields field) const {return fFields[field];}
1269
374ca955
A
1270 /**
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.
1274 *
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.
1278 * @internal
1279 */
1280 inline int32_t internalGet(UCalendarDateFields field, int32_t defaultValue) const {return fStamp[field]>kUnset ? fFields[field] : defaultValue;}
1281
b75a7d8f
A
1282 /**
1283 * Gets the value for a given time field. Subclasses can use this function to get
1284 * field values without forcing recomputation of time.
1285 *
1286 * @param field The given time field.
1287 * @return The value for the given time field.
374ca955 1288 * @internal
b75a7d8f
A
1289 */
1290 inline int32_t internalGet(UCalendarDateFields field) const {return fFields[field];}
1291
1292 /**
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
1295 * flags.
1296 *
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.
1300 */
1301 void internalSet(EDateFields field, int32_t value);
1302
1303 /**
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
1306 * flags.
1307 *
1308 * @param field The given time field.
1309 * @param value The value for the given time field.
374ca955 1310 * @stable ICU 2.6.
b75a7d8f
A
1311 */
1312 inline void internalSet(UCalendarDateFields field, int32_t value);
1313
374ca955
A
1314 /**
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.
1318 * @internal
1319 */
1320 virtual void prepareGetActual(UCalendarDateFields field, UBool isMinimum, UErrorCode &status);
1321
1322 /**
1323 * Limit enums. Not in sync with UCalendarLimitType (refers to internal fields).
1324 * @internal
1325 */
1326 enum ELimitType {
1327 UCAL_LIMIT_MINIMUM = 0,
1328 UCAL_LIMIT_GREATEST_MINIMUM,
1329 UCAL_LIMIT_LEAST_MAXIMUM,
1330 UCAL_LIMIT_MAXIMUM,
1331 UCAL_LIMIT_COUNT
1332 };
1333
1334 /**
1335 * Subclass API for defining limits of different types.
1336 * Subclasses must implement this method to return limits for the
1337 * following fields:
1338 *
1339 * <pre>UCAL_ERA
1340 * UCAL_YEAR
1341 * UCAL_MONTH
1342 * UCAL_WEEK_OF_YEAR
1343 * UCAL_WEEK_OF_MONTH
1344 * UCAL_DATE (DAY_OF_MONTH on Java)
1345 * UCAL_DAY_OF_YEAR
1346 * UCAL_DAY_OF_WEEK_IN_MONTH
1347 * UCAL_YEAR_WOY
1348 * UCAL_EXTENDED_YEAR</pre>
1349 *
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>
1353 * @internal
1354 */
1355 virtual int32_t handleGetLimit(UCalendarDateFields field, ELimitType limitType) const = 0;
1356
1357 /**
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
1361 * @see #ELimitType
1362 * @internal
1363 */
1364 virtual int32_t getLimit(UCalendarDateFields field, ELimitType limitType) const;
1365
1366
1367 /**
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
1375 * the given month
1376 * @return the Julian day number of the day before the first
1377 * day of the given month and year
1378 * @internal
1379 */
1380 virtual int32_t handleComputeMonthStart(int32_t eyear, int32_t month,
1381 UBool useMonth) const = 0;
1382
1383 /**
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.
1388 * @internal
1389 */
1390 virtual int32_t handleGetMonthLength(int32_t extendedYear, int32_t month) const ;
1391
1392 /**
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.
1397 * @stable ICU 2.0
1398 */
1399 virtual int32_t handleGetYearLength(int32_t eyear) const;
1400
1401
1402 /**
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
1406 * fields is newer.
1407 * @return the extended year
1408 * @internal
1409 */
1410 virtual int32_t handleGetExtendedYear() = 0;
1411
1412 /**
1413 * Subclasses may override this. This method calls
1414 * handleGetMonthLength() to obtain the calendar-specific month
1415 * length.
1416 * @param bestField which field to use to calculate the date
1417 * @return julian day specified by calendar fields.
1418 * @internal
1419 */
1420 virtual int32_t handleComputeJulianDay(UCalendarDateFields bestField);
1421
1422 /**
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
1427 * @internal
1428 * @return the extended year, UCAL_EXTENDED_YEAR
1429 */
1430 virtual int32_t handleGetExtendedYearFromWeekFields(int32_t yearWoy, int32_t woy);
1431
1432 /**
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
1436 * @internal
1437 */
1438 int32_t computeJulianDay();
1439
1440 /**
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.
1445 * @internal
1446 */
1447 int32_t computeMillisInDay();
1448
1449 /**
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
1453 * or range.
1454 * @param ec Output param set to failure code on function return
1455 * when this function fails.
1456 * @internal
1457 */
1458 int32_t computeZoneOffset(double millis, int32_t millisInDay, UErrorCode &ec);
1459
1460
1461 /**
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
1467 * @internal
1468 */
1469 int32_t newestStamp(UCalendarDateFields start, UCalendarDateFields end, int32_t bestSoFar) const;
1470
1471 /**
1472 * Values for field resolution tables
1473 * @see #resolveFields
1474 * @internal
1475 */
1476 enum {
1477 /** Marker for end of resolve set (row or group). */
1478 kResolveSTOP = -1,
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. */
1480 kResolveRemap = 32
1481 };
1482
1483 /**
1484 * Precedence table for Dates
1485 * @see #resolveFields
1486 * @internal
1487 */
1488 static const UFieldResolutionTable kDatePrecedence[];
1489
1490 /**
1491 * Precedence table for Year
1492 * @see #resolveFields
1493 * @internal
1494 */
1495 static const UFieldResolutionTable kYearPrecedence[];
1496
1497 /**
1498 * Precedence table for Day of Week
1499 * @see #resolveFields
1500 * @internal
1501 */
1502 static const UFieldResolutionTable kDOWPrecedence[];
1503
1504 /**
1505 * Given a precedence table, return the newest field combination in
1506 * the table, or UCAL_FIELD_COUNT if none is found.
1507 *
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.
1516 *
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.
1524 *
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.
1529 * @internal
1530 */
1531 UCalendarDateFields resolveFields(const UFieldResolutionTable *precedenceTable);
1532
1533
1534 /**
1535 * @internal
1536 */
1537 virtual const UFieldResolutionTable* getFieldResolutionTable() const;
1538
1539 /**
1540 * Return the field that is newer, either defaultField, or
1541 * alternateField. If neither is newer or neither is set, return defaultField.
1542 * @internal
1543 */
1544 UCalendarDateFields newerField(UCalendarDateFields defaultField, UCalendarDateFields alternateField) const;
1545
1546
1547private:
1548 /**
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
1554 * @internal
1555 */
1556 int32_t getActualHelper(UCalendarDateFields field, int32_t startValue, int32_t endValue, UErrorCode &status) const;
1557
1558
b75a7d8f
A
1559protected:
1560 /**
1561 * The flag which indicates if the current time is set in the calendar.
1562 * @stable ICU 2.0
1563 */
1564 UBool fIsTimeSet;
1565
1566 /**
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
1570 * field.
1571 * <P>
1572 * This should really be named areFieldsInSync, but the old name is retained
1573 * for backward compatibility.
1574 * @stable ICU 2.0
1575 */
1576 UBool fAreFieldsSet;
1577
1578 /**
1579 * True if all of the fields have been set. This is initially false, and set to
1580 * true by computeFields().
1581 * @stable ICU 2.0
1582 */
1583 UBool fAreAllFieldsSet;
1584
374ca955
A
1585 /**
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.
73c04bcf 1590 * @stable ICU 3.0
374ca955
A
1591 */
1592 UBool fAreFieldsVirtuallySet;
1593
b75a7d8f
A
1594 /**
1595 * Get the current time without recomputing.
1596 *
1597 * @return the current time without recomputing.
1598 * @stable ICU 2.0
1599 */
1600 UDate internalGetTime(void) const { return fTime; }
1601
1602 /**
1603 * Set the current time without affecting flags or fields.
1604 *
1605 * @param time The time to be set
1606 * @return the current time without recomputing.
1607 * @stable ICU 2.0
1608 */
1609 void internalSetTime(UDate time) { fTime = time; }
1610
1611 /**
1612 * The time fields containing values into which the millis is computed.
1613 * @stable ICU 2.0
1614 */
1615 int32_t fFields[UCAL_FIELD_COUNT];
1616
1617 /**
1618 * The flags which tell if a specified time field for the calendar is set.
374ca955 1619 * @deprecated ICU 2.8 use (fStamp[n]!=kUnset)
b75a7d8f
A
1620 */
1621 UBool fIsSet[UCAL_FIELD_COUNT];
1622
1623 /** Special values of stamp[]
1624 * @stable ICU 2.0
1625 */
1626 enum {
1627 kUnset = 0,
1628 kInternallySet,
1629 kMinimumUserStamp
1630 };
1631
1632 /**
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.
1636 * @stable ICU 2.0
1637 */
1638 int32_t fStamp[UCAL_FIELD_COUNT];
1639
374ca955
A
1640 /**
1641 * Subclasses may override this method to compute several fields
1642 * specific to each calendar system. These are:
1643 *
1644 * <ul><li>ERA
1645 * <li>YEAR
1646 * <li>MONTH
1647 * <li>DAY_OF_MONTH
1648 * <li>DAY_OF_YEAR
1649 * <li>EXTENDED_YEAR</ul>
1650 *
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.
1655 *
1656 * <p>In addition, subclasses should compute any subclass-specific
1657 * fields, that is, fields from BASE_FIELD_COUNT to
1658 * getFieldCount() - 1.
1659 *
1660 * <p>The default implementation in <code>Calendar</code> implements
1661 * a pure proleptic Gregorian calendar.
1662 * @internal
1663 */
1664 virtual void handleComputeFields(int32_t julianDay, UErrorCode &status);
1665
1666 /**
1667 * Return the extended year on the Gregorian calendar as computed by
1668 * <code>computeGregorianFields()</code>.
1669 * @see #computeGregorianFields
1670 * @internal
1671 */
1672 int32_t getGregorianYear() const {
1673 return fGregorianYear;
1674 }
1675
1676 /**
1677 * Return the month (0-based) on the Gregorian calendar as computed by
1678 * <code>computeGregorianFields()</code>.
1679 * @see #computeGregorianFields
1680 * @internal
1681 */
1682 int32_t getGregorianMonth() const {
1683 return fGregorianMonth;
1684 }
1685
1686 /**
1687 * Return the day of year (1-based) on the Gregorian calendar as
1688 * computed by <code>computeGregorianFields()</code>.
1689 * @see #computeGregorianFields
1690 * @internal
1691 */
1692 int32_t getGregorianDayOfYear() const {
1693 return fGregorianDayOfYear;
1694 }
1695
1696 /**
1697 * Return the day of month (1-based) on the Gregorian calendar as
1698 * computed by <code>computeGregorianFields()</code>.
1699 * @see #computeGregorianFields
1700 * @internal
1701 */
1702 int32_t getGregorianDayOfMonth() const {
1703 return fGregorianDayOfMonth;
1704 }
1705
1706 /**
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.
1709 * @internal
1710 * @internal
1711 */
1712 virtual int32_t getDefaultMonthInYear() ;
1713
1714
1715 /**
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.
1718 * @internal
1719 */
1720 virtual int32_t getDefaultDayInMonth(int32_t /*month*/);
1721
1722 //-------------------------------------------------------------------------
1723 // Protected utility methods for use by subclasses. These are very handy
1724 // for implementing add, roll, and computeFields.
1725 //-------------------------------------------------------------------------
1726
1727 /**
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
1732 * to April 30.
1733 * <p>
1734 * <b>Subclassing:</b>
1735 * <br>
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}.
1738 * <p>
1739 * <b>Note:</b>
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.
1747 * <p>
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.
1751 *
1752 * @see #getActualMinimum
1753 * @see #getActualMaximum
1754 * @stable ICU 2.0
1755 */
1756 virtual void pinField(UCalendarDateFields field, UErrorCode& status);
1757
1758 /**
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.
1767 * <p>
1768 * <b>Subclassing:</b>
1769 * <br>
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.
1774 * <p>
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.
1779 * <p>
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.
1783 *
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.
1789 *
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.
1793 *
1794 * @return The week number (one-based), or zero if the day falls before
1795 * the first week because
1796 * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek}
1797 * is more than one.
1798 *
73c04bcf 1799 * @stable ICU 2.8
374ca955
A
1800 */
1801 int32_t weekNumber(int32_t desiredDay, int32_t dayOfPeriod, int32_t dayOfWeek);
1802
1803
1804 /**
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.
1812 * <p>
1813 * <b>Subclassing:</b>
1814 * <br>
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.
1819 * <p>
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.
1823 *
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.
1827 *
1828 * @return The week number (one-based), or zero if the day falls before
1829 * the first week because
1830 * {@link #getMinimalDaysInFirstWeek getMinimalDaysInFirstWeek}
1831 * is more than one.
1832 * @internal
1833 */
1834 inline int32_t weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek);
1835
1836 /**
1837 * returns the local DOW, valid range 0..6
1838 * @internal
1839 */
1840 int32_t getLocalDOW();
1841
b75a7d8f
A
1842private:
1843
374ca955
A
1844 /**
1845 * The next available value for fStamp[]
1846 */
b75a7d8f
A
1847 int32_t fNextStamp;// = MINIMUM_USER_STAMP;
1848
1849 /**
1850 * The current time set for the calendar.
1851 */
1852 UDate fTime;
1853
1854 /**
1855 * @see #setLenient
1856 */
1857 UBool fLenient;
1858
1859 /**
1860 * Time zone affects the time calculation done by Calendar. Calendar subclasses use
1861 * the time zone data to produce the local time.
1862 */
1863 TimeZone* fZone;
1864
1865 /**
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.
1872 */
1873 UCalendarDaysOfWeek fFirstDayOfWeek;
1874 uint8_t fMinimalDaysInFirstWeek;
1875
1876 /**
1877 * Sets firstDayOfWeek and minimalDaysInFirstWeek. Called at Calendar construction
1878 * time.
1879 *
1880 * @param desiredLocale The given locale.
374ca955 1881 * @param type The calendar type identifier, e.g: gregorian, buddhist, etc.
b75a7d8f
A
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.
1885 */
374ca955 1886 void setWeekCountData(const Locale& desiredLocale, const char *type, UErrorCode& success);
b75a7d8f
A
1887
1888 /**
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.
1892 *
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.
1896 */
1897 void updateTime(UErrorCode& status);
1898
374ca955
A
1899 /**
1900 * The Gregorian year, as computed by computeGregorianFields() and
1901 * returned by getGregorianYear().
1902 */
1903 int32_t fGregorianYear;
1904
1905 /**
1906 * The Gregorian month, as computed by computeGregorianFields() and
1907 * returned by getGregorianMonth().
1908 */
1909 int32_t fGregorianMonth;
1910
1911 /**
1912 * The Gregorian day of the year, as computed by
1913 * computeGregorianFields() and returned by getGregorianDayOfYear().
1914 */
1915 int32_t fGregorianDayOfYear;
1916
1917 /**
1918 * The Gregorian day of the month, as computed by
1919 * computeGregorianFields() and returned by getGregorianDayOfMonth().
1920 */
1921 int32_t fGregorianDayOfMonth;
1922
1923 /* calculations */
1924
1925 /**
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
1929 * DOW_LOCAL fields.
1930 */
1931 void computeGregorianAndDOWFields(int32_t julianDay, UErrorCode &ec);
1932
1933 /**
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
1942 */
1943 void computeGregorianFields(int32_t julianDay, UErrorCode &ec);
1944
1945 /**
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.
1950 *
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.
1964 */
1965 void computeWeekFields(UErrorCode &ec);
1966
1967
1968 /**
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.
1972 * @see #isLenient
1973 * @see #validateField(int, int&)
1974 * @internal
1975 */
1976 void validateFields(UErrorCode &status);
1977
1978 /**
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&)
1984 * @internal
1985 */
1986 virtual void validateField(UCalendarDateFields field, UErrorCode &status);
1987
1988 /**
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&)}.
1994 * @internal
1995 */
1996 void validateField(UCalendarDateFields field, int32_t min, int32_t max, UErrorCode& status);
1997
1998 protected:
1999 /**
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.
2003 *
2004 * @param julian The given Julian date number.
2005 * @return Day number from 1..7 (SUN..SAT).
2006 * @internal
2007 */
2008 static uint8_t julianDayToDayOfWeek(double julian);
2009
2010 private:
2011 char validLocale[ULOC_FULLNAME_CAPACITY];
2012 char actualLocale[ULOC_FULLNAME_CAPACITY];
2013
b75a7d8f 2014 public:
374ca955
A
2015#if !UCONFIG_NO_SERVICE
2016 /**
b75a7d8f
A
2017 * INTERNAL FOR 2.6 -- Registration.
2018 */
2019
2020 /**
374ca955 2021 * Return a StringEnumeration over the locales available at the time of the call,
b75a7d8f
A
2022 * including registered locales.
2023 * @return a StringEnumeration over the locales available at the time of the call
2024 * @internal
2025 */
2026 static StringEnumeration* getAvailableLocales(void);
2027
2028 /**
2029 * Register a new Calendar factory. The factory will be adopted.
2030 * INTERNAL in 2.6
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
2034 * @internal
2035 */
2036 static URegistryKey registerFactory(ICUServiceFactory* toAdopt, UErrorCode& status);
2037
2038 /**
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.
2042 * INTERNAL in 2.6
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
2046 * @internal
2047 */
2048 static UBool unregister(URegistryKey key, UErrorCode& status);
374ca955 2049
b75a7d8f
A
2050 /**
2051 * Multiple Calendar Implementation
374ca955 2052 * @internal
b75a7d8f
A
2053 */
2054 friend class CalendarFactory;
2055
2056 /**
2057 * Multiple Calendar Implementation
374ca955 2058 * @internal
b75a7d8f
A
2059 */
2060 friend class CalendarService;
2061
2062 /**
2063 * Multiple Calendar Implementation
374ca955 2064 * @internal
b75a7d8f
A
2065 */
2066 friend class DefaultCalendarFactory;
374ca955 2067#endif /* !UCONFIG_NO_SERVICE */
b75a7d8f
A
2068
2069 /**
374ca955 2070 * @internal
b75a7d8f
A
2071 * @return TRUE if this calendar has a default century (i.e. 03 -> 2003)
2072 */
2073 virtual UBool haveDefaultCentury() const = 0;
2074
2075 /**
2076 * @internal
2077 * @return the start of the default century, as a UDate
2078 */
2079 virtual UDate defaultCenturyStart() const = 0;
2080 /**
374ca955 2081 * @internal
b75a7d8f
A
2082 * @return the beginning year of the default century, as a year
2083 */
2084 virtual int32_t defaultCenturyStartYear() const = 0;
374ca955
A
2085
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
73c04bcf 2090 * @stable ICU 2.8
374ca955
A
2091 */
2092 Locale getLocale(ULocDataLocaleType type, UErrorCode &status) const;
2093
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
2098 * @internal
2099 */
2100 const char* getLocaleID(ULocDataLocaleType type, UErrorCode &status) const;
2101
b75a7d8f
A
2102};
2103
2104// -------------------------------------
2105
2106inline Calendar*
2107Calendar::createInstance(TimeZone* zone, UErrorCode& errorCode)
2108{
2109 // since the Locale isn't specified, use the default locale
2110 return createInstance(zone, Locale::getDefault(), errorCode);
2111}
2112
2113// -------------------------------------
2114
374ca955 2115inline void
b75a7d8f
A
2116Calendar::roll(UCalendarDateFields field, UBool up, UErrorCode& status)
2117{
2118 roll(field, (int32_t)(up ? +1 : -1), status);
2119}
2120
2121inline void
2122Calendar::roll(EDateFields field, UBool up, UErrorCode& status)
2123{
2124 roll((UCalendarDateFields) field, up, status);
2125}
2126
374ca955 2127
b75a7d8f
A
2128// -------------------------------------
2129
2130/**
2131 * Fast method for subclasses. The caller must maintain fUserSetDSTOffset and
2132 * fUserSetZoneOffset, as well as the isSet[] array.
2133 */
2134
2135inline void
2136Calendar::internalSet(UCalendarDateFields field, int32_t value)
2137{
2138 fFields[field] = value;
374ca955
A
2139 fStamp[field] = kInternallySet;
2140 fIsSet[field] = TRUE; // Remove later
b75a7d8f
A
2141}
2142
2143inline void
2144Calendar::internalSet(EDateFields field, int32_t value)
2145{
2146 internalSet((UCalendarDateFields) field, value);
2147}
2148
374ca955
A
2149inline int32_t Calendar::weekNumber(int32_t dayOfPeriod, int32_t dayOfWeek)
2150{
2151 return weekNumber(dayOfPeriod, dayOfPeriod, dayOfWeek);
2152}
2153
2154
b75a7d8f
A
2155U_NAMESPACE_END
2156
2157#endif /* #if !UCONFIG_NO_FORMATTING */
2158
2159#endif // _CALENDAR