Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Date / Time » Downloads
 
Add your own
alternative version

Persian Calendar with Tray Icon

, 12 Mar 2012
A Persian calendar that shows a tray icon
traycalande-binary.zip
traycalande-source.zip
traycalande-source
bin
Release
ClassDiagram1.cd
ClassDiagram2.cd
CrystalReport1.rpt
CrystalReport_task.rpt
CrystalReport_taskall.rpt
hijri.mdb
In_DataSet.xsc
In_DataSet.xss
mytray.csproj.user
mytray.csproj.vspscc
mytray.suo
mytray.vssscc
mytray_TemporaryKey.pfx
Properties
Settings.settings
vssver.scc
Resources
icon1.ico
icon10.ico
icon11.ico
icon12.ico
icon13.ico
icon14.ico
icon15.ico
icon16.ico
icon17.ico
icon18.ico
icon19.ico
icon2.ico
icon20.ico
icon21.ico
icon22.ico
icon23.ico
icon24.ico
icon25.ico
icon26.ico
icon27.ico
icon28.ico
icon29.ico
icon3.ico
icon30.ico
icon31.ico
icon4.ico
icon5.ico
icon6.ico
icon7.ico
icon8.ico
icon9.ico
tumb.jpg
vssver.scc
vssver2.scc
��using System;

using System.Collections.Generic;

using System.Text;

using System.Globalization;



namespace mytray

{

    class CPersianCalender

    {



        const int PersianDiff = 226895; // Absolute days of start of Persian calendar

        HijriCalendar GhamariCalendar = new HijriCalendar();

        public	CPersianCalender(){}

        public void SetGhamariAdjust(int Adjust)

        {

            GhamariCalendar.HijriAdjustment = Adjust;



        }

        public int[] GetGhamariDate()

        {

            DateTime date = DateTime.Now;





            int[] d = new int[3];

            d[0] = GhamariCalendar.GetYear(date);

            d[1] = GhamariCalendar.GetMonth(date);

            d[2] = GhamariCalendar.GetDayOfMonth(date);

            return d;



        }

        /// <summary>

        /// convert shamsi to Gamari Date

        /// </summary>

        /// <param name="PersianYear"></param>

        /// <param name="PersianMon"></param>

        /// <param name="PersianDay"></param>

        /// <returns></returns>





        public int[] ConvertPersianTOGhamariDate(int PersianYear, int PersianMon, int PersianDay)

        {

            int[] ff = ConvertPersianTOGregorianDate(PersianYear, PersianMon, PersianDay);

            int yy = ff[0];

            int mm = ff[1];

            int dd = ff[2];

            DateTime date = new DateTime(yy, mm, dd);



                

            int[] d = new int[3];

            d[0] = GhamariCalendar.GetYear(date);

            d[1] = GhamariCalendar.GetMonth(date);

            d[2] = GhamariCalendar.GetDayOfMonth(date);

            return d;



        }



        /// <summary>

        /// returns DayOfWeek for the input date

        /// </summary>

        /// <param name="PersianDay"></param>

        /// <param name="PersianMonth"></param>

        /// <param name="PersianYear"></param>

        /// <returns></returns>

        public int GetDayOfWeek(int PersianDay,int PersianMonth,int PersianYear)

        {

            int[] ff = ConvertPersianTOGregorianDate(PersianYear, PersianMonth, PersianDay);

            int yy = ff[0];

            int mm = ff[1];

            int dd = ff[2];

            DateTime d1 = new DateTime(yy, mm, dd);

            

            int dayOfWeek = (int)d1.DayOfWeek;

            //change milady day of week to shamsi dayof week

            dayOfWeek = (dayOfWeek + 2) % 7;

            if (dayOfWeek <= 0)

                dayOfWeek = 7;

            return dayOfWeek;

        }

        public int[] GetPersianDate()

        {

            DateTime d1 = DateTime.Now;

            int days = GetGregorianAllDays(d1.Year, d1.Month, d1.Day);

            return GetPersianDate(days);

        }



      public int[] ConvertPersianTOGregorianDate(int PersianYear,int PersianMon,int PersianDay)

        {

             int days = GetPersianAllDays(PersianYear,PersianMon,PersianDay);

              return GetGregorianDate(days);

        }



