using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class DateConvertor
{
public DateConvertor()
{
}
// Convert Function ------------------------------------
[B] public string MiladiToShamsi(DateTime Milady, Boolean OnlyDate)[/B]
{
int shamsiDay, shamsiMonth, shamsiYear;
int dayCount, farvardinDayDiff, deyDayDiff;
int[] sumDayMiladiMonth = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
int[] sumDayMiladiMonthLeap = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
farvardinDayDiff = 79;
if (MiladiIsLeap(Milady.Year))
{
dayCount = sumDayMiladiMonthLeap[Milady.Month - 1] + Milady.Day;
}
else
{
dayCount = sumDayMiladiMonth[Milady.Month - 1] + Milady.Day;
}
if ((MiladiIsLeap(Milady.Year - 1)))
{
deyDayDiff = 11;
}
else
{
deyDayDiff = 10;
}
if (dayCount > farvardinDayDiff)
{
dayCount = dayCount - farvardinDayDiff;
if (dayCount <= 186)
{
switch (dayCount % 31)
{
case 0:
shamsiMonth = dayCount / 31;
shamsiDay = 31;
break;
default:
shamsiMonth = (dayCount / 31) + 1;
shamsiDay = (dayCount % 31);
break;
}
shamsiYear = Milady.Year - 621;
}
else
{
dayCount = dayCount - 186;
switch (dayCount % 30)
{
case 0:
shamsiMonth = (dayCount / 30) + 6;
shamsiDay = 30;
break;
default:
shamsiMonth = (dayCount / 30) + 7;
shamsiDay = (dayCount % 30);
break;
}
shamsiYear = Milady.Year - 621;
}
}
else
{
dayCount = dayCount + deyDayDiff;
switch (dayCount % 30)
{
case 0:
shamsiMonth = (dayCount / 30) + 9;
shamsiDay = 30;
break;
default:
shamsiMonth = (dayCount / 30) + 10;
shamsiDay = (dayCount % 30);
break;
}
shamsiYear = Milady.Year - 622;
}
if (OnlyDate == false)
{
String Hour, Minute;
if (Milady.Hour < 10) { Hour = "0" + Milady.Hour; } else { Hour = Convert.ToString(Milady.Hour); }
if (Milady.Minute < 10) { Minute = "0" + Milady.Minute; } else { Minute = Convert.ToString(Milady.Minute); }
return (shamsiYear + "/" + shamsiMonth + "/" + shamsiDay + " " + Hour + ":" + Minute);
}
else
{
return (shamsiYear + "/" + shamsiMonth + "/" + shamsiDay);
}
}
[B] public bool MiladiIsLeap(int miladiYear)[/B]
{
if (((miladiYear % 100) != 0 && (miladiYear % 4) == 0) || ((miladiYear % 100) == 0 && (miladiYear % 400) == 0))
return true;
else
return false;
}
enum tdatekind { dkSolar = 0, dkGregorian = 1 };
byte[] leapmonth = new byte[2] { 12, 2 };
byte[,] daysofmonths = { { 0, 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 }, { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
int[,] daystomonth = { { 0, 0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 365 }, { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 } };
[B] private bool isleapyear(tdatekind datekind, int year)[/B]
{
if (datekind == tdatekind.dkSolar)
return ((((year + 38) * 31) % 128) <= 30);
else
return (((year % 4) == 0) && (((year % 100) != 0) || ((year % 400) == 0)));
}
private int DaysOfMonth(tdatekind DateKind, int Year, int Month)
{
int result;
if ((Year != 0) && (Month >= 1 && Month <= 12))
{
result = daysofmonths[DateKind.GetHashCode(), Month];
if ((Month == leapmonth[DateKind.GetHashCode()]) && isleapyear(DateKind, Year))
result++;
}
else
result = 0;
return result;
}
[B] private bool IsDateValid(tdatekind DateKind, int Year, int Month, int Day)[/B]
{
return ((Year != 0) && (Month >= 1) && (Month <= 12) && (Day >= 1) && (Day <= DaysOfMonth(DateKind, Year, Month)));
}
private int DaysToDate(tdatekind DateKind, int Year, int Month, int Day)
{
int Result;
if (IsDateValid(DateKind, Year, Month, Day))
{
Result = daystomonth[DateKind.GetHashCode(), Month] + Day;
if ((Month > leapmonth[DateKind.GetHashCode()]) && (isleapyear(DateKind, Year)))
Result++;
}
else
Result = 0;
return Result;
}
[B] private bool DateOfDay(tdatekind DateKind, int Days, int Year, ref int Month, ref int Day)[/B]
{
int LeapDay, m;
LeapDay = 0;
Month = 0;
Day = 0;
for (m = 2; m <= 13; m++)
{
if ((m > leapmonth[DateKind.GetHashCode()]) && (isleapyear(DateKind, Year)))
LeapDay = 1;
if (Days <= (daystomonth[DateKind.GetHashCode(), m] + LeapDay))
{
Month = m - 1;
if (Month <= leapmonth[DateKind.GetHashCode()])
LeapDay = 0;
Day = Days - (daystomonth[DateKind.GetHashCode(), Month] + LeapDay);
goto l1;
}
}
l1: return IsDateValid(DateKind, Year, Month, Day);
}
[B] public bool GregorianToSolar(ref int Year, ref int Month, ref int Day)[/B]
{
int LeapDay, Days;
bool PrevGregorianLeap;
if (IsDateValid(tdatekind.dkGregorian, Year, Month, Day))
{
PrevGregorianLeap = isleapyear(tdatekind.dkGregorian, Year - 1);
Days = DaysToDate(tdatekind.dkGregorian, Year, Month, Day);
Year -= 622;
if (isleapyear(tdatekind.dkSolar, Year))
LeapDay = 1;
else
LeapDay = 0;
if (PrevGregorianLeap && (LeapDay == 1))
Days += 287;
else
Days += 286;
if (Days > (365 + LeapDay))
{
Year += 1;
Days -= 365 + LeapDay;
}
return DateOfDay(tdatekind.dkSolar, Days, Year, ref Month, ref Day);
}
else
return false;
}
[B] public bool GregorianToSolar(ref string Yearss, ref string Monthss, ref string Dayss)[/B]
{
int Year = int.Parse(Yearss);
int Month = int.Parse(Monthss);
int Day = int.Parse(Dayss);
int LeapDay, Days;
bool PrevGregorianLeap;
if (IsDateValid(tdatekind.dkGregorian, Year, Month, Day))
{
PrevGregorianLeap = isleapyear(tdatekind.dkGregorian, Year - 1);
Days = DaysToDate(tdatekind.dkGregorian, Year, Month, Day);
Year -= 622;
if (isleapyear(tdatekind.dkSolar, Year))
LeapDay = 1;
else
LeapDay = 0;
if (PrevGregorianLeap && (LeapDay == 1))
Days += 287;
else
Days += 286;
if (Days > (365 + LeapDay))
{
Year += 1;
Days -= 365 + LeapDay;
}
bool temp = DateOfDay(tdatekind.dkSolar, Days, Year, ref Month, ref Day);
Yearss = Year.ToString();
Monthss = Month.ToString();
Dayss = Day.ToString();
return temp;
}
else
return false;
}
[B] public bool SolarToGregorian(ref int Year, ref int Month, ref int Day)[/B]
{
int LeapDay, Days;
bool PrevSolarLeap;
if (IsDateValid(tdatekind.dkSolar, Year, Month, Day))
{
PrevSolarLeap = isleapyear(tdatekind.dkSolar, Year - 1);
Days = DaysToDate(tdatekind.dkSolar, Year, Month, Day);
Year += 621;
if (isleapyear(tdatekind.dkGregorian, Year))
LeapDay = 1;
else
LeapDay = 0;
if (PrevSolarLeap && (LeapDay == 1))
Days += 80;
else
Days += 79;
if (Days > (365 + LeapDay))
{
Year += 1;
Days -= 365 + LeapDay;
}
return DateOfDay(tdatekind.dkGregorian, Days, Year, ref Month, ref Day);
}
else
return false;
}
[B] public bool DayAddToDate(ref int year, ref int month, ref int day, int days)[/B]
{
int i;
if (IsDateValid(tdatekind.dkSolar, year, month, day))
{
if (days > 0)
for (i = 1; i <= days; i++)
{
if (month >= 1 && month <= 6)
{
if (day == 31)
{
month = month + 1;
day = 1;
}
else
day = day + 1;
}
else if (month >= 7 && month <= 11)
{
if (day == 30)
{
month = month + 1;
day = 1;
}
else
day = day + 1;
}
else
{
if (day < 29)
day = day + 1;
else if (day == 29)
{
if (isleapyear(tdatekind.dkSolar, year))
day = 30;
else
{
year = year + 1;
month = 1;
day = 1;
}
}
else
if (day == 30)
{
year = year + 1;
month = 1;
day = 1;
}
}
}
}
else
return false;
return true;
}
[B] public int DayOfDate(int year, int month, int day)[/B]
{
DateTime d;
SolarToGregorian(ref year, ref month, ref day);
d = new System.DateTime(year, month, day, 12, 14, 0);
switch (d.Date.DayOfWeek)
{
case DayOfWeek.Saturday: return 0;
case DayOfWeek.Sunday: return 1;
case DayOfWeek.Monday: return 2;
case DayOfWeek.Tuesday: return 3;
case DayOfWeek.Wednesday: return 4;
case DayOfWeek.Thursday: return 5;
case DayOfWeek.Friday: return 6;
}
return 8;
}
[B] public int Year(DateTime Milady)[/B]
{
return (Convert.ToDateTime(MiladiToShamsi(Milady, true))).Year;
}
[B] public int Month(DateTime Milady)[/B]
{
return (Convert.ToDateTime(MiladiToShamsi(Milady, true))).Month;
}
[B] public static string TextFarsiToEnglish(string str)[/B]
{
string temp = "";
for (int i = 0; i < str.Length; i++)
{
if (Convert.ToInt32(str[i]) < 127)
{
temp += str[i];
continue;
}
switch (Convert.ToInt32(str[i]))
{
case 1570:
temp += "H";
break;
case 1575:
temp += "h";
break;
case 1576:
temp += "F";
break;
case 1662:
temp += "`";
break;
case 1578:
temp += "J";
break;
case 1579:
temp += "E";
break;
case 1580:
temp += "[";
break;
case 1670:
temp += "]";
break;
case 1581:
temp += "P";
break;
case 1582:
temp += "O";
break;
case 1583:
temp += "N";
break;
case 1584:
temp += "B";
break;
case 1585:
temp += "V";
break;
case 1586:
temp += "C";
break;
case 1688:
temp += "\\";
break;
case 1587:
temp += "S";
break;
case 1588:
temp += "A";
break;
case 1589:
temp += "W";
break;
case 1590:
temp += "Q";
break;
case 1591:
temp += "X";
break;
case 1592:
temp += "Z";
break;
case 1593:
temp += "U";
break;
case 1594:
temp += "Y";
break;
case 1601:
temp += "T";
break;
case 1602:
temp += "R";
break;
case 1603:
temp += ";";
break;
case 1711:
temp += "\'";
break;
case 1604:
temp += "G";
break;
case 1605:
temp += "l";
break;
case 0161:
temp += "K";
break;
case 1608:
temp += ",";
break;
case 1607:
temp += "I";
break;
case 1610:
temp += "d";
break;
case 1609:
temp += "D";
break;
case 1574:
temp += "M";
break;
case 1548:
temp += "L";
break;
case 1632:
temp += ".";
break;
case 1776:
temp += "0";
break;
case 1777:
temp += "1";
break;
case 1778:
temp += "2";
break;
case 1779:
temp += "3";
break;
case 1780:
temp += "4";
break;
case 1781:
temp += "5";
break;
case 1782:
temp += "6";
break;
case 1783:
temp += "7";
break;
case 1784:
temp += "8";
break;
case 1785:
temp += "9";
break;
}
}
//str = temp;
return temp;
}
[B] public static string ChangeToFarsi(string text)[/B]
{
string temp = "";
foreach (char chr in text.ToCharArray())
{
int ch = (int)chr;
if (ch < 48 || ch > 57)
{
temp += chr.ToString();
continue;
}
temp+=char.ConvertFromUtf32(ch+1728);
}
return temp;
}
/*
[B] public static string TextEnglishToFarsi(ref string str)[/B]
{
string temp="";
for (int i = 0; i < str.Length; i++)
{
if (Convert.ToInt32(str[i]) > 127)
{
temp += str[i];
continue;
}
switch (str[i])
{
case 1570:
temp += "H";
break;
case 1575:
temp += "h";
break;
case 1576:
temp += "F";
break;
case 1662:
temp += "`";
break;
case 1578:
temp += "J";
break;
case 1579:
temp += "E";
break;
case 1580:
temp += "[";
break;
case 1670:
temp += "]";
break;
case 1581:
temp += "P";
break;
case 1582:
temp += "O";
break;
case 1583:
temp += "N";
break;
case 1584:
temp += "B";
break;
case 1585:
temp += "V";
break;
case 1586:
temp += "C";
break;
case 1688:
temp += "\\";
break;
case 1587:
temp += "S";
break;
case 1588:
temp += "A";
break;
case 1589:
temp += "W";
break;
case 1590:
temp += "Q";
break;
case 1591:
temp += "X";
break;
case 1592:
temp += "Z";
break;
case 1593:
temp += "U";
break;
case 1594:
temp += "Y";
break;
case 1601:
temp += "T";
break;
case 1602:
temp += "R";
break;
case 1603:
temp += ";";
break;
case 1711:
temp += "\'";
break;
case 1604:
temp += "G";
break;
case 1605:
temp += "l";
break;
case 0161:
temp += "K";
break;
case 1608:
temp += ",";
break;
case 1607:
temp += "I";
break;
case 1610:
temp += "d";
break;
case 1609:
temp += "D";
break;
case 1574:
temp += "M";
break;
case 1548:
temp += "L";
break;
case 1632:
temp += ".";
break;
case 1776:
temp += "0";
break;
case 1777:
temp += "1";
break;
case 1778:
temp += "2";
break;
case 1779:
temp += "3";
break;
case 1780:
temp += "4";
break;
case 1781:
temp += "5";
break;
case 1782:
temp += "6";
break;
case 1783:
temp += "7";
break;
case 1784:
temp += "8";
break;
case 1785:
temp += "9";
break;
}
}
str = temp;
return temp;
}
*/
}