System.Globalization.HebrewCalendar.GetDatePart C# (CSharp) Method

GetDatePart() private method

private GetDatePart ( long ticks, int part ) : int
ticks long
part int
return int
        internal virtual int GetDatePart(long ticks, int part) {
            // The Gregorian year, month, day value for ticks.
            int gregorianYear, gregorianMonth, gregorianDay;
            int hebrewYearType;                // lunar year type
            long AbsoluteDate;                // absolute date - absolute date 1/1/1600

            //
            //  Make sure we have a valid Gregorian date that will fit into our
            //  Hebrew conversion limits.
            //
            CheckTicksRange(ticks);

            DateTime time = new DateTime(ticks);

            //
            //  Save the Gregorian date values.
            //
            gregorianYear = time.Year;
            gregorianMonth = time.Month;
            gregorianDay = time.Day;

            __DateBuffer lunarDate = new __DateBuffer();    // lunar month and day for Jan 1

            // From the table looking-up value of m_HebrewTable[index] (stored in lunarDate.day), we get the the
            // lunar month and lunar day where the Gregorian date 1/1 falls.
            lunarDate.year = gregorianYear + HebrewYearOf1AD;
            hebrewYearType = GetLunarMonthDay(gregorianYear, lunarDate);

            // This is the buffer used to store the result Hebrew date.
            __DateBuffer result = new __DateBuffer();

            //
            //  Store the values for the start of the new year - 1/1.
            //
            result.year  = lunarDate.year;
            result.month = lunarDate.month;
            result.day   = lunarDate.day;

            //
            //  Get the absolute date from 1/1/1600.
            //
            AbsoluteDate = GregorianCalendar.GetAbsoluteDate(gregorianYear, gregorianMonth, gregorianDay);

            //
            //  If the requested date was 1/1, then we're done.
            //
            if ((gregorianMonth == 1) && (gregorianDay == 1)) {
                return (GetResult(result, part));
            }

            //
            //  Calculate the number of days between 1/1 and the requested date.
            //
            long NumDays;                      // number of days since 1/1
            NumDays = AbsoluteDate - GregorianCalendar.GetAbsoluteDate(gregorianYear, 1, 1);

            //
            //  If the requested date is within the current lunar month, then
            //  we're done.
            //
            if ((NumDays + (long)lunarDate.day) <= (long)(m_lunarMonthLen[hebrewYearType, lunarDate.month])) {
                result.day += (int)NumDays;
                return (GetResult(result, part));
            }

            //
            //  Adjust for the current partial month.
            //
            result.month++;
            result.day = 1;

            //
            //  Adjust the Lunar Month and Year (if necessary) based on the number
            //  of days between 1/1 and the requested date.
            //
            //  Assumes Jan 1 can never translate to the last Lunar month, which
            //  is true.
            //
            NumDays -= (long)(m_lunarMonthLen[hebrewYearType, lunarDate.month] - lunarDate.day);
            BCLDebug.Assert(NumDays >= 1, "NumDays >= 1");

            // If NumDays is 1, then we are done.  Otherwise, find the correct Hebrew month
            // and day.
            if (NumDays > 1) {
                //
                //  See if we're on the correct Lunar month.
                //
                while (NumDays > (long)(m_lunarMonthLen[hebrewYearType, result.month])) {
                    //
                    //  Adjust the number of days and move to the next month.
                    //
                    NumDays -= (long)(m_lunarMonthLen[hebrewYearType, result.month++]);

                    //
                    //  See if we need to adjust the Year.
                    //  Must handle both 12 and 13 month years.
                    //
                    if ((result.month > 13) || (m_lunarMonthLen[hebrewYearType, result.month] == 0)) {
                        //
                        //  Adjust the Year.
                        //
                        result.year++;
                        hebrewYearType = m_HebrewTable[(gregorianYear + 1 - FirstGregorianTableYear) * 2 + 1];

                        //
                        //  Adjust the Month.
                        //
                        result.month = 1;
                    }
                }
                //
                //  Found the right Lunar month.
                //
                result.day += (int)(NumDays - 1);
            }
            return (GetResult(result, part));
        }