PDA

View Full Version : error در تبدیل تاریخ شمسی به میلادی



ل-شهبازی
دوشنبه 02 اردیبهشت 1387, 10:46 صبح
با سلام
من یک برنامه در سی شارپ نوشته ام که مشکلات زیر را دارد:
ابتدا تابع های زیر را نوشتم:

public string Miladi2Shamsi(DateTime _date)

{
PersianCalendar pc = new PersianCalendar();
StringBuilder sb = new StringBuilder();
sb.Append(pc.GetYear(_date).ToString("0000"));
sb.Append("/");
sb.Append(pc.GetMonth(_date).ToString("00"));
sb.Append("/");
sb.Append(pc.GetDayOfMonth(_date).ToString("00"));
return sb.ToString();
}
public DateTime convertShanmsiToMiladi(string strShamsiDate)
{ PersianCalendar pc=new PersianCalendar();
int year = Convert.ToInt32(strShamsiDate.Substring(0, 4));
int month = Convert.ToInt32(strShamsiDate.Substring(5, 2));
int day = Convert.ToInt32(strShamsiDate.Substring(8, 2));
DateTime dtMiladiDate = pc.ToDateTime(year, month, day, 12, 59, 59, 59);
return dtMiladiDate;
}
و می خواهم که تاریخ شمسی را ازtextbox بگیرد وبه پایگاه داده اضافه کند
اما پیغام زیررا می دهد:
input string was not in correct format
, قبل از مقدار دهی به پارامتر این کد را قرار دادم:
Datetime dt=convertShamsiTomiladi(TextBox1.Text)
cmd.parameters,Add("",sqldbtype.).Value=dt

linux
دوشنبه 02 اردیبهشت 1387, 13:48 عصر
مقداری که در تکست باکس وارد می کنید باید به فرمت yyyy/mm/dd باشه

علیرضا مداح
دوشنبه 02 اردیبهشت 1387, 17:19 عصر
سلام ،
در ضمن به این نکته توجه کنید که PersianCalendar در دات نت فریم ورک دارای باگ بوده و عملا" بلااستفاده است ، به شما پیشنهاد میکنم از کتابخانه اصلاح شده زیر استفاده کنید :
www.codeproject.com/KB/selection/FarsiLibrary.aspx (http://www.codeproject.com/KB/selection/FarsiLibrary.aspx)

linux
دوشنبه 02 اردیبهشت 1387, 18:22 عصر
سلام ،
در ضمن به این نکته توجه کنید که تبدیل تاریخ میلادی به شمسی در دات نت فریم ورک دارای باگ بوده و عملا" بلااستفاده است ، به شما پیشنهاد میکنم از کتابخانه اصلاح شده زیر استفاده کنید :
www.codeproject.com/KB/selection/FarsiLibrary.aspx (http://www.codeproject.com/KB/selection/FarsiLibrary.aspx)
لطف کنید اگر منبعی برای باگ داشتن این کد سراغ دارید به ما هم معرفی کنید، من تا حالا هیچ مشکلی با این کلاس نداشتم و خروجی این کلاس با برنامه ای که شما معرفی کردید از آغاز این قرن تا سال تقریبا 1440 یکی هست.

علیرضا مداح
دوشنبه 02 اردیبهشت 1387, 20:20 عصر
یکی از باگهای موجود :
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=324315&wa=wsignin1.0
و


Hamed,
In fact people here are very correct. The method you've used for date conversion is incorrect. There is a book named "Taghvim Jalali" (I have it at home) which gives the correct information on how to convert Solar (Shamsi) and Gregorian (Miladi) dates to each other. I don't have time to look at it at the moment, but I may do it later, and paste it here for you as well. Anyways, it is a multi-step process. Many facts affect the date conversion, including but not limitted to:
1) Gregorian years which can be devided to 4 with no residual (like 2004) are leap years, but those ending in 00 are not (for example year 1900 was not a leap year.)
2) There are 10 years missing in the Gregorian calender!http://66.102.9.104/script/Forums/Images/smiley_rolleyes.gif Believe or not, but some year in 14th century (if I can recall it correctly) the Gregorian scientists found out the Gregorian calender to be 10 days behind, so they omitted 10 days from that year (I'm not sure but based on the dark picture I can see in my mind, it was 2-11 February of that year) so no valid Gregorian calendar has those days in it! This means, if one uses your method to convert a Solar date to those years or before, you have to adjust the results for this missing period of time as well.
3) Solar leap years are not that easy of "every four years" again! I guess you live in Iran, which means, you might have noticed 1362, 1366 and 1370 to be leap years, but the next leap year was NOT 1374; it was 1375! And from that year on, our leap years will be four years apart (so they will remain odd numbers) for approximately 130 years. After this period, we have a FIVE years difference again for once.
I highly recommend you to by and read the book I referenced above. I'm not sure about the author name, and if you remind me, I can provide you with it in near future.
Finally, as others have told you here and elsewhere, you have to improve your English writing skills or you are behind.http://66.102.9.104/script/Forums/Images/smiley_cool.gif
Hope it helps you
Hojjat

http://69.10.233.10/KB/vb/Hijri_Shamsi_Date.aspx

و
.....

linux
دوشنبه 02 اردیبهشت 1387, 20:59 عصر
یکی از باگهای موجود :
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=324315&wa=wsignin1.0

http://69.10.233.10/KB/vb/Hijri_Shamsi_Date.aspx
و
.....
یکم!
اینی که اینجا نوشته باگ کلاس persianclaendar نیست، باگ برنامه نویس هست!

Dim obj_Date As New Globalization.PersianCalendar
Dim result As Date
result = obj_Date.AddDays("1386/01/01", 45)
MessageBox.Show(result.ToString())
اگر در MSDN نگاه کنید، تابع AddDays دوتا متغییر می گیره، یکی از نوع dateTime و یکی از نوع int در این مثال این برنامه نویس ماهر (بهروز راد) فکر کرده 1386/01/01 که به این تابع داده باید شمسی باید , سیستم این تاریخ را بصورت میلادی می گیره و خروجی هم میلادی هست. پس جواب درست هست.
اگر شما کد را اینجوری بنویسی

System.Globalization.PersianCalendar pc = new System.Globalization.PersianCalendar();
DateTime inDate;
DateTime outDate;
string result;
inDate = pc.ToDateTime(1386, 1, 1, 0, 0, 0, 0);
outDate = pc.AddDays(inDate, 45);
result = string.Format("{0}/{1}/{2}", pc.GetYear(outDate), pc.GetMonth(outDate), pc.GetDayOfMonth(outDate));
MessageBox.Show(result);
خروجی همان 15/2/1386 خواهد شد.
لینک دوم هم که کلا از ماجرا پرت هست و کاملا چرت .
یک جایی می بینی کسی یه چیزی گفته سریع جوگیر نشو.

علیرضا مداح
دوشنبه 02 اردیبهشت 1387, 21:33 عصر
دوست عزیز محاسبات تاریخ شمسی در دات نت فریم ورک 2.0 در بعضی مواقع اشتباه است و تاکید خاصی دقیقا" بر روی مثال آورده شده نیست ، اگر دقیقتر موضوع را بررسی کنید ، متوجه اشکال در محاسبات خواهید شد.

linux
دوشنبه 02 اردیبهشت 1387, 23:48 عصر
هیچ جایی اشتباه نمی کنه تنها فرق پرشین کلندر و فارسی لایبرری ،نحوه محاسبه سال کبیسه هست
این مثال را اجرا کن تا ببینی از تاریخ 1900/1/1 تا 2025/01/01 این دو تقویم هیچ فرقی با هم ندارند. از این سال به بعد هم اختلافاتی که پیش می آید فقط بخاطر نوع محاسبه سال کبیسه هست. حالا شما می توانی بگی که سال 1403 کبیسه هست یا 1404؟

Behrouz_Rad
سه شنبه 03 اردیبهشت 1387, 00:08 صبح
در مورد اون باگی که گزارش کردم حق با linux (بابک) ;) هست. پارامتر ورودی رو اشتباه برداشت کرده بودم. البته باگ مفروض رو یکی از دوستان (hamed_bostan) در بخش ASP.NET کشف کرد و بنده اون رو گزارش.
متاسفانه وقت و حوصله نبود که اون گزارش رو ببندم ;)

موفق باشید.

linux
سه شنبه 03 اردیبهشت 1387, 00:14 صبح
در مورد اون باگی که گزارش کردم حق با linux (بابک) ;) هست. پارامتر ورودی رو اشتباه برداشت کرده بودم. البته باگ مفروض رو یکی از دوستان (hamed_bostan) در بخش ASP.NET کشف کرد و بنده اون رو گزارش.
متاسفانه وقت و حوصله نبود که اون گزارش رو ببندم ;)

موفق باشید.
آقا از قدیم گفتن با طناب این و اون نباید رفت تو چاه!