47 * The standard (Gregorian) calendar has 2 eras, BC and AD. 48 *
49 * This implementation handles a single discontinuity, which corresponds by default to 50 * the date the Gregorian calendar was originally instituted (October 15, 1582). Not all 51 * countries adopted the Gregorian calendar then, so this cutover date may be changed by 52 * the caller. 53 *
54 * Prior to the institution of the Gregorian Calendar, New Year's Day was March 25. To 55 * avoid confusion, this Calendar always uses January 1. A manual adjustment may be made 56 * if desired for dates that are prior to the Gregorian changeover and which fall 57 * between January 1 and March 24. 58 * 59 *
Values calculated for the WEEK_OF_YEAR field range from 1 to 60 * 53. Week 1 for a year is the first week that contains at least 61 * getMinimalDaysInFirstWeek() days from that year. It thus 62 * depends on the values of getMinimalDaysInFirstWeek(), 63 * getFirstDayOfWeek(), and the day of the week of January 1. 64 * Weeks between week 1 of one year and week 1 of the following year are 65 * numbered sequentially from 2 to 52 or 53 (as needed). 66 * 67 *
WEEK_OF_YEAR
getMinimalDaysInFirstWeek()
getFirstDayOfWeek()
For example, January 1, 1998 was a Thursday. If 68 * getFirstDayOfWeek() is MONDAY and 69 * getMinimalDaysInFirstWeek() is 4 (these are the values 70 * reflecting ISO 8601 and many national standards), then week 1 of 1998 starts 71 * on December 29, 1997, and ends on January 4, 1998. If, however, 72 * getFirstDayOfWeek() is SUNDAY, then week 1 of 1998 73 * starts on January 4, 1998, and ends on January 10, 1998; the first three days 74 * of 1998 then are part of week 53 of 1997. 75 * 76 *
MONDAY
SUNDAY
Example for using GregorianCalendar: 77 *
78 * \code 79 * // get the supported ids for GMT-08:00 (Pacific Standard Time) 80 * UErrorCode success = U_ZERO_ERROR; 81 * const StringEnumeration *ids = TimeZone::createEnumeration(-8 * 60 * 60 * 1000, success); 82 * // if no ids were returned, something is wrong. get out. 83 * if (U_FAILURE(success)) { 84 * return; 85 * } 86 * 87 * // begin output 88 * cout << "Current Time" << endl; 89 * 90 * // create a Pacific Standard Time time zone 91 * SimpleTimeZone* pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids->unext(nullptr, success))); 92 * 93 * // set up rules for daylight savings time 94 * pdt->setStartRule(UCAL_MARCH, 1, UCAL_SUNDAY, 2 * 60 * 60 * 1000); 95 * pdt->setEndRule(UCAL_NOVEMBER, 2, UCAL_SUNDAY, 2 * 60 * 60 * 1000); 96 * 97 * // create a GregorianCalendar with the Pacific Daylight time zone 98 * // and the current date and time 99 * Calendar* calendar = new GregorianCalendar( pdt, success ); 100 * 101 * // print out a bunch of interesting things 102 * cout << "ERA: " << calendar->get( UCAL_ERA, success ) << endl; 103 * cout << "YEAR: " << calendar->get( UCAL_YEAR, success ) << endl; 104 * cout << "MONTH: " << calendar->get( UCAL_MONTH, success ) << endl; 105 * cout << "WEEK_OF_YEAR: " << calendar->get( UCAL_WEEK_OF_YEAR, success ) << endl; 106 * cout << "WEEK_OF_MONTH: " << calendar->get( UCAL_WEEK_OF_MONTH, success ) << endl; 107 * cout << "DATE: " << calendar->get( UCAL_DATE, success ) << endl; 108 * cout << "DAY_OF_MONTH: " << calendar->get( UCAL_DAY_OF_MONTH, success ) << endl; 109 * cout << "DAY_OF_YEAR: " << calendar->get( UCAL_DAY_OF_YEAR, success ) << endl; 110 * cout << "DAY_OF_WEEK: " << calendar->get( UCAL_DAY_OF_WEEK, success ) << endl; 111 * cout << "DAY_OF_WEEK_IN_MONTH: " << calendar->get( UCAL_DAY_OF_WEEK_IN_MONTH, success ) << endl; 112 * cout << "AM_PM: " << calendar->get( UCAL_AM_PM, success ) << endl; 113 * cout << "HOUR: " << calendar->get( UCAL_HOUR, success ) << endl; 114 * cout << "HOUR_OF_DAY: " << calendar->get( UCAL_HOUR_OF_DAY, success ) << endl; 115 * cout << "MINUTE: " << calendar->get( UCAL_MINUTE, success ) << endl; 116 * cout << "SECOND: " << calendar->get( UCAL_SECOND, success ) << endl; 117 * cout << "MILLISECOND: " << calendar->get( UCAL_MILLISECOND, success ) << endl; 118 * cout << "ZONE_OFFSET: " << (calendar->get( UCAL_ZONE_OFFSET, success )/(60*60*1000)) << endl; 119 * cout << "DST_OFFSET: " << (calendar->get( UCAL_DST_OFFSET, success )/(60*60*1000)) << endl; 120 * 121 * cout << "Current Time, with hour reset to 3" << endl; 122 * calendar->clear(UCAL_HOUR_OF_DAY); // so doesn't override 123 * calendar->set(UCAL_HOUR, 3); 124 * cout << "ERA: " << calendar->get( UCAL_ERA, success ) << endl; 125 * cout << "YEAR: " << calendar->get( UCAL_YEAR, success ) << endl; 126 * cout << "MONTH: " << calendar->get( UCAL_MONTH, success ) << endl; 127 * cout << "WEEK_OF_YEAR: " << calendar->get( UCAL_WEEK_OF_YEAR, success ) << endl; 128 * cout << "WEEK_OF_MONTH: " << calendar->get( UCAL_WEEK_OF_MONTH, success ) << endl; 129 * cout << "DATE: " << calendar->get( UCAL_DATE, success ) << endl; 130 * cout << "DAY_OF_MONTH: " << calendar->get( UCAL_DAY_OF_MONTH, success ) << endl; 131 * cout << "DAY_OF_YEAR: " << calendar->get( UCAL_DAY_OF_YEAR, success ) << endl; 132 * cout << "DAY_OF_WEEK: " << calendar->get( UCAL_DAY_OF_WEEK, success ) << endl; 133 * cout << "DAY_OF_WEEK_IN_MONTH: " << calendar->get( UCAL_DAY_OF_WEEK_IN_MONTH, success ) << endl; 134 * cout << "AM_PM: " << calendar->get( UCAL_AM_PM, success ) << endl; 135 * cout << "HOUR: " << calendar->get( UCAL_HOUR, success ) << endl; 136 * cout << "HOUR_OF_DAY: " << calendar->get( UCAL_HOUR_OF_DAY, success ) << endl; 137 * cout << "MINUTE: " << calendar->get( UCAL_MINUTE, success ) << endl; 138 * cout << "SECOND: " << calendar->get( UCAL_SECOND, success ) << endl; 139 * cout << "MILLISECOND: " << calendar->get( UCAL_MILLISECOND, success ) << endl; 140 * cout << "ZONE_OFFSET: " << (calendar->get( UCAL_ZONE_OFFSET, success )/(60*60*1000)) << endl; // in hours 141 * cout << "DST_OFFSET: " << (calendar->get( UCAL_DST_OFFSET, success )/(60*60*1000)) << endl; // in hours 142 * 143 * if (U_FAILURE(success)) { 144 * cout << "An error occurred. success=" << u_errorName(success) << endl; 145 * } 146 * 147 * delete ids; 148 * delete calendar; // also deletes pdt 149 * \endcode 150 *
UCAL_ERA 563 * UCAL_YEAR 564 * UCAL_MONTH 565 * UCAL_WEEK_OF_YEAR 566 * UCAL_WEEK_OF_MONTH 567 * UCAL_DATE (DAY_OF_MONTH on Java) 568 * UCAL_DAY_OF_YEAR 569 * UCAL_DAY_OF_WEEK_IN_MONTH 570 * UCAL_YEAR_WOY 571 * UCAL_EXTENDED_YEAR
MINIMUM
GREATEST_MINIMUM
LEAST_MAXIMUM
MAXIMUM
The GregorianCalendar implementation implements 613 * a calendar with the specified Julian/Gregorian cutover date. 614 * @internal 615 */ 616 virtual void handleComputeFields(int32_t julianDay, UErrorCode &status) override; 617 618 private: 619 /** 620 * Compute the julian day number of the given year. 621 * @param isGregorian if true, using Gregorian calendar, otherwise using Julian calendar 622 * @param year the given year. 623 * @param isLeap true if the year is a leap year. 624 * @return 625 */ 626 static double computeJulianDayOfYear(UBool isGregorian, int32_t year, 627 UBool& isLeap); 628 629 /** 630 * Validates the values of the set time fields. True if they're all valid. 631 * @return True if the set time fields are all valid. 632 */ 633 UBool validateFields(void) const; 634 635 /** 636 * Validates the value of the given time field. True if it's valid. 637 */ 638 UBool boundsCheck(int32_t value, UCalendarDateFields field) const; 639 640 /** 641 * Return the pseudo-time-stamp for two fields, given their 642 * individual pseudo-time-stamps. If either of the fields 643 * is unset, then the aggregate is unset. Otherwise, the 644 * aggregate is the later of the two stamps. 645 * @param stamp_a One given field. 646 * @param stamp_b Another given field. 647 * @return the pseudo-time-stamp for two fields 648 */ 649 int32_t aggregateStamp(int32_t stamp_a, int32_t stamp_b); 650 651 /** 652 * The point at which the Gregorian calendar rules are used, measured in 653 * milliseconds from the standard epoch. Default is October 15, 1582 654 * (Gregorian) 00:00:00 UTC, that is, October 4, 1582 (Julian) is followed 655 * by October 15, 1582 (Gregorian). This corresponds to Julian day number 656 * 2299161. This is measured from the standard epoch, not in Julian Days. 657 */ 658 UDate fGregorianCutover; 659 660 /** 661 * Julian day number of the Gregorian cutover 662 */ 663 int32_t fCutoverJulianDay; 664 665 /** 666 * Midnight, local time (using this Calendar's TimeZone) at or before the 667 * gregorianCutover. This is a pure date value with no time of day or 668 * timezone component. 669 */ 670 UDate fNormalizedGregorianCutover;// = gregorianCutover; 671 672 /** 673 * The year of the gregorianCutover, with 0 representing 674 * 1 BC, -1 representing 2 BC, etc. 675 */ 676 int32_t fGregorianCutoverYear;// = 1582; 677 678 /** 679 * Converts time as milliseconds to Julian date. The Julian date used here is not a 680 * true Julian date, since it is measured from midnight, not noon. 681 * 682 * @param millis The given milliseconds. 683 * @return The Julian date number. 684 */ 685 static double millisToJulianDay(UDate millis); 686 687 /** 688 * Converts Julian date to time as milliseconds. The Julian date used here is not a 689 * true Julian date, since it is measured from midnight, not noon. 690 * 691 * @param julian The given Julian date number. 692 * @return Time as milliseconds. 693 */ 694 static UDate julianDayToMillis(double julian); 695 696 /** 697 * Used by handleComputeJulianDay() and handleComputeMonthStart(). 698 * Temporary field indicating whether the calendar is currently Gregorian as opposed to Julian. 699 */ 700 UBool fIsGregorian; 701 702 /** 703 * Used by handleComputeJulianDay() and handleComputeMonthStart(). 704 * Temporary field indicating that the sense of the gregorian cutover should be inverted 705 * to handle certain calculations on and around the cutover date. 706 */ 707 UBool fInvertGregorian; 708 709 710 public: // internal implementation 711 712 /** 713 * @return true if this calendar has the notion of a default century 714 * @internal 715 */ 716 virtual UBool haveDefaultCentury() const override; 717 718 /** 719 * @return the start of the default century 720 * @internal 721 */ 722 virtual UDate defaultCenturyStart() const override; 723 724 /** 725 * @return the beginning year of the default century 726 * @internal 727 */ 728 virtual int32_t defaultCenturyStartYear() const override; 729 }; 730 731 U_NAMESPACE_END 732 733 #endif /* #if !UCONFIG_NO_FORMATTING */ 734 735 #endif /* U_SHOW_CPLUSPLUS_API */ 736 737 #endif // _GREGOCAL 738 //eof 739