C# 農曆跟日曆的重合週期

  • 1779
  • 0

摘要:C# 農曆跟日曆的重合週期

事情的起因:「某一年,A的生日跟 B的農曆生日同一天,下一次同天是幾年後?」

好奇的 OS:農曆跟日曆的週期會有重疊的時候嗎?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace CountWeek
{
    class Program
    {
        static TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();
        static void Main(string[] args)
        {
            Console.WriteLine(string.Format("min:{0}, max:{1}", tlc.MinSupportedDateTime, tlc.MaxSupportedDateTime));

            int i = 10;
            while (i-->0)
            {
                DateTime start = DateTime.Parse("1913/08/17").AddMonths(i);
                DateTime Iterative = start;
                string target = string.Format("tlc day:{0}/{1}", tlc.GetMonth(start), tlc.GetDayOfMonth(start));
                while (Iterative < tlc.MaxSupportedDateTime)
                {
                    string c = CompareResult(Iterative, start, target);
                    if (!string.IsNullOrEmpty(c))
                    {
                        Console.WriteLine(c);
                    }
                    Iterative = Iterative.AddYears(1);
                }
            }
        }

        static string CompareResult(DateTime Iterative, DateTime start, string target)
        {
            if (string.Format("tlc day:{0}/{1}", tlc.GetMonth(Iterative), tlc.GetDayOfMonth(Iterative)) == target)
                return string.Format("{0}, {1}, {2:N0}", Iterative.ToString("yyyy/MM/dd"), target, (Iterative - start).TotalDays / 365);
            return string.Empty;
        }

    }
}

結論:19×N ± 11×{0, 1},N不連續正整數,11是個看不出規律的差…