using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HotelPms.Share.Windows.Util
{
public class JapaneseDateTime
{
private int year, month, dayOfMonth;
private bool isLeap;
private DateTime time;
//System.Globalization.ChineseLunisolarCalendar cc;
System.Globalization.JapaneseLunisolarCalendar cc;
private static string[] RokuyouList = new string[] { "大安", "赤口", "先勝", "友引", "先負", "仏滅" };
///
/// 旧暦年
///
public int Year
{
get { return year; }
}
///
/// 旧暦月
///
public int Month
{
get { return month; }
}
///
/// 新暦時間
///
public DateTime NewTime
{
get { return time; }
}
///
/// 旧暦時間
///
public DateTime OldTime
{
get { return new DateTime(year, month, dayOfMonth); }
}
///
/// 旧暦日
///
public int DayOfMonth
{
get { return dayOfMonth; }
}
///
/// 閏月かどうか
///
public bool IsLeap
{
get { return isLeap; }
}
///
/// 指定日付で旧暦を変換する
///
///
public JapaneseDateTime(DateTime time)
{
//cc = new System.Globalization.ChineseLunisolarCalendar();
cc = new System.Globalization.JapaneseLunisolarCalendar();
if (time > cc.MaxSupportedDateTime || time < cc.MinSupportedDateTime)
throw new Exception("Out Of Range :" + cc.MinSupportedDateTime.ToShortDateString() + "~" + cc.MaxSupportedDateTime.ToShortDateString());
year = cc.GetYear(time);
month = cc.GetMonth(time);
dayOfMonth = cc.GetDayOfMonth(time);
isLeap = cc.IsLeapMonth(year, month);
if (cc.IsLeapYear(year) && month >= cc.GetLeapMonth(year)) { month -= 1; }
this.time = time;
}
public static JapaneseDateTime Now
{
get { return new JapaneseDateTime(DateTime.Now); }
}
public JapaneseDateTime(int Year, int Month, int DayOfMonth, bool IsLeap)
{
if (Year >= cc.MaxSupportedDateTime.Year || Year <= cc.MinSupportedDateTime.Year)
throw new Exception("Out Of Range ::" + cc.MinSupportedDateTime.ToShortDateString() + "~" + cc.MaxSupportedDateTime.ToShortDateString());
if (Month < 1 || Month > 12)
throw new Exception("日付が不正です。");
//cc = new System.Globalization.ChineseLunisolarCalendar();
cc = new System.Globalization.JapaneseLunisolarCalendar();
if (cc.GetLeapMonth(Year) != Month && IsLeap)
throw new Exception("GetLeapMonth Error");
if (cc.GetDaysInMonth(Year, IsLeap ? Month + 1 : Month) < DayOfMonth || DayOfMonth < 1)
throw new Exception("Day Error");
year = Year;
month = Month;
dayOfMonth = DayOfMonth;
isLeap = IsLeap;
time = DateTime.Now;
}
///
/// 旧暦⇒新暦
///
///
public DateTime ToDateTime()
{
return cc.ToDateTime(year, isLeap ? month + 1 : month, dayOfMonth, time.Hour, time.Minute, time.Second, time.Millisecond);
}
///
/// 旧暦⇒新暦
///
///
public static DateTime ToDateTime(JapaneseDateTime CnTime)
{
return CnTime.ToDateTime();
}
///
/// 新暦⇒旧暦
///
///
public static JapaneseDateTime ToJapaneseDateTime(DateTime Time)
{
return new JapaneseDateTime(Time);
}
///
/// 新暦⇒旧暦
///
///
public static string ToJapaneseDateString(DateTime Time)
{
return ToJapaneseDateString(Time, "yyyy/MM/dd");
}
///
/// 新暦⇒旧暦
///
///
public static string ToJapaneseDateString(DateTime Time, string format)
{
try
{
JapaneseDateTime jpDate = new JapaneseDateTime(Time);
return jpDate.OldTime.ToString(format);
}
catch
{
return string.Empty;
}
}
///
/// 新暦⇒六曜
///
///
public static string ToRokuyou(DateTime Time)
{
try
{
JapaneseDateTime jpDate = new JapaneseDateTime(Time);
int index = (jpDate.Month + jpDate.DayOfMonth) % 6;
return RokuyouList[index];
}
catch
{
return string.Empty;
}
}
}
}