示例#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"));
        }
示例#2
0
        public void TestDay()
        {
            TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();

            int year  = tlc.GetYear(DateTime.Now);
            int month = tlc.GetMonth(DateTime.Now);
            int day   = tlc.GetDayOfMonth(DateTime.Now);

            string aa = string.Format("{0}/{1}/{2}", year, month, day);

            Console.WriteLine("day = " + aa);

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

            Console.WriteLine("day2 = " + date2.ToShortDateString());
        }
        public void ToDateTime()
        {
            Assert.AreEqual(new DateTime(2000, 2, 5), cn.ToDateTime(2000, 1, 1, 0, 0, 0, 0), "cn1");
            Assert.AreEqual(new DateTime(2000, 2, 5), jp.ToDateTime(12, 1, 1, 0, 0, 0, 0, 4), "jp1");             // since 1988 (current epoch)
            Assert.AreEqual(new DateTime(2000, 2, 5), tw.ToDateTime(89, 1, 1, 0, 0, 0, 0), "tw1");                // since 1912 (current epoch)
            Assert.AreEqual(new DateTime(2000, 2, 5), kr.ToDateTime(2000, 1, 1, 0, 0, 0, 0), "kr1");

            Assert.AreEqual(new DateTime(2001, 1, 24), cn.ToDateTime(2001, 1, 1, 0, 0, 0, 0), "cn2");
            Assert.AreEqual(new DateTime(2001, 1, 24), jp.ToDateTime(13, 1, 1, 0, 0, 0, 0, 4), "jp2");
            Assert.AreEqual(new DateTime(2001, 1, 24), tw.ToDateTime(90, 1, 1, 0, 0, 0, 0), "tw2");
            Assert.AreEqual(new DateTime(2001, 1, 24), kr.ToDateTime(2001, 1, 1, 0, 0, 0, 0), "kr2");

            Assert.AreEqual(new DateTime(2002, 2, 12), cn.ToDateTime(2002, 1, 1, 0, 0, 0, 0), "cn3");
            Assert.AreEqual(new DateTime(2002, 2, 12), jp.ToDateTime(14, 1, 1, 0, 0, 0, 0, 4), "jp3");
            Assert.AreEqual(new DateTime(2002, 2, 12), tw.ToDateTime(91, 1, 1, 0, 0, 0, 0), "tw3");
            Assert.AreEqual(new DateTime(2002, 2, 12), kr.ToDateTime(2002, 1, 1, 0, 0, 0, 0), "kr3");

            // actually it is 5th month which is leap, but that
            // does not afffect on resulting DateTime
            Assert.AreEqual(new DateTime(2001, 5, 23), cn.ToDateTime(2001, 5, 1, 0, 0, 0, 0), "cn4");
            Assert.AreEqual(new DateTime(2001, 5, 23), jp.ToDateTime(13, 5, 1, 0, 0, 0, 0, 4), "jp4");
            Assert.AreEqual(new DateTime(2001, 5, 23), tw.ToDateTime(90, 5, 1, 0, 0, 0, 0), "tw4");
            Assert.AreEqual(new DateTime(2001, 5, 23), kr.ToDateTime(2001, 5, 1, 0, 0, 0, 0), "kr4");

            // here the leap month works.
            Assert.AreEqual(new DateTime(2002, 2, 11), cn.ToDateTime(2001, 13, 30, 0, 0, 0, 0), "cn5");
            Assert.AreEqual(new DateTime(2002, 2, 11), jp.ToDateTime(13, 13, 30, 0, 0, 0, 0, 4), "jp5");
            Assert.AreEqual(new DateTime(2002, 2, 11), tw.ToDateTime(90, 13, 30, 0, 0, 0, 0), "tw5");
            Assert.AreEqual(new DateTime(2002, 2, 11), kr.ToDateTime(2001, 13, 30, 0, 0, 0, 0), "kr5");

            Assert.AreEqual(cn.MinSupportedDateTime, cn.ToDateTime(1901, 1, 1, 0, 0, 0, 0), "cn6");
            Assert.AreEqual(jp.MinSupportedDateTime, jp.ToDateTime(35, 1, 1, 0, 0, 0, 0, 3), "jp6");           // 1960-1-1
            Assert.AreEqual(tw.MinSupportedDateTime, tw.ToDateTime(1, 1, 1, 0, 0, 0, 0), "tw6");               // 1912
            Assert.AreEqual(kr.MinSupportedDateTime, kr.ToDateTime(918, 1, 1, 0, 0, 0, 0), "kr6");

            Assert.AreEqual(jp.MinSupportedDateTime, cn.ToDateTime(1960, 1, 1, 0, 0, 0, 0), "jp-cn1");
        }
示例#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);
        }
示例#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]);
        }