Exemplo n.º 1
0
        /// <summary>
        /// 農曆轉西曆。
        /// </summary>
        /// <param name="value">欲進行轉換的日期。</param>
        public static string LunarDateToWestDate(DateTime value)
        {
            var oLCalendar = new TaiwanLunisolarCalendar();
            var isLeap     = oLCalendar.IsLeapYear(value.Year - 1911);

            return(oLCalendar.ToDateTime(value.Year - 1911, isLeap ? value.Month + 1 : value.Month, value.Day, 0, 0, 0, 0).ToString("yyyyMMdd"));
        }
        public void IsLeapYear()
        {
            //Assert.IsFalse (cn.IsLeapYear (1901), "#cn1901");
            //Assert.AreEqual (0, cn.GetSexagenaryYear (cn.MinSupportedDateTime), "#60cn1");

            for (int i = 0; i < 60; i++)
            {
                Assert.AreEqual(leapYears [i % 19], cn.IsLeapYear(2000 + i), "cn" + i);
            }
            for (int i = 0; i < 48; i++)             // only 1-to-61 are allowed
            {
                Assert.AreEqual(leapYears [i % 19], jp.IsLeapYear(12 + i, 4), "jp" + i);
            }
            for (int i = 0; i < 50; i++)
            {
                Assert.AreEqual(leapYears [i % 19], tw.IsLeapYear(89 + i), "tw" + i);
            }
            for (int i = 0; i < 50; i++)
            {
                Assert.AreEqual(leapYears [i % 19], kr.IsLeapYear(2000 + i), "kr" + i);
            }

            // 2033 Rain-Water jieqi (usui) new year day is in
            // the leap month.
            Assert.IsTrue(cn.IsLeapYear(2033), "cn2033");
            Assert.IsFalse(cn.IsLeapYear(2034), "cn2034");
        }
