Ejemplo n.º 1
0
 public static int GetSolarTermIndex(DateTime date)
 => Sun.GetSolarTermIndex(date.AddHours(-ILocalInfoProvider.GetLocalInfoProvider <T>().TimeZone).AddHours(24));
Ejemplo n.º 2
0
 public static string GetDayTerrestrialBranchName(int solarYear, int solarMonth, int solarDay)
 => ILocalInfoProvider.GetLocalInfoProvider <T>().BranchNames[GetDayTerrestrialIndex(solarYear, solarMonth, solarDay)];
Ejemplo n.º 3
0
        ///// <summary>
        ///// 0 -> 12, 1 -> 1, 2 -> 2, 3 -> 3... 11 -> 11
        ///// </summary>
        ///// <param name="monthIndex"></param>
        ///// <returns></returns>
        //public static int GetMonth(int monthIndex) { return monthIndex == 0 ? 12 : monthIndex; }

        #endregion

        #region Day

        public static string GetSolarTermName(DateTime date)
        {
            return(ILocalInfoProvider.GetLocalInfoProvider <T>().SolarTerms[GetSolarTermIndex(date)]);
        }
Ejemplo n.º 4
0
 public static string GetDayCelestialStemName(int solarYear, int solarMonth, int solarDay)
 => ILocalInfoProvider.GetLocalInfoProvider <T>().StemNames[GetDayCelestialStemIndex(solarYear, solarMonth, solarDay)];
Ejemplo n.º 5
0
        internal static LuniSolarDate <T> CalculateLuniSolarDateFromSolarDate(int year, int month, int day)
        {
            // Sample case 1: Solar date 24/05/2000 --- Lunar date 21/04/2000
            //                  previousNewMoon = 04/05/2000
            // lunarYear = previousNewMoon.Year = 2000
            //                  newMoon11Before = 08/12/1999
            //                   newMoon11After = 26/11/2000
            //   (04/05/2000 - 08/12/1999) / 29 = 148 days / 29
            //                                  = 5.10x
            // --------------------------------------------------------------
            // Sample case 2: Solar date 01/01/2014 --- Lunar date 01/12/2013
            //                  previousNewMoon = 01/01/2014
            //             previousNewMoon.Year = 2014
            //                        lunarYear = 2013
            //                  newMoon11Before = 03/12/2013
            //                   newMoon11After = 26/11/2000
            //   (01/01/2014 - 03/12/2013) / 29 = 29 days / 29
            //                                  = 1
            // --------------------------------------------------------------
            // Sample case 3: Solar date 22/12/2033 --- Lunar date 01/11i/2033
            //                  previousNewMoon = 22/12/2033
            // lunarYear = previousNewMoon.Year = 2033
            //                  newMoon11Before = 03/12/2032
            //                   newMoon11After = 22/11/2033

            var timeZone = ILocalInfoProvider.GetLocalInfoProvider <T>().TimeZone;

            int  lunarYear;
            int  lunarMonth;
            int  lunarDay;
            bool isLeapMonth = false;

            var thisDay = new DateTime(year, month, day);

            var k = (int)((thisDay.AddHours(-timeZone).UniversalDateTimeToJulianDate() - 2415021.076998695) / Constants.SynodicMonth + 0.5);

            var previousNewMoon =
                JulianDateConverter.JulianDateToUniversalDateTime(Moon.GetNewMoon(k)).AddHours(timeZone).Date;

            while (previousNewMoon > thisDay)
            {
                previousNewMoon =
                    JulianDateConverter.JulianDateToUniversalDateTime(Moon.GetNewMoon(--k)).AddHours(timeZone).Date;
            }

            // "previous, this/current" and "next" are not used to avoid ambiguity.
            var newMoon11Before = Moon.GetNewMoon11(previousNewMoon.Year - 1, timeZone).Date;
            var newMoon11After  = Moon.GetNewMoon11(previousNewMoon.Year, timeZone).Date;

            // correcting for such cases as case 3
            if (newMoon11After < previousNewMoon)
            {
                newMoon11Before = newMoon11After;
                newMoon11After  = Moon.GetNewMoon11(previousNewMoon.Year + 1, timeZone).Date;
            }

            var isLeapYear = (newMoon11After - newMoon11Before).TotalDays > 365.0;

            var monthsFromNewMoon11Before = (int)((previousNewMoon - newMoon11Before).TotalDays / 29);

            /* Note:
             * monthsFromNewMoon11Before = 0: lunar month = 11, lunar year = previous year
             * monthsFromNewMoon11Before = 1: lunar month = 12, lunar year = previous year
             * monthsFromNewMoon11Before = 2: lunar month =  1, lunar year = this year
             * monthsFromNewMoon11Before = 3: lunar month =  2, lunar year = this year
             * and so on
             */

            // month 11 and 12 belong to lunar year of the previous year.
            if (monthsFromNewMoon11Before < 2)
            {
                lunarYear  = newMoon11After.Year - 1;
                lunarMonth = 11 + monthsFromNewMoon11Before;
            }
            else
            {
                lunarYear  = newMoon11After.Year;
                lunarMonth = monthsFromNewMoon11Before - 1;
            }

            // correcting month number if this lunar year is leap year
            if (isLeapYear)
            {
                var leapMonthIndex = LuniSolarCalendar <VietnameseLocalInfoProvider> .GetLunarYear(newMoon11After.Year).LeapMonthIndex;

                if (monthsFromNewMoon11Before >= leapMonthIndex)
                {
                    lunarMonth--;
                    if (monthsFromNewMoon11Before == leapMonthIndex)
                    {
                        isLeapMonth = true;
                    }
                }
            }

            lunarDay = (int)(thisDay - previousNewMoon).TotalDays + 1;

            return(new LuniSolarDate <T>(lunarYear, lunarMonth, isLeapMonth, lunarDay, thisDay));
        }