Is lunar Festival - CSharp System

CSharp examples for System:DateTime Calculate

Description

Is lunar Festival

Demo Code

//     Copyright (c) Panda work studio. All rights reserved.
using System;/*  w w w  .  ja  v a 2  s  . c  o m*/

public class Main{
        public static bool IsFestival(DateTime dtDay)
        {
            // return value
            bool isFestival = false;

            // lunar year, month, date
            int lunaryear = 0;
            int lunarmonth = 0;
            int lunardate = 0;
            // gregorian year, month, date
            int nyear = 0;
            int nmonth = 0;
            int nday = 0;

            string sYear = dtDay.Year.ToString();
            string sMonth = dtDay.Month.ToString();
            string sDay = dtDay.Day.ToString();

            int year = 0;
            int month = 0;
            int day = 0;

            try
            {
                year = int.Parse(sYear);
                if (year > 2020)
                    year = 2020;
                month = int.Parse(sMonth);
                day = int.Parse(sDay);
            }
            catch
            {
                year = DateTime.Now.Year;
                month = DateTime.Now.Month;
                day = DateTime.Now.Day;
            }

            // initialize nyear, nmonth, nday
            nyear = year;
            nmonth = month;
            nday = day;

            int nTheDate;
            int nIsEnd;
            int k, m, n, nBit, i;
            string calendar = string.Empty;
            nTheDate = (year - 1921) * 365 + (year - 1921) / 4 + day + MonthAdd[month - 1] - 38;
            if ((year % 4 == 0) && (month > 2))
                nTheDate += 1;

            nIsEnd = 0;
            m = 0;
            k = 0;
            n = 0;

            while (nIsEnd != 1)
            {
                if (LunarData[m] < 4095)
                    k = 11;
                else
                    k = 12;
                n = k;
                while (n >= 0)
                {
                    nBit = LunarData[m];
                    for (i = 1; i < n + 1; i++)
                        nBit = nBit / 2;
                    nBit = nBit % 2;
                    if (nTheDate <= (29 + nBit))
                    {
                        nIsEnd = 1;
                        break;
                    }
                    nTheDate = nTheDate - 29 - nBit;
                    n = n - 1;
                }
                if (nIsEnd == 1)
                    break;
                m = m + 1;
            }

            year = 1921 + m;
            month = k - n + 1;
            day = nTheDate;

            if (k == 12)
            {
                if (month == LunarData[m] / 65536 + 1)
                    month = 1 - month;
                else if (month > LunarData[m] / 65536 + 1)
                    month = month - 1;
            }

            lunaryear = year;
            lunarmonth = month;
            lunardate = day;

            if ((lunardate == 9 && lunarmonth == 9) ||
                (lunardate == 15 && lunarmonth == 8) ||
                (lunardate == 7 && lunarmonth == 7) ||
                (lunardate == 5 && lunarmonth == 5) ||
                (lunardate == 15 && lunarmonth == 1) ||
                (lunardate == 1 && lunarmonth == 1))
            {
                isFestival = true;
            }

            if ((nday == 1 && nmonth == 1) ||
                (nday == 1 && nmonth == 5) ||
                (nday == 1 && nmonth == 6) ||
                (nday == 1 && nmonth == 10))
            {
                isFestival = true;
            }

            return isFestival;
        }
}

Related Tutorials