Exemplo n.º 3
0
        /// <summary>
        /// 取得農曆的日期
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns></returns>
        public static string GetLunisolarDate(DateTime dt)
        {
            string lunisolarDate        = "";
            TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();

            if (tlc.IsLeapYear(tlc.GetYear(dt)) && tlc.GetMonth(dt) >= tlc.GetLeapMonth(tlc.GetYear(dt)))
            {
                lunisolarDate = FormatLunisolarYear(tlc.GetYear(dt)) +
                                FormatLunisolarMonth(tlc.GetMonth(dt) - 1) +
                                FormatLunisolarDay(tlc.GetDayOfMonth(dt));
            }
            else
            {
                lunisolarDate = FormatLunisolarYear(tlc.GetYear(dt)) +
                                FormatLunisolarMonth(tlc.GetMonth(dt)) +
                                FormatLunisolarDay(tlc.GetDayOfMonth(dt));
            }
            //return "農曆 " + lunisolarDate;
            return(lunisolarDate);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 排序樣板 By 日期順序,先來的要先排序在前面被檢測
        /// </summary>
        /// <param name="festivalTemplates"></param>
        /// <param name="beforeDay">前幾天(節慶)</param>
        /// <returns></returns>
        private IList <TemplateVO> SortTemplateByDate(IList <TemplateVO> festivalTemplates, int beforeDay)
        {
            IList <TemplateVO> result = new List <TemplateVO>();

            if (festivalTemplates != null && festivalTemplates.Count > 0)
            {
                foreach (TemplateVO templateVO in festivalTemplates)
                {
                    if (templateVO.Name.Equals("聖誕節") || templateVO.Name.Equals("西洋情人節"))
                    {
                        //此兩個節日用西元算
                        DateTime today     = DateTime.Today;
                        int      year      = DateTime.Today.Year;
                        int      month     = int.Parse(templateVO.StartDate.Substring(0, 2));
                        int      day       = int.Parse(templateVO.StartDate.Substring(2, 2));
                        DateTime endDate   = new DateTime(DateTime.Today.Year, month, day);
                        DateTime startDate = endDate.AddDays(((-1) * beforeDay));
                        templateVO.BeginDate = startDate;
                        templateVO.LastDate  = endDate;
                    }
                    else
                    {
                        //此其他的節日用農曆算

                        //先從農曆轉成西元
                        TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();
                        DateTime today = DateTime.Today;
                        int      year  = tlc.GetYear(today);
                        int      month = int.Parse(templateVO.StartDate.Substring(0, 2));
                        int      day   = int.Parse(templateVO.StartDate.Substring(2, 2));
                        int      era   = tlc.GetEra(today);
                        //春節的話因為還沒到這個年,所以要加1個月
                        if (templateVO.Name.Equals("春節"))
                        {
                            year = tlc.GetYear(DateTime.Today.AddMonths(1));
                            era  = tlc.GetEra(DateTime.Today.AddMonths(1));
                        }

                        //判斷今年是否為閏年,閏年的話抓出正確的日期
                        if (tlc.IsLeapYear(year, era))
                        {
                            //閏月月份
                            int leapMonth = tlc.GetLeapMonth(year, era);
                            //閏月天數
                            //int monthDays = tlc.GetDaysInMonth(year, leapMonth, era);

                            //閏月有13個月,例如leapMonth=5,代表閏四月,則農曆5月開始要多加1個月
                            if (month >= leapMonth)
                            {
                                month += 1;
                            }
                        }

                        DateTime endDate = tlc.ToDateTime(year, month, day, 0, 0, 0, 0, era);

                        DateTime startDate = endDate.AddDays(((-1) * beforeDay));

                        templateVO.BeginDate = startDate;
                        templateVO.LastDate  = endDate;
                    }
                }

                result = festivalTemplates.OrderBy(p => p.BeginDate).ToList();
            }

            return(result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 取得目前要使用的樣板
        /// </summary>
        /// <param name="beforeDay">前幾天(節慶)</param>
        /// <returns>目前要使用的樣板</returns>
        public TemplateVO GetCurrentTemplate(int beforeDay)
        {
            //先抓節日的 (要存農曆日還是西元要再確認,存西元必須每年度去調整)
            IList <TemplateVO> festivalTemplates = GetTemplateList(TemplateVO.Type.Festival);

            if (festivalTemplates != null && festivalTemplates.Count > 0)
            {
                foreach (TemplateVO templateVO in festivalTemplates)
                {
                    if (templateVO.Name.Equals("聖誕節") || templateVO.Name.Equals("西洋情人節"))
                    {
                        //此兩個節日用西元算
                        DateTime today     = DateTime.Today;
                        int      year      = DateTime.Today.Year;
                        int      month     = int.Parse(templateVO.StartDate.Substring(0, 2));
                        int      day       = int.Parse(templateVO.StartDate.Substring(2, 2));
                        DateTime endDate   = new DateTime(DateTime.Today.Year, month, day);
                        DateTime startDate = endDate.AddDays(((-1) * beforeDay));

                        if (today >= startDate && today <= endDate)
                        {
                            return(templateVO);
                        }
                    }
                    else
                    {
                        //此其他的節日用農曆算

                        //先從農曆轉成西元
                        TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();
                        DateTime today = DateTime.Today;
                        int      year  = tlc.GetYear(today);
                        int      month = int.Parse(templateVO.StartDate.Substring(0, 2));
                        int      day   = int.Parse(templateVO.StartDate.Substring(2, 2));
                        int      era   = tlc.GetEra(today);

                        //判斷今年是否為閏年,閏年的話抓出正確的日期
                        if (tlc.IsLeapYear(year, era))
                        {
                            //閏月月份
                            int leapMonth = tlc.GetLeapMonth(year, era);
                            //閏月天數
                            //int monthDays = tlc.GetDaysInMonth(year, leapMonth, era);

                            //閏月有13個月,例如leapMonth=5,代表閏四月,則農曆5月開始要多加1個月
                            if (month >= leapMonth)
                            {
                                month += 1;
                            }
                        }

                        DateTime endDate = tlc.ToDateTime(year, month, day, 0, 0, 0, 0, era);

                        DateTime startDate = endDate.AddDays(((-1) * beforeDay));

                        if (today >= startDate && today <= endDate)
                        {
                            return(templateVO);
                        }
                    }
                }
            }

            //季節
            IList <TemplateVO> seasonTemplates = GetTemplateList(TemplateVO.Type.Season);

            if (seasonTemplates != null && seasonTemplates.Count > 0)
            {
                DateTime date     = DateTime.Today.AddDays(beforeDay);
                string   showDate = string.Format("{0}{1}", date.Month.ToString().PadLeft(2, '0'), date.Day.ToString().PadLeft(2, '0'));
                int      count    = 0;
                foreach (TemplateVO templateVO in seasonTemplates)
                {
                    count += 1;
                    if (count < seasonTemplates.Count)
                    {
                        if (int.Parse(showDate) >= int.Parse(templateVO.StartDate) && int.Parse(showDate) <= int.Parse(templateVO.EndDate))
                        {
                            return(templateVO);
                        }
                    }
                    else
                    {
                        //最後一季的算法不一樣,可能有跨年(起始日期>結束時 則表示有跨年)
                        if (int.Parse(templateVO.StartDate) > int.Parse(templateVO.EndDate))
                        {
                            if (int.Parse(showDate) >= int.Parse(templateVO.StartDate) && int.Parse(showDate) <= int.Parse("1231"))
                            {
                                return(templateVO);
                            }
                            if (int.Parse(showDate) >= int.Parse("0101") && int.Parse(showDate) <= int.Parse(templateVO.EndDate))
                            {
                                return(templateVO);
                            }
                        }
                        else
                        {
                            if (int.Parse(showDate) >= int.Parse(templateVO.StartDate) && int.Parse(showDate) <= int.Parse(templateVO.EndDate))
                            {
                                return(templateVO);
                            }
                        }
                    }
                }
            }

            //都沒有的話回傳季節的第一個樣板
            return(seasonTemplates[0]);
        }