Exemplo n.º 1
0
        /// <inheritdoc/>
        public override bool IsThisFestival(DateTime date)
        {
            var cdate    = ChineseDate.From(date);
            var festival = ChineseDate.From(cdate.Year, Month, Day);

            return(cdate == festival);
        }
Exemplo n.º 2
0
 /// <inheritdoc/>
 protected override bool TryGetDate(int year, int month, int day, out DateTime date)
 {
     try
     {
         date = ChineseDate.From(year, month, day).ToDate();
         return(true);
     }
     catch (Exception)
     {
         date = DateTime.Now;
         return(false);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// 指定年月日索引,月/日可以为负数,负数表示倒数
        /// </summary>
        /// <param name="year">年份,范围为1901-2100</param>
        /// <param name="month">月份,允许值:1-12(当年不含闰月),1-13(当年含闰月)</param>
        /// <param name="day">日期,允许值:1-30(大月),1-29(小月)</param>
        /// <exception cref="ArgumentOutOfRangeException">日期超出范围</exception>
        /// <returns>农历日期</returns>
        public static ChineseDate FromIndex(int year, int month, int day)
        {
            if (year < 1901 || year > 2100)
            {
                throw new ArgumentOutOfRangeException($"年份超出范围 1901 -- 2100");
            }
            var months = chineseCalendar.GetMonthsInYear(year);

            if (month == 0 || month > months)
            {
                throw new ArgumentOutOfRangeException($"{year}年,月份允许范围为 1 -- {months}");
            }
            if (month < 0 - months)
            {
                throw new ArgumentOutOfRangeException($"{year}年,月份允许范围为 -1 -- {0 - months}");
            }
            if (month < 0)
            {
                month = months + month + 1;
            }

            var days = chineseCalendar.GetDaysInMonth(year, month);

            if (day == 0 || day > days)
            {
                throw new ArgumentOutOfRangeException($"日期允许范围为 1 -- {days}");
            }
            if (day < 0 - days)
            {
                throw new ArgumentOutOfRangeException($"日期允许范围为 -1 -- {0 - days}");
            }
            if (day < 0)
            {
                day = days + day + 1;
            }

            var cdate = new ChineseDate();

            cdate.Year       = year;
            cdate.MonthIndex = cdate.Month = month;
            cdate.Day        = day;
            int leapMonth = chineseCalendar.GetLeapMonth(year);

            cdate.IsLeapMonth = leapMonth == cdate.MonthIndex;
            if (cdate.Month >= leapMonth && leapMonth > 0)
            {
                cdate.Month--;
            }
            cdate.LeapMonthOfYear = Math.Max(0, leapMonth - 1);
            return(cdate);
        }
Exemplo n.º 4
0
 /// <summary>
 /// 转换一个公历日期为农历日期
 /// </summary>
 /// <param name="date">公历日期</param>
 /// <exception cref="ArgumentOutOfRangeException">日期超出范围</exception>
 /// <returns>农历日期</returns>
 public static ChineseDate From(DateTime date)
 {
     try
     {
         var cdate = new ChineseDate();
         cdate.Year       = chineseCalendar.GetYear(date);
         cdate.MonthIndex = cdate.Month = chineseCalendar.GetMonth(date);
         cdate.Day        = chineseCalendar.GetDayOfMonth(date);
         //获取闰月, 0 则表示没有闰月
         int leapMonth = chineseCalendar.GetLeapMonth(cdate.Year);
         cdate.IsLeapMonth = leapMonth == cdate.Month;
         if (cdate.Month >= leapMonth && leapMonth > 0)
         {
             cdate.Month--;
         }
         cdate.LeapMonthOfYear = Math.Max(0, leapMonth - 1);
         return(cdate);
     }
     catch (ArgumentOutOfRangeException ex)
     {
         throw new ArgumentOutOfRangeException($"日期超出范围 1901-02-19(公历) -- 2101-01-28(公历)", ex);
     }
 }