        bool HasLeapFrac(int year)

        {

            double a = 31*((double)year+38)/128;

            if (a - (int)a < 0.31)

                return true;

            return false;

        }

        bool IsPersianLeapYear(int year)

        {

            if (HasLeapFrac(year) && !HasLeapFrac(year-1))

                return true;

            return false;

        }

        int GetDaysInPersianYear(int year)

        {

            if (IsPersianLeapYear(year) == true)

                return 366;

            return 365;

        }

public int GetDaysOfPersianMonth(int month, int year)

{

	if (month >= 1 && month < 7) return 31;

	if (month == 12)

	{

		if (IsPersianLeapYear(year) == true)

			return 30;

		else

			return 29;

	}

	return 30;

}





public int [] GetPersianDate(int days)

{

	int year = 1; int month = 1;

	int daysInNextYear = 365;

	while (days > daysInNextYear)

	{

		days -= daysInNextYear;

		year++;

		daysInNextYear = GetDaysInPersianYear(year);

	}

	//////////////////////////////////////////

	for (int i = 1; i<= 12; i++)

	{

		int monthdays = GetDaysOfPersianMonth(i,year);

        if (days > monthdays)

        {

            days -= monthdays;

            month++;

        }

        else

            break;

	}



    int[] d =new int[3];

    d[0] = year;

    d[1] = month;

    d[2] = days;

    return d;

}

int GetPersianAllDays(int year, int month ,int day)

{

	int days = 0;

	for (int i = 1 ; i<= year - 1 ; i++)

		days += GetDaysInPersianYear(i);



	for (int i = 1 ; i<= month - 1 ; i++)

		days += GetDaysOfPersianMonth(i,year);

	days += day;

	days +=PersianDiff;

	return days;

}



bool IsGregorianLeapYear(int year)

{

	if ((((year % 4) == 0) && ((year % 100) != 0))

		|| ((year % 400) == 0))

	{

		return true;

	}

	else

		return false;

}



int GetDaysInGregorianYear(int year)

{

	if (IsGregorianLeapYear(year) == true)

		return 366;

	return 365;

}





int GetDaysOfGregorianMonth(int month, int year)

{

	switch (month)

	{

	case 2:

		if (IsGregorianLeapYear(year) == true)

			return 29;

		else

			return 28;

	case 4:

	case 6:

	case 9:

	case 11: return 30;

	default: return 31;

	}

}





public int GetGregorianAllDays(int year, int month ,int day)

{

	int days = 0;

	for (int i = 1 ; i<= year - 1 ; i++)

		days +=GetDaysInGregorianYear(i);



	for ( int i = 1 ; i<= month - 1 ; i++)

		days += GetDaysOfGregorianMonth(i,year);

	days += day;

	days -=PersianDiff;

	return days;

}



  int [] GetGregorianDate(int days)

{

	int year = 1; int month = 1;

	int daysInNextYear = 365;

	while (days > daysInNextYear)

	{

		days -= daysInNextYear;

		year++;

		daysInNextYear = GetDaysInGregorianYear(year);

	}

	//////////////////////////////////////////



	for (int i = 1; i<= 12; i++)

	{

		int monthdays = GetDaysOfGregorianMonth(i,year);

        if (days > monthdays)

        {

            days -= monthdays;

            month++;

        }

        else break;

	}

   int[] d =new int[3];

    d[0] = year;

    d[1] = month;

    d[2] = days;

    return d;



}



        public string GetMonthName(int Month)

