]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/gregocal.h
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / gregocal.h
CommitLineData
b75a7d8f
A
1/*
2* Copyright (C) {1997-2003}, International Business Machines Corporation and others. All Rights Reserved.
3********************************************************************************
4*
5* File GREGOCAL.H
6*
7* Modification History:
8*
9* Date Name Description
10* 04/22/97 aliu Overhauled header.
11* 07/28/98 stephen Sync with JDK 1.2
12* 09/04/98 stephen Re-sync with JDK 8/31 putback
13* 09/14/98 stephen Changed type of kOneDay, kOneWeek to double.
14* Fixed bug in roll()
15* 10/15/99 aliu Fixed j31, incorrect WEEK_OF_YEAR computation.
16* Added documentation of WEEK_OF_YEAR computation.
17* 10/15/99 aliu Fixed j32, cannot set date to Feb 29 2000 AD.
18* {JDK bug 4210209 4209272}
19********************************************************************************
20*/
21
22#ifndef GREGOCAL_H
23#define GREGOCAL_H
24
25#include "unicode/utypes.h"
26
27#if !UCONFIG_NO_FORMATTING
28
29#include "unicode/calendar.h"
30
31U_NAMESPACE_BEGIN
32
33/**
34 * Concrete class which provides the standard calendar used by most of the world.
35 * <P>
36 * The standard (Gregorian) calendar has 2 eras, BC and AD.
37 * <P>
38 * This implementation handles a single discontinuity, which corresponds by default to
39 * the date the Gregorian calendar was originally instituted (October 15, 1582). Not all
40 * countries adopted the Gregorian calendar then, so this cutover date may be changed by
41 * the caller.
42 * <P>
43 * Prior to the institution of the Gregorian Calendar, New Year's Day was March 25. To
44 * avoid confusion, this Calendar always uses January 1. A manual adjustment may be made
45 * if desired for dates that are prior to the Gregorian changeover and which fall
46 * between January 1 and March 24.
47 *
48 * <p>Values calculated for the <code>WEEK_OF_YEAR</code> field range from 1 to
49 * 53. Week 1 for a year is the first week that contains at least
50 * <code>getMinimalDaysInFirstWeek()</code> days from that year. It thus
51 * depends on the values of <code>getMinimalDaysInFirstWeek()</code>,
52 * <code>getFirstDayOfWeek()</code>, and the day of the week of January 1.
53 * Weeks between week 1 of one year and week 1 of the following year are
54 * numbered sequentially from 2 to 52 or 53 (as needed).
55 *
56 * <p>For example, January 1, 1998 was a Thursday. If
57 * <code>getFirstDayOfWeek()</code> is <code>MONDAY</code> and
58 * <code>getMinimalDaysInFirstWeek()</code> is 4 (these are the values
59 * reflecting ISO 8601 and many national standards), then week 1 of 1998 starts
60 * on December 29, 1997, and ends on January 4, 1998. If, however,
61 * <code>getFirstDayOfWeek()</code> is <code>SUNDAY</code>, then week 1 of 1998
62 * starts on January 4, 1998, and ends on January 10, 1998; the first three days
63 * of 1998 then are part of week 53 of 1997.
64 *
65 * <p>Example for using GregorianCalendar:
66 * <pre>
67 * \code
68 * // get the supported ids for GMT-08:00 (Pacific Standard Time)
69 * int32_t idsCount;
70 * const UnicodeString** ids = TimeZone::createAvailableIDs(-8 * 60 * 60 * 1000, idsCount);
71 * // if no ids were returned, something is wrong. get out.
72 * if (idsCount == 0) {
73 * return;
74 * }
75 *
76 * // begin output
77 * cout << "Current Time" << endl;
78 *
79 * // create a Pacific Standard Time time zone
80 * SimpleTimeZone* pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, *(ids[0]));
81 *
82 * // set up rules for daylight savings time
83 * pdt->setStartRule(Calendar::APRIL, 1, Calendar::SUNDAY, 2 * 60 * 60 * 1000);
84 * pdt->setEndRule(Calendar::OCTOBER, -1, Calendar::SUNDAY, 2 * 60 * 60 * 1000);
85 *
86 * // create a GregorianCalendar with the Pacific Daylight time zone
87 * // and the current date and time
88 * UErrorCode success = U_ZERO_ERROR;
89 * Calendar* calendar = new GregorianCalendar( pdt, success );
90 *
91 * // print out a bunch of interesting things
92 * cout << "ERA: " << calendar->get( Calendar::ERA, success ) << endl;
93 * cout << "YEAR: " << calendar->get( Calendar::YEAR, success ) << endl;
94 * cout << "MONTH: " << calendar->get( Calendar::MONTH, success ) << endl;
95 * cout << "WEEK_OF_YEAR: " << calendar->get( Calendar::WEEK_OF_YEAR, success ) << endl;
96 * cout << "WEEK_OF_MONTH: " << calendar->get( Calendar::WEEK_OF_MONTH, success ) << endl;
97 * cout << "DATE: " << calendar->get( Calendar::DATE, success ) << endl;
98 * cout << "DAY_OF_MONTH: " << calendar->get( Calendar::DAY_OF_MONTH, success ) << endl;
99 * cout << "DAY_OF_YEAR: " << calendar->get( Calendar::DAY_OF_YEAR, success ) << endl;
100 * cout << "DAY_OF_WEEK: " << calendar->get( Calendar::DAY_OF_WEEK, success ) << endl;
101 * cout << "DAY_OF_WEEK_IN_MONTH: " << calendar->get( Calendar::DAY_OF_WEEK_IN_MONTH, success ) << endl;
102 * cout << "AM_PM: " << calendar->get( Calendar::AM_PM, success ) << endl;
103 * cout << "HOUR: " << calendar->get( Calendar::HOUR, success ) << endl;
104 * cout << "HOUR_OF_DAY: " << calendar->get( Calendar::HOUR_OF_DAY, success ) << endl;
105 * cout << "MINUTE: " << calendar->get( Calendar::MINUTE, success ) << endl;
106 * cout << "SECOND: " << calendar->get( Calendar::SECOND, success ) << endl;
107 * cout << "MILLISECOND: " << calendar->get( Calendar::MILLISECOND, success ) << endl;
108 * cout << "ZONE_OFFSET: " << (calendar->get( Calendar::ZONE_OFFSET, success )/(60*60*1000)) << endl;
109 * cout << "DST_OFFSET: " << (calendar->get( Calendar::DST_OFFSET, success )/(60*60*1000)) << endl;
110 *
111 * cout << "Current Time, with hour reset to 3" << endl;
112 * calendar->clear(Calendar::HOUR_OF_DAY); // so doesn't override
113 * calendar->set(Calendar::HOUR, 3);
114 * cout << "ERA: " << calendar->get( Calendar::ERA, success ) << endl;
115 * cout << "YEAR: " << calendar->get( Calendar::YEAR, success ) << endl;
116 * cout << "MONTH: " << calendar->get( Calendar::MONTH, success ) << endl;
117 * cout << "WEEK_OF_YEAR: " << calendar->get( Calendar::WEEK_OF_YEAR, success ) << endl;
118 * cout << "WEEK_OF_MONTH: " << calendar->get( Calendar::WEEK_OF_MONTH, success ) << endl;
119 * cout << "DATE: " << calendar->get( Calendar::DATE, success ) << endl;
120 * cout << "DAY_OF_MONTH: " << calendar->get( Calendar::DAY_OF_MONTH, success ) << endl;
121 * cout << "DAY_OF_YEAR: " << calendar->get( Calendar::DAY_OF_YEAR, success ) << endl;
122 * cout << "DAY_OF_WEEK: " << calendar->get( Calendar::DAY_OF_WEEK, success ) << endl;
123 * cout << "DAY_OF_WEEK_IN_MONTH: " << calendar->get( Calendar::DAY_OF_WEEK_IN_MONTH, success ) << endl;
124 * cout << "AM_PM: " << calendar->get( Calendar::AM_PM, success ) << endl;
125 * cout << "HOUR: " << calendar->get( Calendar::HOUR, success ) << endl;
126 * cout << "HOUR_OF_DAY: " << calendar->get( Calendar::HOUR_OF_DAY, success ) << endl;
127 * cout << "MINUTE: " << calendar->get( Calendar::MINUTE, success ) << endl;
128 * cout << "SECOND: " << calendar->get( Calendar::SECOND, success ) << endl;
129 * cout << "MILLISECOND: " << calendar->get( Calendar::MILLISECOND, success ) << endl;
130 * cout << "ZONE_OFFSET: " << (calendar->get( Calendar::ZONE_OFFSET, success )/(60*60*1000)) << endl; // in hours
131 * cout << "DST_OFFSET: " << (calendar->get( Calendar::DST_OFFSET, success )/(60*60*1000)) << endl; // in hours
132 *
133 * delete[] ids;
134 * delete calendar; // also deletes pdt
135 * \endcode
136 * </pre>
137 * @stable ICU 2.0
138 */
139class U_I18N_API GregorianCalendar: public Calendar {
140public:
141
142 /**
143 * Useful constants for GregorianCalendar and TimeZone.
144 * @stable ICU 2.0
145 */
146 enum EEras {
147 BC,
148 AD
149 };
150
151 /**
152 * Constructs a default GregorianCalendar using the current time in the default time
153 * zone with the default locale.
154 *
155 * @param success Indicates the status of GregorianCalendar object construction.
156 * Returns U_ZERO_ERROR if constructed successfully.
157 * @stable ICU 2.0
158 */
159 GregorianCalendar(UErrorCode& success);
160
161 /**
162 * Constructs a GregorianCalendar based on the current time in the given time zone
163 * with the default locale. Clients are no longer responsible for deleting the given
164 * time zone object after it's adopted.
165 *
166 * @param zoneToAdopt The given timezone.
167 * @param success Indicates the status of GregorianCalendar object construction.
168 * Returns U_ZERO_ERROR if constructed successfully.
169 * @stable ICU 2.0
170 */
171 GregorianCalendar(TimeZone* zoneToAdopt, UErrorCode& success);
172
173 /**
174 * Constructs a GregorianCalendar based on the current time in the given time zone
175 * with the default locale.
176 *
177 * @param zone The given timezone.
178 * @param success Indicates the status of GregorianCalendar object construction.
179 * Returns U_ZERO_ERROR if constructed successfully.
180 * @stable ICU 2.0
181 */
182 GregorianCalendar(const TimeZone& zone, UErrorCode& success);
183
184 /**
185 * Constructs a GregorianCalendar based on the current time in the default time zone
186 * with the given locale.
187 *
188 * @param aLocale The given locale.
189 * @param success Indicates the status of GregorianCalendar object construction.
190 * Returns U_ZERO_ERROR if constructed successfully.
191 * @stable ICU 2.0
192 */
193 GregorianCalendar(const Locale& aLocale, UErrorCode& success);
194
195 /**
196 * Constructs a GregorianCalendar based on the current time in the given time zone
197 * with the given locale. Clients are no longer responsible for deleting the given
198 * time zone object after it's adopted.
199 *
200 * @param zoneToAdopt The given timezone.
201 * @param aLocale The given locale.
202 * @param success Indicates the status of GregorianCalendar object construction.
203 * Returns U_ZERO_ERROR if constructed successfully.
204 * @stable ICU 2.0
205 */
206 GregorianCalendar(TimeZone* zoneToAdopt, const Locale& aLocale, UErrorCode& success);
207
208 /**
209 * Constructs a GregorianCalendar based on the current time in the given time zone
210 * with the given locale.
211 *
212 * @param zone The given timezone.
213 * @param aLocale The given locale.
214 * @param success Indicates the status of GregorianCalendar object construction.
215 * Returns U_ZERO_ERROR if constructed successfully.
216 * @stable ICU 2.0
217 */
218 GregorianCalendar(const TimeZone& zone, const Locale& aLocale, UErrorCode& success);
219
220 /**
221 * Constructs a GregorianCalendar with the given AD date set in the default time
222 * zone with the default locale.
223 *
224 * @param year The value used to set the YEAR time field in the calendar.
225 * @param month The value used to set the MONTH time field in the calendar. Month
226 * value is 0-based. e.g., 0 for January.
227 * @param date The value used to set the DATE time field in the calendar.
228 * @param success Indicates the status of GregorianCalendar object construction.
229 * Returns U_ZERO_ERROR if constructed successfully.
230 * @stable ICU 2.0
231 */
232 GregorianCalendar(int32_t year, int32_t month, int32_t date, UErrorCode& success);
233
234 /**
235 * Constructs a GregorianCalendar with the given AD date and time set for the
236 * default time zone with the default locale.
237 *
238 * @param year The value used to set the YEAR time field in the calendar.
239 * @param month The value used to set the MONTH time field in the calendar. Month
240 * value is 0-based. e.g., 0 for January.
241 * @param date The value used to set the DATE time field in the calendar.
242 * @param hour The value used to set the HOUR_OF_DAY time field in the calendar.
243 * @param minute The value used to set the MINUTE time field in the calendar.
244 * @param success Indicates the status of GregorianCalendar object construction.
245 * Returns U_ZERO_ERROR if constructed successfully.
246 * @stable ICU 2.0
247 */
248 GregorianCalendar(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute, UErrorCode& success);
249
250 /**
251 * Constructs a GregorianCalendar with the given AD date and time set for the
252 * default time zone with the default locale.
253 *
254 * @param year The value used to set the YEAR time field in the calendar.
255 * @param month The value used to set the MONTH time field in the calendar. Month
256 * value is 0-based. e.g., 0 for January.
257 * @param date The value used to set the DATE time field in the calendar.
258 * @param hour The value used to set the HOUR_OF_DAY time field in the calendar.
259 * @param minute The value used to set the MINUTE time field in the calendar.
260 * @param second The value used to set the SECOND time field in the calendar.
261 * @param success Indicates the status of GregorianCalendar object construction.
262 * Returns U_ZERO_ERROR if constructed successfully.
263 * @stable ICU 2.0
264 */
265 GregorianCalendar(int32_t year, int32_t month, int32_t date, int32_t hour, int32_t minute, int32_t second, UErrorCode& success);
266
267 /**
268 * Destructor
269 * @stable ICU 2.0
270 */
271 virtual ~GregorianCalendar();
272
273 /**
274 * Copy constructor
275 * @param source the object to be copied.
276 * @stable ICU 2.0
277 */
278 GregorianCalendar(const GregorianCalendar& source);
279
280 /**
281 * Default assignment operator
282 * @param right the object to be copied.
283 * @stable ICU 2.0
284 */
285 GregorianCalendar& operator=(const GregorianCalendar& right);
286
287 /**
288 * Create and return a polymorphic copy of this calendar.
289 * @return return a polymorphic copy of this calendar.
290 * @stable ICU 2.0
291 */
292 virtual Calendar* clone(void) const;
293
294 /**
295 * Sets the GregorianCalendar change date. This is the point when the switch from
296 * Julian dates to Gregorian dates occurred. Default is 00:00:00 local time, October
297 * 15, 1582. Previous to this time and date will be Julian dates.
298 *
299 * @param date The given Gregorian cutover date.
300 * @param success Output param set to success/failure code on exit.
301 * @stable ICU 2.0
302 */
303 void setGregorianChange(UDate date, UErrorCode& success);
304
305 /**
306 * Gets the Gregorian Calendar change date. This is the point when the switch from
307 * Julian dates to Gregorian dates occurred. Default is 00:00:00 local time, October
308 * 15, 1582. Previous to this time and date will be Julian dates.
309 *
310 * @return The Gregorian cutover time for this calendar.
311 * @stable ICU 2.0
312 */
313 UDate getGregorianChange(void) const;
314
315 /**
316 * Return true if the given year is a leap year. Determination of whether a year is
317 * a leap year is actually very complicated. We do something crude and mostly
318 * correct here, but for a real determination you need a lot of contextual
319 * information. For example, in Sweden, the change from Julian to Gregorian happened
320 * in a complex way resulting in missed leap years and double leap years between
321 * 1700 and 1753. Another example is that after the start of the Julian calendar in
322 * 45 B.C., the leap years did not regularize until 8 A.D. This method ignores these
323 * quirks, and pays attention only to the Julian onset date and the Gregorian
324 * cutover (which can be changed).
325 *
326 * @param year The given year.
327 * @return True if the given year is a leap year; false otherwise.
328 * @stable ICU 2.0
329 */
330 UBool isLeapYear(int32_t year) const;
331
332 /**
333 * Returns TRUE if the given Calendar object is equivalent to this
334 * one. Calendar override.
335 *
336 * @param other the Calendar to be compared with this Calendar
337 * @draft ICU 2.4
338 */
339 virtual UBool isEquivalentTo(const Calendar& other) const;
340
341 /**
342 * (Overrides Calendar) UDate Arithmetic function. Adds the specified (signed) amount
343 * of time to the given time field, based on the calendar's rules. For more
344 * information, see the documentation for Calendar::add().
345 *
346 * @param field The time field.
347 * @param amount The amount of date or time to be added to the field.
348 * @param status Output param set to success/failure code on exit. If any value
349 * previously set in the time field is invalid, this will be set to
350 * an error status.
351 * @deprecated ICU 2.6. Use add(UCalendarDateFields field, int32_t amount, UErrorCode& status) instead.
352 */
353 virtual void add(EDateFields field, int32_t amount, UErrorCode& status);
354
355 /**
356 * (Overrides Calendar) UDate Arithmetic function. Adds the specified (signed) amount
357 * of time to the given time field, based on the calendar's rules. For more
358 * information, see the documentation for Calendar::add().
359 *
360 * @param field The time field.
361 * @param amount The amount of date or time to be added to the field.
362 * @param status Output param set to success/failure code on exit. If any value
363 * previously set in the time field is invalid, this will be set to
364 * an error status.
365 * @draft ICU 2.6.
366 */
367 virtual void add(UCalendarDateFields field, int32_t amount, UErrorCode& status);
368
369 /**
370 * (Overrides Calendar) Rolls up or down by the given amount in the specified field.
371 * For more information, see the documentation for Calendar::roll().
372 *
373 * @param field The time field.
374 * @param amount Indicates amount to roll.
375 * @param status Output param set to success/failure code on exit. If any value
376 * previously set in the time field is invalid, this will be set to
377 * an error status.
378 * @deprecated ICU 2.6. Use roll(UCalendarDateFields field, int32_t amount, UErrorCode& status) instead.
379 */
380 virtual void roll(EDateFields field, int32_t amount, UErrorCode& status);
381
382 /**
383 * (Overrides Calendar) Rolls up or down by the given amount in the specified field.
384 * For more information, see the documentation for Calendar::roll().
385 *
386 * @param field The time field.
387 * @param amount Indicates amount to roll.
388 * @param status Output param set to success/failure code on exit. If any value
389 * previously set in the time field is invalid, this will be set to
390 * an error status.
391 * @draft ICU 2.6.
392 */
393 virtual void roll(UCalendarDateFields field, int32_t amount, UErrorCode& status);
394
395 /**
396 * (Overrides Calendar) Returns minimum value for the given field. e.g. for
397 * Gregorian DAY_OF_MONTH, 1.
398 * @param field the time field.
399 * @return minimum value for the given field
400 * @deprecated ICU 2.6. Use getMinimum(UCalendarDateFields field) instead.
401 */
402 virtual int32_t getMinimum(EDateFields field) const;
403
404 /**
405 * (Overrides Calendar) Returns minimum value for the given field. e.g. for
406 * Gregorian DAY_OF_MONTH, 1.
407 * @param field the time field.
408 * @return minimum value for the given field
409 * @draft ICU 2.6.
410 */
411 virtual int32_t getMinimum(UCalendarDateFields field) const;
412
413 /**
414 * (Overrides Calendar) Returns maximum value for the given field. e.g. for
415 * Gregorian DAY_OF_MONTH, 31.
416 * @param field the time field.
417 * @return maximum value for the given field
418 * @deprecated ICU 2.6. Use getMaximum(UCalendarDateFields field) instead.
419 */
420 virtual int32_t getMaximum(EDateFields field) const;
421
422 /**
423 * (Overrides Calendar) Returns maximum value for the given field. e.g. for
424 * Gregorian DAY_OF_MONTH, 31.
425 * @param field the time field.
426 * @return maximum value for the given field
427 * @draft ICU 2.6.
428 */
429 virtual int32_t getMaximum(UCalendarDateFields field) const;
430
431 /**
432 * (Overrides Calendar) Returns highest minimum value for the given field if varies.
433 * Otherwise same as getMinimum(). For Gregorian, no difference.
434 * @param field the time field.
435 * @return highest minimum value for the given field if varies.
436 * Otherwise same as getMinimum().
437 * @deprecated ICU 2.6. Use getGreatestMinimum(UCalendarDateFields field) instead.
438 */
439 virtual int32_t getGreatestMinimum(EDateFields field) const;
440
441 /**
442 * (Overrides Calendar) Returns highest minimum value for the given field if varies.
443 * Otherwise same as getMinimum(). For Gregorian, no difference.
444 * @param field the time field.
445 * @return highest minimum value for the given field if varies.
446 * Otherwise same as getMinimum().
447 * @draft ICU 2.6.
448 */
449 virtual int32_t getGreatestMinimum(UCalendarDateFields field) const;
450
451 /**
452 * (Overrides Calendar) Returns lowest maximum value for the given field if varies.
453 * Otherwise same as getMaximum(). For Gregorian DAY_OF_MONTH, 28.
454 * @param field the time field.
455 * @return lowest maximum value for the given field if varies.
456 * Otherwise same as getMaximum().
457 * @deprecated ICU 2.6. Use getLeastMaximum(UCalendarDateFields field) instead.
458 */
459 virtual int32_t getLeastMaximum(EDateFields field) const;
460
461 /**
462 * (Overrides Calendar) Returns lowest maximum value for the given field if varies.
463 * Otherwise same as getMaximum(). For Gregorian DAY_OF_MONTH, 28.
464 * @param field the time field.
465 * @return lowest maximum value for the given field if varies.
466 * Otherwise same as getMaximum().
467 * @draft ICU 2.6.
468 */
469 virtual int32_t getLeastMaximum(UCalendarDateFields field) const;
470
471 /**
472 * Return the minimum value that this field could have, given the current date.
473 * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().
474 * @param field the time field.
475 * @return the minimum value that this field could have, given the current date.
476 * @deprecated ICU 2.6. Use getActualMinimum(UCalendarDateFields field) instead.
477 */
478 int32_t getActualMinimum(EDateFields field) const;
479
480 /**
481 * Return the minimum value that this field could have, given the current date.
482 * For the Gregorian calendar, this is the same as getMinimum() and getGreatestMinimum().
483 * @param field the time field.
484 * @return the minimum value that this field could have, given the current date.
485 * @draft ICU 2.6.
486 */
487 int32_t getActualMinimum(UCalendarDateFields field) const;
488
489 /**
490 * Return the maximum value that this field could have, given the current date.
491 * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual
492 * maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar,
493 * for some years the actual maximum for MONTH is 12, and for others 13.
494 * @param field the time field.
495 * @return the maximum value that this field could have, given the current date.
496 * @deprecated ICU 2.6. Use getActualMaximum(UCalendarDateFields field) instead.
497 */
498 int32_t getActualMaximum(EDateFields field) const;
499
500 /**
501 * Return the maximum value that this field could have, given the current date.
502 * For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual
503 * maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar,
504 * for some years the actual maximum for MONTH is 12, and for others 13.
505 * @param field the time field.
506 * @return the maximum value that this field could have, given the current date.
507 * @draft ICU 2.6.
508 */
509 int32_t getActualMaximum(UCalendarDateFields field) const;
510
511 /**
512 * (Overrides Calendar) Return true if the current date for this Calendar is in
513 * Daylight Savings Time. Recognizes DST_OFFSET, if it is set.
514 *
515 * @param status Fill-in parameter which receives the status of this operation.
516 * @return True if the current date for this Calendar is in Daylight Savings Time,
517 * false, otherwise.
518 * @stable ICU 2.0
519 */
520 virtual UBool inDaylightTime(UErrorCode& status) const;
521
522public:
523
524 /**
525 * Override Calendar Returns a unique class ID POLYMORPHICALLY. Pure virtual
526 * override. This method is to implement a simple version of RTTI, since not all C++
527 * compilers support genuine RTTI. Polymorphic operator==() and clone() methods call
528 * this method.
529 *
530 * @return The class ID for this object. All objects of a given class have the
531 * same class ID. Objects of other classes have different class IDs.
532 * @stable ICU 2.0
533 */
534 virtual UClassID getDynamicClassID(void) const;
535
536 /**
537 * Return the class ID for this class. This is useful only for comparing to a return
538 * value from getDynamicClassID(). For example:
539 *
540 * Base* polymorphic_pointer = createPolymorphicObject();
541 * if (polymorphic_pointer->getDynamicClassID() ==
542 * Derived::getStaticClassID()) ...
543 *
544 * @return The class ID for all objects of this class.
545 * @stable ICU 2.0
546 */
547 static inline UClassID getStaticClassID(void);
548
549 /**
550 * Get the calendar type, "gregorian", for use in DateFormatSymbols.
551 *
552 * @return calendar type
553 * @internal
554 */
555 virtual const char * getType() const;
556
557protected:
558
559 /**
560 * Called by computeFields. Converts calendar's year into Gregorian Extended Year (where negative = BC)
561 * @return Current year in Gregorian years, where -3 means 4 BC (1-bcyear)
562 * @internal
563 */
564 virtual int32_t getGregorianYear(UErrorCode &status) const;
565
566 /**
567 * Called by computeJulianDay. Returns the default month (0-based) for the year,
568 * taking year and era into account. Defaults to 0 for Gregorian, which doesn't care.
569 * @internal
570 */
571 virtual inline int32_t getDefaultMonthInYear() const { return 0; }
572
573
574 /**
575 * Called by computeJulianDay. Returns the default day (1-based) for the month,
576 * taking currently-set year and era into account. Defaults to 1 for Gregorian, which doesn't care.
577 * @internal
578 */
579 virtual inline int32_t getDefaultDayInMonth(int32_t /*month*/) const { return 1; }
580
581 /**
582 * (Overrides Calendar) Converts GMT as milliseconds to time field values.
583 * @param status Fill-in parameter which receives the status of this operation.
584 * @stable ICU 2.0
585 */
586 virtual void computeFields(UErrorCode& status);
587
588 /**
589 * (Overrides Calendar) Converts Calendar's time field values to GMT as
590 * milliseconds.
591 *
592 * @param status Output param set to success/failure code on exit. If any value
593 * previously set in the time field is invalid, this will be set to
594 * an error status.
595 * @stable ICU 2.0
596 */
597 virtual void computeTime(UErrorCode& status);
598
599 private:
600 GregorianCalendar(); // default constructor not implemented
601
602 protected:
603 /**
604 * Return the ERA. We need a special method for this because the
605 * default ERA is AD, but a zero (unset) ERA is BC.
606 * @return the ERA.
607 * @internal
608 */
609 virtual int32_t internalGetEra() const;
610
611 /**
612 * return the length of the given month.
613 * @param month the given month.
614 * @return the length of the given month.
615 * @internal
616 */
617 virtual int32_t monthLength(int32_t month) const;
618
619 /**
620 * return the length of the month according to the given year.
621 * @param month the given month.
622 * @param year the given year.
623 * @return the length of the month
624 * @internal
625 */
626 virtual int32_t monthLength(int32_t month, int32_t year) const;
627
628 /**
629 * return the length of the given year.
630 * @param year the given year.
631 * @return the length of the given year.
632 * @internal
633 */
634 int32_t yearLength(int32_t year) const;
635
636 /**
637 * return the length of the year field.
638 * @return the length of the year field
639 * @internal
640 */
641 int32_t yearLength(void) const;
642
643 /**
644 * After adjustments such as add(MONTH), add(YEAR), we don't want the
645 * month to jump around. E.g., we don't want Jan 31 + 1 month to go to Mar
646 * 3, we want it to go to Feb 28. Adjustments which might run into this
647 * problem call this method to retain the proper month.
648 * @internal
649 */
650 void pinDayOfMonth(void);
651
652 /**
653 * Return the day number with respect to the epoch. January 1, 1970 (Gregorian)
654 * is day zero.
655 * @param status Fill-in parameter which receives the status of this operation.
656 * @return the day number with respect to the epoch.
657 * @internal
658 */
659 virtual UDate getEpochDay(UErrorCode& status);
660
661 /**
662 * Compute the date-based fields given the milliseconds since the epoch start. Do
663 * not compute the time-based fields (HOUR, MINUTE, etc.).
664 *
665 * @param theTime the time in wall millis (either Standard or DST),
666 * whichever is in effect
667 * @param quick if true, only compute the ERA, YEAR, MONTH, DATE,
668 * DAY_OF_WEEK, and DAY_OF_YEAR.
669 * @param status Fill-in parameter which receives the status of this operation.
670 * @internal
671 */
672 virtual void timeToFields(UDate theTime, UBool quick, UErrorCode& status);
673
674 private:
675 /**
676 * Compute the julian day number of the given year.
677 * @param isGregorian if true, using Gregorian calendar, otherwise using Julian calendar
678 * @param year the given year.
679 * @param isLeap true if the year is a leap year.
680 * @return
681 */
682 static double computeJulianDayOfYear(UBool isGregorian, int32_t year,
683 UBool& isLeap);
684
685 /**
686 * Compute the day of week, relative to the first day of week, from
687 * 0..6, of the current DOW_LOCAL or DAY_OF_WEEK fields. This is
688 * equivalent to get(DOW_LOCAL) - 1.
689 * @return the day of week, relative to the first day of week.
690 */
691 int32_t computeRelativeDOW() const;
692
693 /**
694 * Compute the day of week, relative to the first day of week,
695 * from 0..6 of the given julian day.
696 * @param julianDay the given julian day.
697 * @return the day of week, relative to the first day of week.
698 */
699 int32_t computeRelativeDOW(double julianDay) const;
700
701 /**
702 * Compute the DOY using the WEEK_OF_YEAR field and the julian day
703 * of the day BEFORE January 1 of a year (a return value from
704 * computeJulianDayOfYear).
705 * @param julianDayOfYear the given julian day of the day BEFORE
706 * January 1 of a year.
707 * @return the DOY using the WEEK_OF_YEAR field.
708 */
709 int32_t computeDOYfromWOY(double julianDayOfYear) const;
710
711 /**
712 * Compute the Julian day number under either the Gregorian or the
713 * Julian calendar, using the given year and the remaining fields.
714 * @param isGregorian if true, use the Gregorian calendar
715 * @param year the adjusted year number, with 0 indicating the
716 * year 1 BC, -1 indicating 2 BC, etc.
717 * @return the Julian day number
718 */
719 double computeJulianDay(UBool isGregorian, int32_t year);
720
721
722 /**
723 * Return the week number of a day, within a period. This may be the week number in
724 * a year, or the week number in a month. Usually this will be a value >= 1, but if
725 * some initial days of the period are excluded from week 1, because
726 * minimalDaysInFirstWeek is > 1, then the week number will be zero for those
727 * initial days. Requires the day of week for the given date in order to determine
728 * the day of week of the first day of the period.
729 *
730 * @param date Day-of-year or day-of-month. Should be 1 for first day of period.
731 * @param day Day-of-week for given dayOfPeriod. 1-based with 1=Sunday.
732 * @return Week number, one-based, or zero if the day falls in part of the
733 * month before the first week, when there are days before the first
734 * week because the minimum days in the first week is more than one.
735 */
736 int32_t weekNumber(int32_t date, int32_t day);
737
738 /**
739 * Validates the values of the set time fields. True if they're all valid.
740 * @return True if the set time fields are all valid.
741 */
742 UBool validateFields(void) const;
743
744 /**
745 * Validates the value of the given time field. True if it's valid.
746 */
747 UBool boundsCheck(int32_t value, UCalendarDateFields field) const;
748
749 /**
750 * Return the pseudo-time-stamp for two fields, given their
751 * individual pseudo-time-stamps. If either of the fields
752 * is unset, then the aggregate is unset. Otherwise, the
753 * aggregate is the later of the two stamps.
754 * @param stamp_a One given field.
755 * @param stamp_b Another given field.
756 * @return the pseudo-time-stamp for two fields
757 */
758 int32_t aggregateStamp(int32_t stamp_a, int32_t stamp_b);
759
760 /**
761 * The point at which the Gregorian calendar rules are used, measured in
762 * milliseconds from the standard epoch. Default is October 15, 1582
763 * (Gregorian) 00:00:00 UTC, that is, October 4, 1582 (Julian) is followed
764 * by October 15, 1582 (Gregorian). This corresponds to Julian day number
765 * 2299161.
766 */
767 // This is measured from the standard epoch, not in Julian Days.
768 UDate fGregorianCutover;
769
770 /**
771 * Midnight, local time (using this Calendar's TimeZone) at or before the
772 * gregorianCutover. This is a pure date value with no time of day or
773 * timezone component.
774 */
775 UDate fNormalizedGregorianCutover;// = gregorianCutover;
776
777 /**
778 * The year of the gregorianCutover, with 0 representing
779 * 1 BC, -1 representing 2 BC, etc.
780 */
781 int32_t fGregorianCutoverYear;// = 1582;
782
783 static const char fgClassID;
784
785 /**
786 * Converts time as milliseconds to Julian date. The Julian date used here is not a
787 * true Julian date, since it is measured from midnight, not noon.
788 *
789 * @param millis The given milliseconds.
790 * @return The Julian date number.
791 */
792 static double millisToJulianDay(UDate millis);
793
794 /**
795 * Converts Julian date to time as milliseconds. The Julian date used here is not a
796 * true Julian date, since it is measured from midnight, not noon.
797 *
798 * @param julian The given Julian date number.
799 * @return Time as milliseconds.
800 */
801 static UDate julianDayToMillis(double julian);
802
803 /**
804 * Convert a quasi Julian date to the day of the week. The Julian date used here is
805 * not a true Julian date, since it is measured from midnight, not noon. Return
806 * value is one-based.
807 *
808 * @param julian The given Julian date number.
809 * @return Day number from 1..7 (SUN..SAT).
810 */
811 static uint8_t julianDayToDayOfWeek(double julian);
812
813 /**
814 * Divide two long integers, returning the floor of the quotient.
815 * <p>
816 * Unlike the built-in division, this is mathematically well-behaved.
817 * E.g., <code>-1/4</code> => 0
818 * but <code>floorDivide(-1,4)</code> => -1.
819 * @param numerator the numerator
820 * @param denominator a divisor which must be > 0
821 * @return the floor of the quotient.
822 */
823 static double floorDivide(double numerator, double denominator);
824
825 /**
826 * Divide two integers, returning the floor of the quotient.
827 * <p>
828 * Unlike the built-in division, this is mathematically well-behaved.
829 * E.g., <code>-1/4</code> => 0
830 * but <code>floorDivide(-1,4)</code> => -1.
831 * @param numerator the numerator
832 * @param denominator a divisor which must be > 0
833 * @return the floor of the quotient.
834 */
835 static int32_t floorDivide(int32_t numerator, int32_t denominator);
836
837 /**
838 * Divide two integers, returning the floor of the quotient, and
839 * the modulus remainder.
840 * <p>
841 * Unlike the built-in division, this is mathematically well-behaved.
842 * E.g., <code>-1/4</code> => 0 and <code>-1%4</code> => -1,
843 * but <code>floorDivide(-1,4)</code> => -1 with <code>remainder[0]</code> => 3.
844 * @param numerator the numerator
845 * @param denominator a divisor which must be > 0
846 * @param remainder an array of at least one element in which the value
847 * <code>numerator mod denominator</code> is returned. Unlike <code>numerator
848 * % denominator</code>, this will always be non-negative.
849 * @return the floor of the quotient.
850 */
851 static int32_t floorDivide(int32_t numerator, int32_t denominator, int32_t remainder[]);
852
853 /**
854 * Divide two integers, returning the floor of the quotient, and
855 * the modulus remainder.
856 * <p>
857 * Unlike the built-in division, this is mathematically well-behaved.
858 * E.g., <code>-1/4</code> => 0 and <code>-1%4</code> => -1,
859 * but <code>floorDivide(-1,4)</code> => -1 with <code>remainder[0]</code> => 3.
860 * @param numerator the numerator
861 * @param denominator a divisor which must be > 0
862 * @param remainder an array of at least one element in which the value
863 * <code>numerator mod denominator</code> is returned. Unlike <code>numerator
864 * % denominator</code>, this will always be non-negative.
865 * @return the floor of the quotient.
866 */
867 static int32_t floorDivide(double numerator, int32_t denominator, int32_t remainder[]);
868
869 public: // internal implementation
870
871 /**
872 * @internal
873 * @return TRUE if this calendar has the notion of a default century
874 */
875 virtual UBool haveDefaultCentury() const;
876
877 /**
878 * @internal
879 * @return the start of the default century
880 */
881 virtual UDate defaultCenturyStart() const;
882
883 /**
884 * @internal
885 * @return the beginning year of the default century
886 */
887 virtual int32_t defaultCenturyStartYear() const;
888
889 private:
890 /**
891 * The system maintains a static default century start date. This is initialized
892 * the first time it is used. Before then, it is set to SYSTEM_DEFAULT_CENTURY to
893 * indicate an uninitialized state. Once the system default century date and year
894 * are set, they do not change.
895 */
896 static UDate fgSystemDefaultCenturyStart;
897
898 /**
899 * See documentation for systemDefaultCenturyStart.
900 */
901 static int32_t fgSystemDefaultCenturyStartYear;
902
903 /**
904 * Default value that indicates the defaultCenturyStartYear is unitialized
905 */
906 static const int32_t fgSystemDefaultCenturyYear;
907
908 /**
909 * TODO: (ICU 2.8) use this value instead of SimpleDateFormat::fgSystemDefaultCentury
910 */
911 //static const UDate fgSystemDefaultCentury;
912
913 /**
914 * Returns the beginning date of the 100-year window that dates with 2-digit years
915 * are considered to fall within.
916 * @return the beginning date of the 100-year window that dates with 2-digit years
917 * are considered to fall within.
918 */
919 UDate internalGetDefaultCenturyStart(void) const;
920
921 /**
922 * Returns the first year of the 100-year window that dates with 2-digit years
923 * are considered to fall within.
924 * @return the first year of the 100-year window that dates with 2-digit years
925 * are considered to fall within.
926 */
927 int32_t internalGetDefaultCenturyStartYear(void) const;
928
929 /**
930 * Initializes the 100-year window that dates with 2-digit years are considered
931 * to fall within so that its start date is 80 years before the current time.
932 */
933 static void initializeSystemDefaultCentury(void);
934
935};
936
937inline UClassID
938GregorianCalendar::getStaticClassID(void)
939{ return (UClassID)&fgClassID; }
940
941inline UClassID
942GregorianCalendar::getDynamicClassID(void) const
943{ return GregorianCalendar::getStaticClassID(); }
944
945inline uint8_t GregorianCalendar::julianDayToDayOfWeek(double julian)
946{
947 // If julian is negative, then julian%7 will be negative, so we adjust
948 // accordingly. We add 1 because Julian day 0 is Monday.
949 int8_t dayOfWeek = (int8_t) uprv_fmod(julian + 1, 7);
950
951 uint8_t result = (uint8_t)(dayOfWeek + ((dayOfWeek < 0) ? (7 + UCAL_SUNDAY) : UCAL_SUNDAY));
952 return result;
953}
954
955U_NAMESPACE_END
956
957#endif /* #if !UCONFIG_NO_FORMATTING */
958
959#endif // _GREGOCAL
960//eof
961