示例#1
0
        /// <summary>
        /// Adds the specified number of days to the value of this instance.
        /// </summary>
        /// <param name="numberOfDays">A number of whole days.
        /// The value parameter can be negative or positive.</param>
        public CivilDate AddDays(long numberOfDays)
        {
            RataDie currentValue = null;

            currentValue       = ToRataDie();
            currentValue.Days += numberOfDays;
            return(CivilDate.FromRataDie(currentValue));
        }
示例#2
0
        /// <summary>
        /// Returns a <see cref="CivilDate"/> object set to a date based on its <see cref="RataDie"/> counterpart.
        /// <remarks>See the footnote on page 384 of "Calendrical Calculations, Part II: Three Historical Calendars"
        /// by E. M. Reingold,  N. Dershowitz, and S. M. Clamen, Software--Practice and Experience, Volume 23,
        /// Number 4 (April, 1993), pages 383-404 for an explanation.</remarks>
        /// </summary>
        /// <param name="fixedDate">An <see cref="RataDie"/> object that represents the desired date.</param>
        public static CivilDate FromRataDie(RataDie fixedDate)
        {
            int       day    = 0;
            int       year   = 0;
            int       month  = 0;
            int       mlen   = 0;
            long      d0     = 0L;
            long      n400   = 0L;
            long      d1     = 0L;
            long      n100   = 0L;
            long      d2     = 0L;
            long      n4     = 0L;
            long      d3     = 0L;
            long      n1     = 0L;
            CivilDate result = null;

            d0   = fixedDate.Days - 1L;
            n400 = (int)(d0 / 146097L);
            d1   = (int)(d0 % 146097L);
            n100 = (int)(d1 / 36524L);
            d2   = (int)(d1 % 36524L);
            n4   = (int)(d2 / 1461L);
            d3   = (int)(d2 % 1461L);
            n1   = (int)(d3 / 365L);

            day  = (int)((d3 % 365L) + 1L);
            year = (int)(400L * n400 + 100L * n100 + 4L * n4 + n1);

            if (n100 == 4L || n1 == 4L)
            {
                result = new CivilDate(year, CivilMonth.December, 31);
            }
            else
            {
                year++;
                month = 1;                 // Start from January
                while ((mlen = GetDaysInMonth(year, (CivilMonth)month)) < day)
                {
                    day -= mlen;
                    month++;
                }
                result = new CivilDate(year, (CivilMonth)month, day);
            }

            return(result);
        }
示例#3
0
        /// <summary>
        /// Converts the date to the number of days elapsed between the Gregorian date 12/31/1 BC and the date.
        /// The Gregorian date Sunday, December 31, 1 BC is imaginary.
        /// </summary>
        /// <returns>The number of days elapsed since 12/31/1 BC.</returns>
        public RataDie ToRataDie()
        {
            long    julianLeapYears    = 0;
            long    centuryYears       = 0;
            long    gregorianLeapYears = 0;
            RataDie result             = null;

            result             = new RataDie();
            julianLeapYears    = (int)((Year - 1) / 4);
            centuryYears       = (int)((Year - 1) / 100);
            gregorianLeapYears = (int)((Year - 1) / 400);
            result.Days        = ((long)DayOfYear                  /* days this year */
                                  + 365L * (long)(Year - 1)        /* + days in prior years */
                                  + (long)(julianLeapYears         /* + Julian Leap years */
                                           - centuryYears          /* - century years */
                                           + gregorianLeapYears)); /* + Gregorian leap years */

            return(result);
        }