        {

            string strMonth="";

            switch (Month)

            {

                case 1:

                    strMonth = "A1H1/JF";

                    break;

                case 2:

                    strMonth = "'1/J(G4*";

                    break;

                case 3:

                    strMonth = ".1/'/";

                    break;

                case 4:

                    strMonth = "*J1";

                    break;

                case 5:

                    strMonth = "E1/'/";

                    break;

                case 6:

                    strMonth = "4G1JH1";

                    break;

                case 7:

                    strMonth = "EG1";

                    break;

                case 8:

                    strMonth = ""('F";

                    break;

                case 9:

                    strMonth = ""01";

                    break;

                case 10:

                    strMonth = "/J";

                    break;

                case 11:

                    strMonth = "(GEF";

                    break;

                case 12:

                    strMonth = "'3AF/";

                    break;



            }

            return strMonth;

        }

        public string GetGamariMonthName(int Month)

        {

            string strMonth = "";

            switch (Month)

            {

                case 1:

                    strMonth = "E-1E";

                    break;

                case 2:

                    strMonth = "5A1";

                    break;

                case 3:

                    strMonth = "1(J9 'D'HD";

                    break;

                case 4:

                    strMonth = "1(J9 'D+'FJ";

                    break;

                case 5:

                    strMonth = ",E'/I 'D'HDJ";

                    break;

                case 6:

                    strMonth = ",E'/I 'D+'FJ ";

                    break;

                case 7:

                    strMonth = "1,(";

                    break;

                case 8:

                    strMonth = "49('F";

                    break;

                case 9:

                    strMonth = "1E6'F";

                    break;

                case 10:

                    strMonth = "4H'D";

                    break;

                case 11:

                    strMonth = "0JB9/)";

                    break;

                case 12:

                    strMonth = "0J-,)";

                    break;



            }

            return strMonth;

        }

        public string GetDayOfWeekName(int Day)

        {

            string strDay = "";

            switch (Day)

            {

                case 1:

                    strDay = "4F(G";

                    break;

                case 2:

                    strDay = "JC 4F(G";

                    break;

                case 3:

                    strDay = "/H4F(G";

                    break;

                case 4:

                    strDay = "3G 4F(G";

                    break;

                case 5:

                    strDay = "�G'14F(G";

                    break;

                case 6:

                    strDay = "~F,4F(G";

                    break;

                case 7:

                    strDay = ",E9G";

                    break;

            }

            return strDay;



        }

        public String GetDayFullTooltip(int PersianDay,int PersianMonth,int PersianYear)

        {

            int DayOfweek = GetDayOfWeek(PersianDay, PersianMonth, PersianYear);



            string s = GetDayOfWeekName(DayOfweek) + " " + Convert.ToString(PersianDay) + " "

                + GetMonthName(PersianMonth) + " " + Convert.ToString(PersianYear);

            int[] ff = ConvertPersianTOGregorianDate(PersianYear, PersianMonth, PersianDay);

            int yy = ff[0];

            int mm = ff[1];

            int dd = ff[2];

            DateTime d1 = new DateTime(yy, mm, dd);



            int[] GhamariDate = ConvertPersianTOGhamariDate(PersianYear, PersianMonth, PersianDay);

    /*        s += "\n" + Convert.ToString(GhamariDate[2]);

            s += " " + GetGamariMonthName(GhamariDate[1]);

            s+= " " + Convert.ToString(GhamariDate[0]);

            */



            s += "\n" +GetDayOfWeekName(DayOfweek) + " "+ Convert.ToString(GhamariDate[2]) + " "

                          + GetGamariMonthName(GhamariDate[1])+" " + Convert.ToString(GhamariDate[0]);



            s += "\n" + Convert.ToString(yy) + "/"

              + Convert.ToString(mm) + "/" + Convert.ToString(dd);







            //s += "\n" + d1.ToLongDateString();

            return s;

 }

        public String GetDaySmallTooltip(int DayOfweek, int PersianDay, int PersianMonth, int PersianYear)

        {

            string s = GetDayOfWeekName(DayOfweek) + " " + Convert.ToString(PersianYear) + "/"

                + Convert.ToString(PersianMonth) + "/" + Convert.ToString(PersianDay);

            int[] GhamariDate = ConvertPersianTOGhamariDate(PersianYear, PersianMonth, PersianDay);

            s += "\n" +GetDayOfWeekName(DayOfweek) + " "+ Convert.ToString(GhamariDate[2]) + " "

                          + GetGamariMonthName(GhamariDate[1])+" " + Convert.ToString(GhamariDate[0]);





              return s;

        }



    }

    }



By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

mohammad hajibegloo
Engineer neyshabur azad univeristy
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
I had worked as programmer,project manager,web developer for more than 3 years, and have worked on programming personaly for more than 10 years.
I have worked in university as a teacher for 8 years.
my favorite langueges are : VC++,C#,ASP.NET,PHP

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 12 Mar 2012
Article Copyright 2008 by mohammad hajibegloo
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid