2008年3月27日 星期四

.Net農民曆改版V2.0

前篇文章所提到的國曆轉農曆小程式,後來經測試發現有Bug,沒有做到檢查閏月的程序,因此有時候會造成13月,或是某些月份與正常的月份多一個月,因此就進行些許的修改,以下為完整程式碼 (此程式在線上跑時還是有做些許修改,但是此程式的轉換功能算是已經完整):
using System;
using System.Collections.Generic;
using System.Text;using System.Globalization;
namespace TimeTran{
class Program {
static int Main(string[] args) {
string s = "91 / 2 / 9";
int L_ly,ly,lm,y,ny,nm=0,iToken1,iToken2;
string strny,tT,S;
string[] dateFormats = { "yyyy/MM/dd", "yyy/MM/dd", "yy/MM/dd", "y/MM/dd", "yyyy-MM-dd", "yyy-MM-dd", "yy-MM-dd", "y-MM-dd", "yyyy/M/dd","yyy/M/dd","yy/M/dd","y/M/dd", "yyyy-M-dd","yyy-M-dd","yy-M-dd","y-M-dd", "yyyy/MM/d","yyy/MM/d","yy/MM/d","y/MM/d", "yyyy-MM-d","yyy-MM-d","yy-MM-d","y-MM-d", "yyyy/M/d","yyy/M/d","yy/M/d","y/M/d", "yyyy-M-d","yyy-M-d","yy-M-d","y-M-d"};
ChineseLunisolarCalendar tl = new ChineseLunisolarCalendar();
S = s.Replace(" ", "");
iToken1 = S.IndexOf("/", 0);
iToken2 = S.IndexOf("/", iToken1 + 1);
string iYear = S.Substring(0, iToken1).Trim();
string iMonth = S.Substring(iToken1+1, iToken2-iToken1-1).Trim();
string iDay = S.Substring(iToken2+1).Trim();
ny = int.Parse(iYear) + 1911;
strny = ny.ToString();
tT = strny + "/" + iMonth + "/" + iDay;
DateTime dy = DateTime.ParseExact(tT, dateFormats, null, DateTimeStyles.AllowWhiteSpaces);
ly = tl.GetYear(dy);
lm = tl.GetMonth(dy);
if (tl.IsLeapYear(ly)) //判斷此農曆年是否為閏年
{
L_ly = tl.GetLeapMonth(ly); //抓出此閏年閏何月
switch(L_ly)
{
case 1:
if (lm >= 1)
lm--;
break;
case 2:
if (lm >= 2)
lm--;
break;
case 3:
if (lm >= 3)
lm--;
break;
case 4:
if (lm >= 4)
lm--;
break;
case 5:
if (lm >= 5)
lm--;
break;
case 6:
if (lm >= 6)
lm--;
break;
case 7:
if (lm >= 7)
lm--;
break;
case 8:
if (lm >= 8)
lm--;
break;
case 9:
if (lm >= 9)
lm--;
break;
case 10:
if (lm >= 10)
lm--;
break;
case 11:
if (lm >= 11)
lm--;
break;
case 12:
if (lm >= 12)
lm--;
break;
default:
if (lm >= 13)
lm--;
break;
}
}
else
lm = tl.GetMonth(dy);
Console.WriteLine(DateTime.Now);
Console.WriteLine("國曆:" + tT);
Console.WriteLine("陰曆:" + tl.GetYear(dy) + "/" + lm + "/" + tl.GetDayOfMonth(dy));
return 0;
}
}
}

沒有留言: