mirror of
https://gitee.com/6tail/lunar-csharp.git
synced 2025-12-06 10:19:07 +08:00
v1.2.5.0 支持至9999年;修复除夕错误;修复物候错误;大运、小运、流年支持自定义轮数;提升运行速度。
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# lunar [](https://github.com/6tail/lunar-csharp/blob/master/LICENSE)
|
||||
|
||||
lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)工具,支持星座、儒略日、干支、生肖、节气、节日、彭祖百忌、每日宜忌、吉神宜趋凶煞宜忌、吉神(喜神/福神/财神/阳贵神/阴贵神)方位、胎神方位、冲煞、纳音、星宿、八字、五行、十神、建除十二值星、青龙名堂等十二神、黄道黑道日及吉凶等。
|
||||
lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)工具,支持星座、儒略日、干支、生肖、节气、节日、彭祖百忌、每日宜忌、吉神宜趋凶煞宜忌、吉神(喜神/福神/财神/阳贵神/阴贵神)方位、胎神方位、冲煞、纳音、星宿、八字、五行、十神、建除十二值星、青龙名堂等十二神、黄道日及吉凶等。
|
||||
|
||||
> 支持.NET 2.0及以上版本。
|
||||
|
||||
|
||||
28
lunar/ExactDate.cs
Normal file
28
lunar/ExactDate.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace com.nlf.calendar
|
||||
{
|
||||
class ExactDate
|
||||
{
|
||||
public static DateTime fromYmdHms(int year, int month, int day, int hour, int minute, int second)
|
||||
{
|
||||
if (year < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("solar year must bigger than 0");
|
||||
}
|
||||
return new DateTime(year, month, day, hour, minute, second, 0, DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
public static DateTime fromYmd(int year, int month, int day)
|
||||
{
|
||||
return fromYmdHms(year, month, day, 0, 0, 0);
|
||||
}
|
||||
|
||||
public static DateTime fromDate(DateTime date)
|
||||
{
|
||||
return fromYmdHms(date.Year, date.Month, date.Day, date.Hour, date.Minute,date.Second);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,19 +53,18 @@ namespace com.nlf.calendar
|
||||
public void setName(string name)
|
||||
{
|
||||
this.name = name;
|
||||
foreach (string key in LunarUtil.JIE)
|
||||
for (int i = 0, j = Lunar.JIE_QI.Length; i < j; i++)
|
||||
{
|
||||
if (key.Equals(name))
|
||||
if (name.Equals(Lunar.JIE_QI[i]))
|
||||
{
|
||||
this.jie = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
foreach (string key in LunarUtil.QI)
|
||||
{
|
||||
if (key.Equals(name))
|
||||
{
|
||||
this.qi = true;
|
||||
if (i % 2 == 0)
|
||||
{
|
||||
this.qi = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.jie = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
809
lunar/Lunar.cs
809
lunar/Lunar.cs
File diff suppressed because one or more lines are too long
109
lunar/LunarMonth.cs
Normal file
109
lunar/LunarMonth.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using com.nlf.calendar.util;
|
||||
|
||||
namespace com.nlf.calendar
|
||||
{
|
||||
/// <summary>
|
||||
/// 农历月
|
||||
/// </summary>
|
||||
public class LunarMonth
|
||||
{
|
||||
/// <summary>
|
||||
/// 农历年
|
||||
/// </summary>
|
||||
private int year;
|
||||
|
||||
/// <summary>
|
||||
/// 农历月:1-12,闰月为负数,如闰2月为-2
|
||||
/// </summary>
|
||||
private int month;
|
||||
|
||||
/// <summary>
|
||||
/// 天数,大月30天,小月29天
|
||||
/// </summary>
|
||||
private int dayCount;
|
||||
|
||||
/// <summary>
|
||||
/// 初一的儒略日
|
||||
/// </summary>
|
||||
private double firstJulianDay;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
/// </summary>
|
||||
/// <param name="lunarYear">农历年</param>
|
||||
/// <param name="lunarMonth">农历月:1-12,闰月为负数,如闰2月为-2</param>
|
||||
/// <param name="dayCount">天数</param>
|
||||
/// <param name="firstJulianDay">初一的儒略日</param>
|
||||
public LunarMonth(int lunarYear, int lunarMonth, int dayCount, double firstJulianDay)
|
||||
{
|
||||
this.year = lunarYear;
|
||||
this.month = lunarMonth;
|
||||
this.dayCount = dayCount;
|
||||
this.firstJulianDay = firstJulianDay;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过农历年月初始化
|
||||
/// </summary>
|
||||
/// <param name="lunarYear">农历年</param>
|
||||
/// <param name="lunarMonth">农历月:1-12,闰月为负数,如闰2月为-2</param>
|
||||
/// <returns>农历月</returns>
|
||||
public static LunarMonth fromYm(int lunarYear, int lunarMonth)
|
||||
{
|
||||
return LunarYear.fromYear(lunarYear).getMonth(lunarMonth);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取农历年
|
||||
/// </summary>
|
||||
/// <returns>农历年</returns>
|
||||
public int getYear()
|
||||
{
|
||||
return year;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取农历月
|
||||
/// </summary>
|
||||
/// <returns>农历月:1-12,闰月为负数,如闰2月为-2</returns>
|
||||
public int getMonth()
|
||||
{
|
||||
return month;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否闰月
|
||||
/// </summary>
|
||||
/// <returns>true/false</returns>
|
||||
public bool isLeap()
|
||||
{
|
||||
return month < 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取天数
|
||||
/// </summary>
|
||||
/// <returns>天数</returns>
|
||||
public int getDayCount()
|
||||
{
|
||||
return dayCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取初一的儒略日
|
||||
/// </summary>
|
||||
/// <returns>初一的儒略日</returns>
|
||||
public double getFirstJulianDay()
|
||||
{
|
||||
return firstJulianDay;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return year + "年" + (isLeap() ? "闰" : "") + LunarUtil.MONTH[Math.Abs(month)] + "月(" + dayCount + "天)";
|
||||
}
|
||||
}
|
||||
}
|
||||
239
lunar/LunarYear.cs
Normal file
239
lunar/LunarYear.cs
Normal file
@@ -0,0 +1,239 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using com.nlf.calendar.util;
|
||||
|
||||
namespace com.nlf.calendar
|
||||
{
|
||||
/// <summary>
|
||||
/// 农历年
|
||||
/// </summary>
|
||||
public class LunarYear
|
||||
{
|
||||
/// <summary>
|
||||
/// 闰冬月年份
|
||||
/// </summary>
|
||||
private static readonly int[] LEAP_11 = { 75, 94, 170, 238, 265, 322, 389, 469, 553, 583, 610, 678, 735, 754, 773, 849, 887, 936, 1050, 1069, 1126, 1145, 1164, 1183, 1259, 1278, 1308, 1373, 1403, 1441, 1460, 1498, 1555, 1593, 1612, 1631, 1642, 2033, 2128, 2147, 2242, 2614, 2728, 2910, 3062, 3244, 3339, 3616, 3711, 3730, 3825, 4007, 4159, 4197, 4322, 4341, 4379, 4417, 4531, 4599, 4694, 4713, 4789, 4808, 4971, 5085, 5104, 5161, 5180, 5199, 5294, 5305, 5476, 5677, 5696, 5772, 5791, 5848, 5886, 6049, 6068, 6144, 6163, 6258, 6402, 6440, 6497, 6516, 6630, 6641, 6660, 6679, 6736, 6774, 6850, 6869, 6899, 6918, 6994, 7013, 7032, 7051, 7070, 7089, 7108, 7127, 7146, 7222, 7271, 7290, 7309, 7366, 7385, 7404, 7442, 7461, 7480, 7491, 7499, 7594, 7624, 7643, 7662, 7681, 7719, 7738, 7814, 7863, 7882, 7901, 7939, 7958, 7977, 7996, 8034, 8053, 8072, 8091, 8121, 8159, 8186, 8216, 8235, 8254, 8273, 8311, 8330, 8341, 8349, 8368, 8444, 8463, 8474, 8493, 8531, 8569, 8588, 8626, 8664, 8683, 8694, 8702, 8713, 8721, 8751, 8789, 8808, 8816, 8827, 8846, 8884, 8903, 8922, 8941, 8971, 9036, 9066, 9085, 9104, 9123, 9142, 9161, 9180, 9199, 9218, 9256, 9294, 9313, 9324, 9343, 9362, 9381, 9419, 9438, 9476, 9514, 9533, 9544, 9552, 9563, 9571, 9582, 9601, 9639, 9658, 9666, 9677, 9696, 9734, 9753, 9772, 9791, 9802, 9821, 9886, 9897, 9916, 9935, 9954, 9973, 9992 };
|
||||
|
||||
/// <summary>
|
||||
/// 闰腊月年份
|
||||
/// </summary>
|
||||
private static readonly int[] LEAP_12 = { 37, 56, 113, 132, 151, 189, 208, 227, 246, 284, 303, 341, 360, 379, 417, 436, 458, 477, 496, 515, 534, 572, 591, 629, 648, 667, 697, 716, 792, 811, 830, 868, 906, 925, 944, 963, 982, 1001, 1020, 1039, 1058, 1088, 1153, 1202, 1221, 1240, 1297, 1335, 1392, 1411, 1422, 1430, 1517, 1525, 1536, 1574, 3358, 3472, 3806, 3988, 4751, 4941, 5066, 5123, 5275, 5343, 5438, 5457, 5495, 5533, 5552, 5715, 5810, 5829, 5905, 5924, 6421, 6535, 6793, 6812, 6888, 6907, 7002, 7184, 7260, 7279, 7374, 7556, 7746, 7757, 7776, 7833, 7852, 7871, 7966, 8015, 8110, 8129, 8148, 8224, 8243, 8338, 8406, 8425, 8482, 8501, 8520, 8558, 8596, 8607, 8615, 8645, 8740, 8778, 8835, 8865, 8930, 8960, 8979, 8998, 9017, 9055, 9074, 9093, 9112, 9150, 9188, 9237, 9275, 9332, 9351, 9370, 9408, 9427, 9446, 9457, 9465, 9495, 9560, 9590, 9628, 9647, 9685, 9715, 9742, 9780, 9810, 9818, 9829, 9848, 9867, 9905, 9924, 9943, 9962, 10000 };
|
||||
|
||||
private static readonly Dictionary<int, int> LEAP = new Dictionary<int, int>();
|
||||
|
||||
private static readonly Dictionary<int, LunarYear> CACHE = new Dictionary<int, LunarYear>();
|
||||
|
||||
static LunarYear()
|
||||
{
|
||||
foreach (int y in LEAP_11)
|
||||
{
|
||||
LEAP.Add(y, 13);
|
||||
}
|
||||
foreach (int y in LEAP_12)
|
||||
{
|
||||
LEAP.Add(y, 14);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 年
|
||||
/// </summary>
|
||||
private int year;
|
||||
|
||||
private List<LunarMonth> months = new List<LunarMonth>();
|
||||
|
||||
private List<double> jieQiJulianDays = new List<double>();
|
||||
|
||||
/// <summary>
|
||||
/// 通过农历年初始化
|
||||
/// </summary>
|
||||
/// <param name="lunarYear">农历年</param>
|
||||
public LunarYear(int lunarYear)
|
||||
{
|
||||
this.year = lunarYear;
|
||||
compute();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过农历年初始化
|
||||
/// </summary>
|
||||
/// <param name="lunarYear">农历年</param>
|
||||
/// <returns>农历年</returns>
|
||||
public static LunarYear fromYear(int lunarYear)
|
||||
{
|
||||
LunarYear obj = null;
|
||||
try
|
||||
{
|
||||
obj = CACHE[lunarYear];
|
||||
}
|
||||
catch { }
|
||||
if (null == obj)
|
||||
{
|
||||
obj = new LunarYear(lunarYear);
|
||||
CACHE.Add(lunarYear, obj);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
private void compute()
|
||||
{
|
||||
// 节气(中午12点)
|
||||
double[] jq = new double[27];
|
||||
// 合朔,即每月初一(中午12点)
|
||||
double[] hs = new double[16];
|
||||
// 每月天数
|
||||
int[] dayCounts = new int[hs.Length - 1];
|
||||
|
||||
int currentYear = this.year;
|
||||
|
||||
int year = currentYear - 2000;
|
||||
// 从上年的大雪到下年的立春
|
||||
for (int i = 0, j = Lunar.JIE_QI_IN_USE.Length; i < j; i++)
|
||||
{
|
||||
// 精确的节气
|
||||
double t = 36525 * ShouXingUtil.saLonT((year + (17 + i) * 15d / 360) * ShouXingUtil.PI_2);
|
||||
t += ShouXingUtil.ONE_THIRD - ShouXingUtil.dtT(t);
|
||||
jieQiJulianDays.Add(t + Solar.J2000);
|
||||
// 按中午12点算的节气
|
||||
if (i > 0 && i < 28)
|
||||
{
|
||||
jq[i - 1] = Math.Round(t);
|
||||
}
|
||||
}
|
||||
|
||||
// 冬至前的初一
|
||||
double w = ShouXingUtil.calcShuo(jq[0]);
|
||||
if (w > jq[0])
|
||||
{
|
||||
w -= 29.5306;
|
||||
}
|
||||
// 递推每月初一
|
||||
for (int i = 0, j = hs.Length; i < j; i++)
|
||||
{
|
||||
hs[i] = ShouXingUtil.calcShuo(w + 29.5306 * i);
|
||||
}
|
||||
// 每月天数
|
||||
for (int i = 0, j = dayCounts.Length; i < j; i++)
|
||||
{
|
||||
dayCounts[i] = (int)(hs[i + 1] - hs[i]);
|
||||
}
|
||||
|
||||
int currentYearLeap = -1;
|
||||
try
|
||||
{
|
||||
currentYearLeap = LEAP[currentYear];
|
||||
}
|
||||
catch { }
|
||||
if (-1 == currentYearLeap)
|
||||
{
|
||||
currentYearLeap = -1;
|
||||
if (hs[13] <= jq[24])
|
||||
{
|
||||
int i = 1;
|
||||
while (hs[i + 1] > jq[2 * i] && i < 13)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
currentYearLeap = i;
|
||||
}
|
||||
}
|
||||
|
||||
int prevYear = currentYear - 1;
|
||||
int prevYearLeap = -1;
|
||||
try
|
||||
{
|
||||
prevYearLeap = LEAP[prevYear];
|
||||
}
|
||||
catch { }
|
||||
prevYearLeap = -1 == prevYearLeap ? -1 : prevYearLeap - 12;
|
||||
|
||||
int y = prevYear;
|
||||
int m = 11;
|
||||
for (int i = 0, j = dayCounts.Length; i < j; i++)
|
||||
{
|
||||
int cm = m;
|
||||
bool isNextLeap = false;
|
||||
if (y == currentYear && i == currentYearLeap)
|
||||
{
|
||||
cm = -cm;
|
||||
}
|
||||
else if (y == prevYear && i == prevYearLeap)
|
||||
{
|
||||
cm = -cm;
|
||||
}
|
||||
if (y == currentYear && i + 1 == currentYearLeap)
|
||||
{
|
||||
isNextLeap = true;
|
||||
}
|
||||
else if (y == prevYear && i + 1 == prevYearLeap)
|
||||
{
|
||||
isNextLeap = true;
|
||||
}
|
||||
this.months.Add(new LunarMonth(y, cm, dayCounts[i], hs[i] + Solar.J2000));
|
||||
if (!isNextLeap)
|
||||
{
|
||||
m++;
|
||||
}
|
||||
if (m == 13)
|
||||
{
|
||||
m = 1;
|
||||
y++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取年
|
||||
/// </summary>
|
||||
/// <returns>年</returns>
|
||||
public int getYear()
|
||||
{
|
||||
return year;
|
||||
}
|
||||
|
||||
public List<LunarMonth> getMonths()
|
||||
{
|
||||
return months;
|
||||
}
|
||||
|
||||
public List<Double> getJieQiJulianDays()
|
||||
{
|
||||
return jieQiJulianDays;
|
||||
}
|
||||
|
||||
public LunarMonth getMonth(int lunarMonth)
|
||||
{
|
||||
foreach (LunarMonth m in months)
|
||||
{
|
||||
if (m.getYear() == year && m.getMonth() == lunarMonth)
|
||||
{
|
||||
return m;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getLeapMonth()
|
||||
{
|
||||
foreach (LunarMonth m in months)
|
||||
{
|
||||
if (m.getYear() == year && m.isLeap())
|
||||
{
|
||||
return Math.Abs(m.getMonth());
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return year + "";
|
||||
}
|
||||
|
||||
public string toFullString()
|
||||
{
|
||||
return year + "年";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("6tail")]
|
||||
[assembly: AssemblyProduct("lunar")]
|
||||
[assembly: AssemblyCopyright("版权所有 (C) 2020")]
|
||||
[assembly: AssemblyCopyright("版权所有 (C) 2021")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
@@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
|
||||
//
|
||||
// 可以指定所有这些值,也可以使用“修订号”和“内部版本号”的默认值,
|
||||
// 方法是按如下所示使用“*”:
|
||||
[assembly: AssemblyVersion("1.1.0.23")]
|
||||
[assembly: AssemblyFileVersion("1.1.0.23")]
|
||||
[assembly: AssemblyVersion("1.2.5.0")]
|
||||
[assembly: AssemblyFileVersion("1.2.5.0")]
|
||||
|
||||
@@ -78,15 +78,22 @@ namespace com.nlf.calendar
|
||||
/// <param name="hour">小时,0到23</param>
|
||||
/// <param name="minute">分钟,0到59</param>
|
||||
/// <param name="second">秒钟,0到59</param>
|
||||
public Solar(int year, int month, int day, int hour, int minute,int second)
|
||||
public Solar(int year, int month, int day, int hour, int minute, int second)
|
||||
{
|
||||
if (year == 1582 && month == 10)
|
||||
{
|
||||
if (day >= 15)
|
||||
{
|
||||
day -= 10;
|
||||
}
|
||||
}
|
||||
this.year = year;
|
||||
this.month = month;
|
||||
this.day = day;
|
||||
this.hour = hour;
|
||||
this.minute = minute;
|
||||
this.second = second;
|
||||
this.calendar = new DateTime(year, month, day, hour, minute, second);
|
||||
this.calendar = ExactDate.fromYmdHms(year, month, day, hour, minute, second);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -144,8 +151,7 @@ namespace com.nlf.calendar
|
||||
{
|
||||
second = 59;
|
||||
}
|
||||
|
||||
calendar = new DateTime(year, month, day, hour, minute, second);
|
||||
calendar = ExactDate.fromYmdHms(year, month, day, hour, minute, second);
|
||||
this.year = year;
|
||||
this.month = month;
|
||||
this.day = day;
|
||||
@@ -202,7 +208,7 @@ namespace com.nlf.calendar
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过八字获取阳历列表(流派2,晚子时日柱按当天)
|
||||
/// 通过八字获取阳历列表(流派2,晚子时日柱按当天,起始年为1900)
|
||||
/// </summary>
|
||||
/// <param name="yearGanZhi">年柱</param>
|
||||
/// <param name="monthGanZhi">月柱</param>
|
||||
@@ -215,7 +221,7 @@ namespace com.nlf.calendar
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过八字获取阳历列表
|
||||
/// 通过八字获取阳历列表(起始年为1900)
|
||||
/// </summary>
|
||||
/// <param name="yearGanZhi">年柱</param>
|
||||
/// <param name="monthGanZhi">月柱</param>
|
||||
@@ -224,6 +230,21 @@ namespace com.nlf.calendar
|
||||
/// <param name="sect">流派,2晚子时日柱按当天,1晚子时日柱按明天</param>
|
||||
/// <returns>符合的阳历列表</returns>
|
||||
public static List<Solar> fromBaZi(string yearGanZhi, string monthGanZhi, string dayGanZhi, string timeGanZhi, int sect)
|
||||
{
|
||||
return fromBaZi(yearGanZhi, monthGanZhi, dayGanZhi, timeGanZhi, sect, 1900);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过八字获取阳历列表
|
||||
/// </summary>
|
||||
/// <param name="yearGanZhi">年柱</param>
|
||||
/// <param name="monthGanZhi">月柱</param>
|
||||
/// <param name="dayGanZhi">日柱</param>
|
||||
/// <param name="timeGanZhi">时柱</param>
|
||||
/// <param name="sect">流派,2晚子时日柱按当天,1晚子时日柱按明天</param>
|
||||
/// <param name="baseYear">起始年</param>
|
||||
/// <returns>符合的阳历列表</returns>
|
||||
public static List<Solar> fromBaZi(string yearGanZhi, string monthGanZhi, string dayGanZhi, string timeGanZhi, int sect, int baseYear)
|
||||
{
|
||||
sect = (1 == sect) ? 1 : 2;
|
||||
List<Solar> l = new List<Solar>();
|
||||
@@ -236,7 +257,7 @@ namespace com.nlf.calendar
|
||||
}
|
||||
int startYear = today.getYear() - offsetYear;
|
||||
int hour = 0;
|
||||
string timeZhi = timeGanZhi.Substring(1);
|
||||
String timeZhi = timeGanZhi.Substring(1);
|
||||
for (int i = 0, j = LunarUtil.ZHI.Length; i < j; i++)
|
||||
{
|
||||
if (LunarUtil.ZHI[i].Equals(timeZhi))
|
||||
@@ -244,7 +265,7 @@ namespace com.nlf.calendar
|
||||
hour = (i - 1) * 2;
|
||||
}
|
||||
}
|
||||
while (startYear >= SolarUtil.BASE_YEAR - 1)
|
||||
while (startYear >= baseYear)
|
||||
{
|
||||
int year = startYear - 1;
|
||||
int counter = 0;
|
||||
@@ -253,14 +274,10 @@ namespace com.nlf.calendar
|
||||
bool found = false;
|
||||
while (counter < 15)
|
||||
{
|
||||
if (year >= SolarUtil.BASE_YEAR)
|
||||
if (year >= baseYear)
|
||||
{
|
||||
day = 1;
|
||||
if (year == SolarUtil.BASE_YEAR && month == SolarUtil.BASE_MONTH)
|
||||
{
|
||||
day = SolarUtil.BASE_DAY;
|
||||
}
|
||||
Solar solar = Solar.fromYmdHms(year, month, day, hour, 0, 0);
|
||||
Solar solar = new Solar(year, month, day, hour, 0, 0);
|
||||
lunar = solar.getLunar();
|
||||
if (lunar.getYearInGanZhiExact().Equals(yearGanZhi) && lunar.getMonthInGanZhiExact().Equals(monthGanZhi))
|
||||
{
|
||||
@@ -286,15 +303,11 @@ namespace com.nlf.calendar
|
||||
year--;
|
||||
}
|
||||
day = 1;
|
||||
if (year == SolarUtil.BASE_YEAR && month == SolarUtil.BASE_MONTH)
|
||||
{
|
||||
day = SolarUtil.BASE_DAY;
|
||||
}
|
||||
Solar solar = Solar.fromYmdHms(year, month, day, hour, 0, 0);
|
||||
Solar solar = new Solar(year, month, day, hour, 0, 0);
|
||||
while (counter < 61)
|
||||
{
|
||||
lunar = solar.getLunar();
|
||||
string dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
|
||||
String dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
|
||||
if (lunar.getYearInGanZhiExact().Equals(yearGanZhi) && lunar.getMonthInGanZhiExact().Equals(monthGanZhi) && dgz.Equals(dayGanZhi) && lunar.getTimeInGanZhi().Equals(timeGanZhi))
|
||||
{
|
||||
l.Add(solar);
|
||||
@@ -515,7 +528,20 @@ namespace com.nlf.calendar
|
||||
|
||||
public string toYmd()
|
||||
{
|
||||
return year + "-" + (month < 10 ? "0" : "") + month + "-" + (day < 10 ? "0" : "") + day;
|
||||
int d = this.day;
|
||||
if (this.year == 1582 && this.month == 10)
|
||||
{
|
||||
if (d >= 5)
|
||||
{
|
||||
d += 10;
|
||||
}
|
||||
}
|
||||
string y = this.year + "";
|
||||
while (y.Length < 4)
|
||||
{
|
||||
y = "0" + y;
|
||||
}
|
||||
return y + "-" + (month < 10 ? "0" : "") + month + "-" + (d < 10 ? "0" : "") + d;
|
||||
}
|
||||
|
||||
[Obsolete("This method is obsolete, use method toYmdHms instead")]
|
||||
@@ -566,7 +592,7 @@ namespace com.nlf.calendar
|
||||
/// <returns>阳历日期</returns>
|
||||
public Solar next(int days, bool onlyWorkday)
|
||||
{
|
||||
DateTime c = new DateTime(year, month, day, hour, minute, second);
|
||||
DateTime c = ExactDate.fromYmdHms(year, month, day, hour, minute, second);
|
||||
if (0 != days)
|
||||
{
|
||||
if (!onlyWorkday)
|
||||
|
||||
@@ -112,7 +112,7 @@ namespace com.nlf.calendar
|
||||
{
|
||||
return new SolarHalfYear(year, month);
|
||||
}
|
||||
DateTime c = new DateTime(year, month, 1);
|
||||
DateTime c = ExactDate.fromYmd(year, month, 1);
|
||||
c = c.AddMonths(MONTH_COUNT * halfYears);
|
||||
return new SolarHalfYear(c);
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ namespace com.nlf.calendar
|
||||
/// <returns>阳历月</returns>
|
||||
public SolarMonth next(int months)
|
||||
{
|
||||
DateTime c = new DateTime(year, month, 1);
|
||||
DateTime c = ExactDate.fromYmd(year, month, 1);
|
||||
c = c.AddMonths(months);
|
||||
return new SolarMonth(c);
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ namespace com.nlf.calendar
|
||||
{
|
||||
return new SolarSeason(year, month);
|
||||
}
|
||||
DateTime c = new DateTime(year, month, 1);
|
||||
DateTime c = ExactDate.fromYmd(year, month, 1);
|
||||
c = c.AddMonths(MONTH_COUNT * seasons);
|
||||
return new SolarSeason(c);
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ namespace com.nlf.calendar
|
||||
/// <returns>周序号,从1开始</returns>
|
||||
public int getIndex()
|
||||
{
|
||||
DateTime firstDay = new DateTime(year, month, 1);
|
||||
DateTime firstDay = ExactDate.fromYmd(year, month, 1);
|
||||
int firstDayWeek = Convert.ToInt32(firstDay.DayOfWeek.ToString("d"));
|
||||
if (firstDayWeek == 0)
|
||||
{
|
||||
@@ -152,7 +152,7 @@ namespace com.nlf.calendar
|
||||
if (separateMonth)
|
||||
{
|
||||
int n = weeks;
|
||||
DateTime c = new DateTime(year, month, day);
|
||||
DateTime c = ExactDate.fromYmd(year, month, day);
|
||||
SolarWeek week = new SolarWeek(c, start);
|
||||
int m = this.month;
|
||||
bool plus = n > 0;
|
||||
@@ -174,7 +174,7 @@ namespace com.nlf.calendar
|
||||
}
|
||||
else
|
||||
{
|
||||
c = new DateTime(week.getYear(), week.getMonth(), 1);
|
||||
c = ExactDate.fromYmd(week.getYear(), week.getMonth(), 1);
|
||||
week = new SolarWeek(c, start);
|
||||
}
|
||||
}
|
||||
@@ -190,7 +190,7 @@ namespace com.nlf.calendar
|
||||
}
|
||||
else
|
||||
{
|
||||
c = new DateTime(week.getYear(), week.getMonth(), SolarUtil.getDaysOfMonth(week.getYear(), week.getMonth()));
|
||||
c = ExactDate.fromYmd(week.getYear(), week.getMonth(), SolarUtil.getDaysOfMonth(week.getYear(), week.getMonth()));
|
||||
week = new SolarWeek(c, start);
|
||||
}
|
||||
}
|
||||
@@ -202,7 +202,7 @@ namespace com.nlf.calendar
|
||||
}
|
||||
else
|
||||
{
|
||||
DateTime c = new DateTime(year, month, day);
|
||||
DateTime c = ExactDate.fromYmd(year, month, day);
|
||||
c = c.AddDays(weeks * 7);
|
||||
return new SolarWeek(c, start);
|
||||
}
|
||||
@@ -214,7 +214,7 @@ namespace com.nlf.calendar
|
||||
/// <returns>本周第一天的阳历日期</returns>
|
||||
public Solar getFirstDay()
|
||||
{
|
||||
DateTime c = new DateTime(year, month, day);
|
||||
DateTime c = ExactDate.fromYmd(year, month, day);
|
||||
int week = Convert.ToInt32(c.DayOfWeek.ToString("d"));
|
||||
int prev = week - start;
|
||||
if (prev < 0)
|
||||
|
||||
@@ -97,7 +97,7 @@ namespace com.nlf.calendar
|
||||
/// <returns>阳历年</returns>
|
||||
public SolarYear next(int years)
|
||||
{
|
||||
DateTime c = new DateTime(year, 1, 1);
|
||||
DateTime c = ExactDate.fromYmd(year, 1, 1);
|
||||
c = c.AddYears(years);
|
||||
return new SolarYear(c);
|
||||
}
|
||||
|
||||
@@ -138,10 +138,10 @@ namespace com.nlf.calendar.eightchar
|
||||
/// <summary>
|
||||
/// 获取流年
|
||||
/// </summary>
|
||||
/// <param name="n">轮数</param>
|
||||
/// <returns>流年</returns>
|
||||
public LiuNian[] getLiuNian()
|
||||
public LiuNian[] getLiuNian(int n)
|
||||
{
|
||||
int n = 10;
|
||||
if (index < 1)
|
||||
{
|
||||
n = endYear - startYear + 1;
|
||||
@@ -154,13 +154,22 @@ namespace com.nlf.calendar.eightchar
|
||||
return l;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取10轮流年
|
||||
/// </summary>
|
||||
/// <returns>流年</returns>
|
||||
public LiuNian[] getLiuNian()
|
||||
{
|
||||
return getLiuNian(10);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取小运
|
||||
/// </summary>
|
||||
/// <param name="n">轮数</param>
|
||||
/// <returns>小运</returns>
|
||||
public XiaoYun[] getXiaoYun()
|
||||
public XiaoYun[] getXiaoYun(int n)
|
||||
{
|
||||
int n = 10;
|
||||
if (index < 1)
|
||||
{
|
||||
n = endYear - startYear + 1;
|
||||
@@ -173,6 +182,15 @@ namespace com.nlf.calendar.eightchar
|
||||
return l;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取10轮小运
|
||||
/// </summary>
|
||||
/// <returns>小运</returns>
|
||||
public XiaoYun[] getXiaoYun()
|
||||
{
|
||||
return getXiaoYun(10);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -55,9 +55,12 @@ namespace com.nlf.calendar.eightchar
|
||||
Solar current = lunar.getSolar();
|
||||
Solar start = forward ? current : prev.getSolar();
|
||||
Solar end = forward ? next.getSolar() : current;
|
||||
int hourDiff = LunarUtil.getTimeZhiIndex(end.toYmdHms().Substring(11, 5)) - LunarUtil.getTimeZhiIndex(start.toYmdHms().Substring(11, 5));
|
||||
DateTime endCalendar = new DateTime(end.getYear(), end.getMonth(), end.getDay(), 0, 0, 0, 0);
|
||||
DateTime startCalendar = new DateTime(start.getYear(), start.getMonth(), start.getDay(), 0, 0, 0, 0);
|
||||
int endTimeZhiIndex = (end.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(end.toYmdHms().Substring(11, 5));
|
||||
int startTimeZhiIndex = (start.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().Substring(11, 5));
|
||||
// 时辰差
|
||||
int hourDiff = endTimeZhiIndex - startTimeZhiIndex;
|
||||
DateTime endCalendar = ExactDate.fromYmd(end.getYear(), end.getMonth(), end.getDay());
|
||||
DateTime startCalendar = ExactDate.fromYmd(start.getYear(), start.getMonth(), start.getDay());
|
||||
int dayDiff = endCalendar.Subtract(startCalendar).Days;
|
||||
if (hourDiff < 0)
|
||||
{
|
||||
@@ -131,7 +134,7 @@ namespace com.nlf.calendar.eightchar
|
||||
public Solar getStartSolar()
|
||||
{
|
||||
Solar birth = lunar.getSolar();
|
||||
DateTime c = new DateTime(birth.getYear(), birth.getMonth(), birth.getDay(), 0, 0, 0);
|
||||
DateTime c = ExactDate.fromYmd(birth.getYear(), birth.getMonth(), birth.getDay());
|
||||
c = c.AddYears(startYear);
|
||||
c = c.AddMonths(startMonth);
|
||||
c = c.AddDays(startDay);
|
||||
@@ -141,10 +144,10 @@ namespace com.nlf.calendar.eightchar
|
||||
/// <summary>
|
||||
/// 获取大运
|
||||
/// </summary>
|
||||
/// <param name="n">轮数</param>
|
||||
/// <returns>大运</returns>
|
||||
public DaYun[] getDaYun()
|
||||
public DaYun[] getDaYun(int n)
|
||||
{
|
||||
int n = 10;
|
||||
DaYun[] l = new DaYun[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
@@ -153,6 +156,15 @@ namespace com.nlf.calendar.eightchar
|
||||
return l;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取10轮大运
|
||||
/// </summary>
|
||||
/// <returns>大运</returns>
|
||||
public DaYun[] getDaYun()
|
||||
{
|
||||
return getDaYun(10);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
<Compile Include="eightchar\LiuYue.cs" />
|
||||
<Compile Include="eightchar\XiaoYun.cs" />
|
||||
<Compile Include="eightchar\Yun.cs" />
|
||||
<Compile Include="ExactDate.cs" />
|
||||
<Compile Include="Fu.cs" />
|
||||
<Compile Include="Holiday.cs" />
|
||||
<Compile Include="JieQi.cs" />
|
||||
@@ -57,6 +58,9 @@
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="NineStar.cs" />
|
||||
<Compile Include="EightChar.cs" />
|
||||
<Compile Include="util\ShouXingUtil.cs" />
|
||||
<Compile Include="LunarYear.cs" />
|
||||
<Compile Include="LunarMonth.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -5,65 +5,15 @@ using System.Text;
|
||||
namespace com.nlf.calendar.util
|
||||
{
|
||||
/// <summary>
|
||||
/// 农历工具,基准日期为1900年十一月十一,对应阳历1901年1月1日,最远仅支持到2099年
|
||||
/// 农历工具
|
||||
/// </summary>
|
||||
public class LunarUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// 农历基准年
|
||||
/// </summary>
|
||||
public const int BASE_YEAR = 1900;
|
||||
|
||||
/// <summary>
|
||||
/// 农历基准月
|
||||
/// </summary>
|
||||
public const int BASE_MONTH = 11;
|
||||
|
||||
/// <summary>
|
||||
/// 农历基准日
|
||||
/// </summary>
|
||||
public const int BASE_DAY = 11;
|
||||
|
||||
/// <summary>
|
||||
/// 农历与阳历年偏移量
|
||||
/// </summary>
|
||||
public const int BASE_INDEX = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 基准对应的值年九星偏移量
|
||||
/// </summary>
|
||||
public const int BASE_YEAR_JIU_XING_INDEX = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 基准对应的年干支偏移量
|
||||
/// </summary>
|
||||
public const int BASE_YEAR_GANZHI_INDEX = -4;
|
||||
|
||||
/// <summary>
|
||||
/// 基准对应的日干支偏移量
|
||||
/// </summary>
|
||||
public const int BASE_DAY_GANZHI_INDEX = 15;
|
||||
|
||||
/// <summary>
|
||||
/// 月份地支偏移量,因正月起寅
|
||||
/// </summary>
|
||||
public const int BASE_MONTH_ZHI_INDEX = 2;
|
||||
|
||||
/// <summary>
|
||||
/// 星期偏移量
|
||||
/// </summary>
|
||||
public const int BASE_WEEK_INDEX = 2;
|
||||
|
||||
/// <summary>
|
||||
/// 闰年表
|
||||
/// </summary>
|
||||
public static readonly int[] LEAP_MONTH_YEAR = { 6, 14, 19, 25, 33, 36, 38, 41, 44, 52, 55, 79, 117, 136, 147, 150, 155, 158, 185, 193 };
|
||||
|
||||
/// <summary>
|
||||
/// 闰月表
|
||||
/// </summary>
|
||||
public static readonly int[] LUNAR_MONTH = { 0x00, 0x04, 0xad, 0x08, 0x5a, 0x01, 0xd5, 0x54, 0xb4, 0x09, 0x64, 0x05, 0x59, 0x45, 0x95, 0x0a, 0xa6, 0x04, 0x55, 0x24, 0xad, 0x08, 0x5a, 0x62, 0xda, 0x04, 0xb4, 0x05, 0xb4, 0x55, 0x52, 0x0d, 0x94, 0x0a, 0x4a, 0x2a, 0x56, 0x02, 0x6d, 0x71, 0x6d, 0x01, 0xda, 0x02, 0xd2, 0x52, 0xa9, 0x05, 0x49, 0x0d, 0x2a, 0x45, 0x2b, 0x09, 0x56, 0x01, 0xb5, 0x20, 0x6d, 0x01, 0x59, 0x69, 0xd4, 0x0a, 0xa8, 0x05, 0xa9, 0x56, 0xa5, 0x04, 0x2b, 0x09, 0x9e, 0x38, 0xb6, 0x08, 0xec, 0x74, 0x6c, 0x05, 0xd4, 0x0a, 0xe4, 0x6a, 0x52, 0x05, 0x95, 0x0a, 0x5a, 0x42, 0x5b, 0x04, 0xb6, 0x04, 0xb4, 0x22, 0x6a, 0x05, 0x52, 0x75, 0xc9, 0x0a, 0x52, 0x05, 0x35, 0x55, 0x4d, 0x0a, 0x5a, 0x02, 0x5d, 0x31, 0xb5, 0x02, 0x6a, 0x8a, 0x68, 0x05, 0xa9, 0x0a, 0x8a, 0x6a, 0x2a, 0x05, 0x2d, 0x09, 0xaa, 0x48, 0x5a, 0x01, 0xb5, 0x09, 0xb0, 0x39, 0x64, 0x05, 0x25, 0x75, 0x95, 0x0a, 0x96, 0x04, 0x4d, 0x54, 0xad, 0x04, 0xda, 0x04, 0xd4, 0x44, 0xb4, 0x05, 0x54, 0x85, 0x52, 0x0d, 0x92, 0x0a, 0x56, 0x6a, 0x56, 0x02, 0x6d, 0x02, 0x6a, 0x41, 0xda, 0x02, 0xb2, 0xa1, 0xa9, 0x05, 0x49, 0x0d, 0x0a, 0x6d, 0x2a, 0x09, 0x56, 0x01, 0xad, 0x50, 0x6d, 0x01, 0xd9, 0x02, 0xd1, 0x3a, 0xa8, 0x05, 0x29, 0x85, 0xa5, 0x0c, 0x2a, 0x09, 0x96, 0x54, 0xb6, 0x08, 0x6c, 0x09, 0x64, 0x45, 0xd4, 0x0a, 0xa4, 0x05, 0x51, 0x25, 0x95, 0x0a, 0x2a, 0x72, 0x5b, 0x04, 0xb6, 0x04, 0xac, 0x52, 0x6a, 0x05, 0xd2, 0x0a, 0xa2, 0x4a, 0x4a, 0x05, 0x55, 0x94, 0x2d, 0x0a, 0x5a, 0x02, 0x75, 0x61, 0xb5, 0x02, 0x6a, 0x03, 0x61, 0x45, 0xa9, 0x0a, 0x4a, 0x05, 0x25, 0x25, 0x2d, 0x09, 0x9a, 0x68, 0xda, 0x08, 0xb4, 0x09, 0xa8, 0x59, 0x54, 0x03, 0xa5, 0x0a, 0x91, 0x3a, 0x96, 0x04, 0xad, 0xb0, 0xad, 0x04, 0xda, 0x04, 0xf4, 0x62, 0xb4, 0x05, 0x54, 0x0b, 0x44, 0x5d, 0x52, 0x0a, 0x95, 0x04, 0x55, 0x22, 0x6d, 0x02, 0x5a, 0x71, 0xda, 0x02, 0xaa, 0x05, 0xb2, 0x55, 0x49, 0x0b, 0x4a, 0x0a, 0x2d, 0x39, 0x36, 0x01, 0x6d, 0x80, 0x6d, 0x01, 0xd9, 0x02, 0xe9, 0x6a, 0xa8, 0x05, 0x29, 0x0b, 0x9a, 0x4c, 0xaa, 0x08, 0xb6, 0x08, 0xb4, 0x38, 0x6c, 0x09, 0x54, 0x75, 0xd4, 0x0a, 0xa4, 0x05, 0x45, 0x55, 0x95, 0x0a, 0x9a, 0x04, 0x55, 0x44, 0xb5, 0x04, 0x6a, 0x82, 0x6a, 0x05, 0xd2, 0x0a, 0x92, 0x6a, 0x4a, 0x05, 0x55, 0x0a, 0x2a, 0x4a, 0x5a, 0x02, 0xb5, 0x02, 0xb2, 0x31, 0x69, 0x03, 0x31, 0x73, 0xa9, 0x0a, 0x4a, 0x05, 0x2d, 0x55, 0x2d, 0x09, 0x5a, 0x01, 0xd5, 0x48, 0xb4, 0x09, 0x68, 0x89, 0x54, 0x0b, 0xa4, 0x0a, 0xa5, 0x6a, 0x95, 0x04, 0xad, 0x08, 0x6a, 0x44, 0xda, 0x04, 0x74, 0x05, 0xb0, 0x25, 0x54, 0x03 };
|
||||
|
||||
/// <summary>
|
||||
/// 旬
|
||||
/// </summary>
|
||||
@@ -203,16 +153,6 @@ namespace com.nlf.calendar.util
|
||||
/// </summary>
|
||||
public static readonly string[] SHENGXIAO = { "", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
|
||||
|
||||
/// <summary>
|
||||
/// 气
|
||||
/// </summary>
|
||||
public static readonly string[] QI = { "大寒", "雨水", "春分", "谷雨", "小满", "夏至", "大暑", "处暑", "秋分", "霜降", "小雪", "冬至" };
|
||||
|
||||
/// <summary>
|
||||
/// 节
|
||||
/// </summary>
|
||||
public static readonly string[] JIE = { "小寒", "立春", "惊蛰", "清明", "立夏", "芒种", "小暑", "立秋", "白露", "寒露", "立冬", "大雪" };
|
||||
|
||||
/// <summary>
|
||||
/// 日
|
||||
/// </summary>
|
||||
@@ -381,7 +321,6 @@ namespace com.nlf.calendar.util
|
||||
FESTIVAL.Add("8-15", "中秋节");
|
||||
FESTIVAL.Add("9-9", "重阳节");
|
||||
FESTIVAL.Add("12-8", "腊八节");
|
||||
FESTIVAL.Add("12-30", "除夕");
|
||||
|
||||
OTHER_FESTIVAL.Add("1-1", new List<string>(new string[] { "弥勒佛圣诞" }));
|
||||
OTHER_FESTIVAL.Add("1-8", new List<string>(new string[] { "五殿阎罗天子诞" }));
|
||||
@@ -1251,125 +1190,6 @@ namespace com.nlf.calendar.util
|
||||
ZHI_HIDE_GAN.Add("亥", new List<string>(new string[] { "壬", "甲" }));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算指定日期距离基准日期的天数
|
||||
/// </summary>
|
||||
/// <param name="year">农历年</param>
|
||||
/// <param name="month">农历月</param>
|
||||
/// <param name="day">农历日</param>
|
||||
/// <returns>距离天数</returns>
|
||||
public static int computeAddDays(int year, int month, int day)
|
||||
{
|
||||
if (BASE_YEAR == year && BASE_MONTH == month)
|
||||
{
|
||||
return day - BASE_DAY;
|
||||
}
|
||||
int y = BASE_YEAR;
|
||||
int m = BASE_MONTH;
|
||||
int diff = getDaysOfMonth(y, m) - BASE_DAY;
|
||||
while (y != year || m != month)
|
||||
{
|
||||
m = nextMonth(y, m);
|
||||
if (m == 1)
|
||||
{
|
||||
y++;
|
||||
}
|
||||
if (y == year && m == month)
|
||||
{
|
||||
diff += day;
|
||||
}
|
||||
else
|
||||
{
|
||||
diff += getDaysOfMonth(y, m);
|
||||
}
|
||||
}
|
||||
return diff;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定年份的闰月
|
||||
/// </summary>
|
||||
/// <param name="year">年份</param>
|
||||
/// <returns>闰月数字,1代表闰1月,0代表无闰月</returns>
|
||||
public static int getLeapMonth(int year)
|
||||
{
|
||||
int index = year - BASE_YEAR + BASE_INDEX;
|
||||
int v = LUNAR_MONTH[2 * index + 1];
|
||||
v = (v >> 4) & 0x0F;
|
||||
return v;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定年月的下一个月是第几月
|
||||
/// </summary>
|
||||
/// <param name="y">农历年</param>
|
||||
/// <param name="m">农历月,闰月为负数</param>
|
||||
/// <returns>1到12,闰月为负数</returns>
|
||||
public static int nextMonth(int y, int m)
|
||||
{
|
||||
int n = Math.Abs(m) + 1;
|
||||
if (m > 0)
|
||||
{
|
||||
if (m == getLeapMonth(y))
|
||||
{
|
||||
n = -m;
|
||||
}
|
||||
}
|
||||
return 13 != n ? n : 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取某年某月有多少天
|
||||
/// </summary>
|
||||
/// <param name="year">农历年</param>
|
||||
/// <param name="month">农历月,闰月为负数</param>
|
||||
/// <returns>天数</returns>
|
||||
public static int getDaysOfMonth(int year, int month)
|
||||
{
|
||||
int index = year - BASE_YEAR + BASE_INDEX;
|
||||
int v, l, d = 30;
|
||||
if (1 <= month && month <= 8)
|
||||
{
|
||||
v = LUNAR_MONTH[2 * index];
|
||||
l = month - 1;
|
||||
if (((v >> l) & 0x01) == 1)
|
||||
{
|
||||
d = 29;
|
||||
}
|
||||
}
|
||||
else if (9 <= month && month <= 12)
|
||||
{
|
||||
v = LUNAR_MONTH[2 * index + 1];
|
||||
l = month - 9;
|
||||
if (((v >> l) & 0x01) == 1)
|
||||
{
|
||||
d = 29;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
v = LUNAR_MONTH[2 * index + 1];
|
||||
v = (v >> 4) & 0x0F;
|
||||
if (v != Math.Abs(month))
|
||||
{
|
||||
d = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
d = 29;
|
||||
foreach (int i in LEAP_MONTH_YEAR)
|
||||
{
|
||||
if (i == index)
|
||||
{
|
||||
d = 30;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取HH:mm时刻的地支序号,非法的时刻返回0
|
||||
/// </summary>
|
||||
|
||||
316
lunar/util/ShouXingUtil.cs
Normal file
316
lunar/util/ShouXingUtil.cs
Normal file
File diff suppressed because one or more lines are too long
@@ -5,25 +5,10 @@ using System.Text;
|
||||
namespace com.nlf.calendar.util
|
||||
{
|
||||
/// <summary>
|
||||
/// 阳历工具,基准日期为1901年1月1日,对应农历1900年十一月十一
|
||||
/// 阳历工具
|
||||
/// </summary>
|
||||
public class SolarUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// 阳历基准年
|
||||
/// </summary>
|
||||
public const int BASE_YEAR = 1901;
|
||||
|
||||
/// <summary>
|
||||
/// 阳历基准月
|
||||
/// </summary>
|
||||
public const int BASE_MONTH = 1;
|
||||
|
||||
/// <summary>
|
||||
/// 阳历基准日
|
||||
/// </summary>
|
||||
public const int BASE_DAY = 1;
|
||||
|
||||
/// <summary>
|
||||
/// 星期
|
||||
/// </summary>
|
||||
@@ -97,7 +82,6 @@ namespace com.nlf.calendar.util
|
||||
OTHER_FESTIVAL.Add("5-5", new List<string>(new string[] { "马克思诞辰纪念日" }));
|
||||
OTHER_FESTIVAL.Add("5-8", new List<string>(new string[] { "世界红十字日" }));
|
||||
OTHER_FESTIVAL.Add("5-11", new List<string>(new string[] { "世界肥胖日" }));
|
||||
OTHER_FESTIVAL.Add("5-23", new List<string>(new string[] { "世界读书日" }));
|
||||
OTHER_FESTIVAL.Add("5-27", new List<string>(new string[] { "上海解放日" }));
|
||||
OTHER_FESTIVAL.Add("5-31", new List<string>(new string[] { "世界无烟日" }));
|
||||
OTHER_FESTIVAL.Add("6-5", new List<string>(new string[] { "世界环境日" }));
|
||||
@@ -131,7 +115,6 @@ namespace com.nlf.calendar.util
|
||||
OTHER_FESTIVAL.Add("12-12", new List<string>(new string[] { "西安事变纪念日" }));
|
||||
OTHER_FESTIVAL.Add("12-13", new List<string>(new string[] { "南京大屠杀纪念日" }));
|
||||
OTHER_FESTIVAL.Add("12-26", new List<string>(new string[] { "毛泽东诞辰纪念日" }));
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -165,7 +148,7 @@ namespace com.nlf.calendar.util
|
||||
public static int getWeeksOfMonth(int year, int month, int start)
|
||||
{
|
||||
int days = getDaysOfMonth(year, month);
|
||||
DateTime firstDay = new DateTime(year, month, 1);
|
||||
DateTime firstDay = ExactDate.fromYmd(year, month, 1);
|
||||
int week = Convert.ToInt32(firstDay.DayOfWeek.ToString("d"));
|
||||
return (int)Math.Ceiling((days + week - start) * 1D / WEEK.Length);
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ namespace test
|
||||
LiuNian liuNian = l[i];
|
||||
Assert.AreEqual(years[i], liuNian.getYear());
|
||||
Assert.AreEqual(ages[i], liuNian.getAge());
|
||||
Assert.AreEqual(ganZhi[i], liuNian.getGanZhi(),years[i] + "年");
|
||||
Assert.AreEqual(ganZhi[i], liuNian.getGanZhi(), years[i] + "年");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,7 +202,7 @@ namespace test
|
||||
XiaoYun xiaoYun = l[i];
|
||||
Assert.AreEqual(years[i], xiaoYun.getYear());
|
||||
Assert.AreEqual(ages[i], xiaoYun.getAge());
|
||||
Assert.AreEqual(ganZhi[i], xiaoYun.getGanZhi(),years[i] + "年");
|
||||
Assert.AreEqual(ganZhi[i], xiaoYun.getGanZhi(), years[i] + "年");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ namespace test
|
||||
Lunar lunar = solar.getLunar();
|
||||
EightChar eightChar = lunar.getEightChar();
|
||||
|
||||
Assert.AreEqual("戊辰", eightChar.getYear(),"年柱");
|
||||
Assert.AreEqual("戊辰", eightChar.getYear(), "年柱");
|
||||
Assert.AreEqual("甲寅", eightChar.getMonth(), "月柱");
|
||||
Assert.AreEqual("庚子", eightChar.getDay(), "日柱");
|
||||
Assert.AreEqual("戊子", eightChar.getTime(), "时柱");
|
||||
@@ -271,5 +271,28 @@ namespace test
|
||||
Assert.AreEqual("丁亥", eightChar.getDay(), "日柱");
|
||||
Assert.AreEqual("辛亥", eightChar.getTime(), "时柱");
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void testEightChar2Solar()
|
||||
{
|
||||
List<Solar> l = Solar.fromBaZi("辛丑", "丁酉", "丙寅", "戊戌");
|
||||
string[] solars = { "2021-09-15 20:00:00 星期三 处女座", "1961-09-30 20:00:00 星期六 天秤座" };
|
||||
for (int i = 0, j = solars.Length; i < j; i++)
|
||||
{
|
||||
Assert.AreEqual(solars[i], l[i].toFullString());
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test5()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmdHms(2019, 12, 12, 11, 22, 0);
|
||||
EightChar eightChar = lunar.getEightChar();
|
||||
|
||||
Assert.AreEqual("己亥", eightChar.getYear(), "年柱");
|
||||
Assert.AreEqual("丁丑", eightChar.getMonth(), "月柱");
|
||||
Assert.AreEqual("戊申", eightChar.getDay(), "日柱");
|
||||
Assert.AreEqual("戊午", eightChar.getTime(), "时柱");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,6 +238,18 @@ namespace test
|
||||
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test5()
|
||||
{
|
||||
string ymd = "2016-10-04";
|
||||
|
||||
string expected = "2016-10-01";
|
||||
string actual;
|
||||
|
||||
actual = com.nlf.calendar.util.HolidayUtil.getHoliday(ymd).getTarget();
|
||||
|
||||
Assert.AreEqual(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
513
test/LunarTest.cs
Normal file
513
test/LunarTest.cs
Normal file
@@ -0,0 +1,513 @@
|
||||
// 以下代码由 Microsoft Visual Studio 2005 生成。
|
||||
// 测试所有者应该检查每个测试的有效性。
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using com.nlf.calendar;
|
||||
using com.nlf.calendar.util;
|
||||
namespace test
|
||||
{
|
||||
/// <summary>
|
||||
///这是 com.nlf.calendar.Lunar 的测试类,旨在
|
||||
///包含所有 com.nlf.calendar.Lunar 单元测试
|
||||
///</summary>
|
||||
[TestClass()]
|
||||
public class LunarTest
|
||||
{
|
||||
|
||||
|
||||
private TestContext testContextInstance;
|
||||
|
||||
/// <summary>
|
||||
///获取或设置测试上下文,上下文提供
|
||||
///有关当前测试运行及其功能的信息。
|
||||
///</summary>
|
||||
public TestContext TestContext
|
||||
{
|
||||
get
|
||||
{
|
||||
return testContextInstance;
|
||||
}
|
||||
set
|
||||
{
|
||||
testContextInstance = value;
|
||||
}
|
||||
}
|
||||
#region 附加测试属性
|
||||
//
|
||||
//编写测试时,可使用以下附加属性:
|
||||
//
|
||||
//使用 ClassInitialize 在运行类中的第一个测试前先运行代码
|
||||
//
|
||||
//[ClassInitialize()]
|
||||
//public static void MyClassInitialize(TestContext testContext)
|
||||
//{
|
||||
//}
|
||||
//
|
||||
//使用 ClassCleanup 在运行完类中的所有测试后再运行代码
|
||||
//
|
||||
//[ClassCleanup()]
|
||||
//public static void MyClassCleanup()
|
||||
//{
|
||||
//}
|
||||
//
|
||||
//使用 TestInitialize 在运行每个测试前先运行代码
|
||||
//
|
||||
//[TestInitialize()]
|
||||
//public void MyTestInitialize()
|
||||
//{
|
||||
//}
|
||||
//
|
||||
//使用 TestCleanup 在运行完每个测试后运行代码
|
||||
//
|
||||
//[TestCleanup()]
|
||||
//public void MyTestCleanup()
|
||||
//{
|
||||
//}
|
||||
//
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
[TestMethod()]
|
||||
public void test1()
|
||||
{
|
||||
Solar solar = Solar.fromYmdHms(2021, 6, 7, 21, 18, 0);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("二〇二一年四月廿七", lunar.ToString(), "com.nlf.calendar.Solar.getLunar 有错。");
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test2()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmdHms(2021, 6, 7, 21, 18, 0);
|
||||
Solar solar = lunar.getSolar();
|
||||
Assert.AreEqual("2021-07-16", solar.ToString(), "com.nlf.calendar.Lunar.getSolar 有错。");
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test038()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmd(7013, -11, 4);
|
||||
Solar solar = lunar.getSolar();
|
||||
Assert.AreEqual("7013-12-24", solar.ToString(), "com.nlf.calendar.Lunar.getSolar 有错。");
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test041()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(4, 2, 10);
|
||||
Assert.AreEqual("鼠", solar.getLunar().getYearShengXiao(), "com.nlf.calendar.Lunar.getYearShengXiao 有错。");
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test042()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(4, 2, 9);
|
||||
Assert.AreEqual("猪", solar.getLunar().getYearShengXiao(), "com.nlf.calendar.Lunar.getYearShengXiao 有错。");
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test22()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmd(2033, -11, 1);
|
||||
Assert.AreEqual("2033-12-22", lunar.getSolar().toYmd());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test23()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmd(2022, 1, 1);
|
||||
Assert.AreEqual("五黄土玉衡", lunar.getYearNineStar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test24()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmd(2033, 1, 1);
|
||||
Assert.AreEqual("三碧木天玑", lunar.getYearNineStar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test025()
|
||||
{
|
||||
Solar solar = Solar.fromYmdHms(2021, 6, 7, 21, 18, 0);
|
||||
Assert.AreEqual("二〇二一年四月廿七", solar.getLunar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test026()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmdHms(2021, 6, 7, 21, 18, 0);
|
||||
Assert.AreEqual("2021-07-16", lunar.getSolar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test027()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(1989, 4, 28);
|
||||
Assert.AreEqual(23, solar.getLunar().getDay());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test028()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(1990, 10, 8);
|
||||
Assert.AreEqual("乙酉", solar.getLunar().getMonthInGanZhiExact());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test029()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(1990, 10, 9);
|
||||
Assert.AreEqual("丙戌", solar.getLunar().getMonthInGanZhiExact());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test030()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(1990, 10, 8);
|
||||
Assert.AreEqual("丙戌", solar.getLunar().getMonthInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test031()
|
||||
{
|
||||
Solar solar = Solar.fromYmdHms(1987, 4, 17, 9, 0, 0);
|
||||
Assert.AreEqual("一九八七年三月二十", solar.getLunar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test032()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmd(2034, 1, 1);
|
||||
Assert.AreEqual("2034-02-19", lunar.getSolar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test033()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmd(2033, 12, 1);
|
||||
Assert.AreEqual("2034-01-20", lunar.getSolar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test034()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmd(37, -12, 1);
|
||||
Assert.AreEqual("闰腊", lunar.getMonthInChinese());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test035()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmd(56, -12, 1);
|
||||
Assert.AreEqual("闰腊", lunar.getMonthInChinese());
|
||||
|
||||
lunar = Lunar.fromYmd(75, -11, 1);
|
||||
Assert.AreEqual("闰冬", lunar.getMonthInChinese());
|
||||
|
||||
lunar = Lunar.fromYmd(94, -11, 1);
|
||||
Assert.AreEqual("闰冬", lunar.getMonthInChinese());
|
||||
|
||||
lunar = Lunar.fromYmd(94, 12, 1);
|
||||
Assert.AreEqual("腊", lunar.getMonthInChinese());
|
||||
|
||||
lunar = Lunar.fromYmd(113, 12, 1);
|
||||
Assert.AreEqual("腊", lunar.getMonthInChinese());
|
||||
|
||||
lunar = Lunar.fromYmd(113, -12, 1);
|
||||
Assert.AreEqual("闰腊", lunar.getMonthInChinese());
|
||||
|
||||
lunar = Lunar.fromYmd(5552, -12, 1);
|
||||
Assert.AreEqual("闰腊", lunar.getMonthInChinese());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test036()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(5553, 1, 22);
|
||||
Assert.AreEqual("五五五二年闰腊月初二", solar.getLunar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test037()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(7013, 12, 24);
|
||||
Assert.AreEqual("七〇一三年闰冬月初四", solar.getLunar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test35()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmd(2021, 12, 29);
|
||||
Assert.AreEqual("除夕", lunar.getFestivals()[0]);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test36()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmd(2020, 12, 30);
|
||||
Assert.AreEqual("除夕", lunar.getFestivals()[0]);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test37()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmd(2020, 12, 29);
|
||||
Assert.AreEqual(0, lunar.getFestivals().Count);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test38()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(2022, 1, 31);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("除夕", lunar.getFestivals()[0]);
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test8()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmdHms(2020, 12, 10, 13, 0, 0);
|
||||
Assert.AreEqual("二〇二〇年腊月初十", lunar.toString());
|
||||
Assert.AreEqual("2021-01-22", lunar.getSolar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test9()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmdHms(1500, 1, 1, 12, 0, 0);
|
||||
Assert.AreEqual("1500-01-31", lunar.getSolar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test10()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmdHms(1500, 12, 29, 12, 0, 0);
|
||||
Assert.AreEqual("1501-01-18", lunar.getSolar().toString());
|
||||
}
|
||||
|
||||
|
||||
[TestMethod()]
|
||||
public void test11()
|
||||
{
|
||||
Solar solar = Solar.fromYmdHms(1500, 1, 1, 12, 0, 0);
|
||||
Assert.AreEqual("一四九九年腊月初一", solar.getLunar().toString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 1500年计算二月初一是2月29日导致DateTime报错,这个暂未解决,绕开1500年这些特殊的吧。(1582年之前,欧洲实行儒略历,能被4整除的都是闰年,所以1100,1200,1300,1400,1500都是闰年。1582年之後,实行格里历,整百年份必须能被400整除才是闰年。)
|
||||
/// </summary>
|
||||
[TestMethod()]
|
||||
public void test12()
|
||||
{
|
||||
Solar solar = Solar.fromYmdHms(1500, 12, 31, 12, 0, 0);
|
||||
Assert.AreEqual("一五〇〇年腊月十一", solar.getLunar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test13()
|
||||
{
|
||||
Solar solar = Solar.fromYmdHms(1582, 10, 4, 12, 0, 0);
|
||||
Assert.AreEqual("一五八二年九月十八", solar.getLunar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test14()
|
||||
{
|
||||
Solar solar = Solar.fromYmdHms(1582, 10, 15, 12, 0, 0);
|
||||
Assert.AreEqual("一五八二年九月十九", solar.getLunar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test15()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmdHms(1582, 9, 18, 12, 0, 0);
|
||||
Assert.AreEqual("1582-10-04", lunar.getSolar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test16()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmdHms(1582, 9, 19, 12, 0, 0);
|
||||
Assert.AreEqual("1582-10-15", lunar.getSolar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test17()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmdHms(2019, 12, 12, 11, 22, 0);
|
||||
Assert.AreEqual("2020-01-06", lunar.getSolar().toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test18()
|
||||
{
|
||||
Solar solar = Solar.fromYmdHms(2020, 2, 4, 13, 22, 0);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("庚子", lunar.getYearInGanZhi());
|
||||
Assert.AreEqual("庚子", lunar.getYearInGanZhiByLiChun());
|
||||
Assert.AreEqual("己亥", lunar.getYearInGanZhiExact());
|
||||
Assert.AreEqual("戊寅", lunar.getMonthInGanZhi());
|
||||
Assert.AreEqual("丁丑", lunar.getMonthInGanZhiExact());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test19()
|
||||
{
|
||||
Solar solar = Solar.fromYmdHms(2019, 2, 8, 13, 22, 0);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("己亥", lunar.getYearInGanZhi());
|
||||
Assert.AreEqual("己亥", lunar.getYearInGanZhiByLiChun());
|
||||
Assert.AreEqual("己亥", lunar.getYearInGanZhiExact());
|
||||
Assert.AreEqual("丙寅", lunar.getMonthInGanZhi());
|
||||
Assert.AreEqual("丙寅", lunar.getMonthInGanZhiExact());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test20()
|
||||
{
|
||||
Solar solar = Solar.fromYmdHms(1988, 2, 15, 23, 30, 0);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("丁卯", lunar.getYearInGanZhi());
|
||||
Assert.AreEqual("戊辰", lunar.getYearInGanZhiByLiChun());
|
||||
Assert.AreEqual("戊辰", lunar.getYearInGanZhiExact());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test21()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(1988, 2, 15);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("丁卯", lunar.getYearInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test022()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(2012, 12, 27);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("壬辰", lunar.getYearInGanZhi());
|
||||
Assert.AreEqual("壬子", lunar.getMonthInGanZhi());
|
||||
Assert.AreEqual("壬戌", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test023()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(2012, 12, 20);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("壬辰", lunar.getYearInGanZhi());
|
||||
Assert.AreEqual("壬子", lunar.getMonthInGanZhi());
|
||||
Assert.AreEqual("乙卯", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test024()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(2012, 11, 20);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("壬辰", lunar.getYearInGanZhi());
|
||||
Assert.AreEqual("辛亥", lunar.getMonthInGanZhi());
|
||||
Assert.AreEqual("乙酉", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test25()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(2012, 10, 20);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("壬辰", lunar.getYearInGanZhi());
|
||||
Assert.AreEqual("庚戌", lunar.getMonthInGanZhi());
|
||||
Assert.AreEqual("甲寅", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test26()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(2012, 9, 20);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("壬辰", lunar.getYearInGanZhi());
|
||||
Assert.AreEqual("己酉", lunar.getMonthInGanZhi());
|
||||
Assert.AreEqual("甲申", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test126()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(2012, 8, 5);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("戊戌", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test27()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(2000, 2, 2);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("庚寅", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test28()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(1996, 1, 16);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("壬子", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test29()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(1997, 2, 16);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("己丑", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test30()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(1998, 3, 16);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("壬戌", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test31()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(1999, 4, 16);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("戊戌", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test32()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(2000, 7, 16);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("乙亥", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test33()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(2000, 1, 6);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("癸亥", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
|
||||
[TestMethod()]
|
||||
public void test34()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(2000, 1, 9);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("丙寅", lunar.getDayInGanZhi());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -120,6 +120,40 @@ namespace test
|
||||
Assert.AreEqual("2020-01-23", lunar.getSolar().ToString(), "com.nlf.calendar.Lunar.getSolar 有错。");
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void testLeapYear()
|
||||
{
|
||||
Assert.AreEqual(false, SolarUtil.isLeapYear(1500));
|
||||
}
|
||||
|
||||
|
||||
[TestMethod()]
|
||||
public void test1()
|
||||
{
|
||||
Solar solar = Solar.fromYmdHms(2020, 5, 24, 13, 0, 0);
|
||||
Assert.AreEqual("二〇二〇年闰四月初二", solar.getLunar().ToString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test6()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(11, 1, 1);
|
||||
Assert.AreEqual("一〇年腊月初八", solar.getLunar().ToString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test7()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(11, 3, 1);
|
||||
Assert.AreEqual("一一年二月初八", solar.getLunar().ToString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test9()
|
||||
{
|
||||
Solar solar = Solar.fromYmd(26, 4, 13);
|
||||
Assert.AreEqual("二六年三月初八", solar.getLunar().ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -130,6 +130,15 @@ namespace test
|
||||
Assert.AreEqual("鹖鴠不鸣", lunar.getWuHou(), solar.toString());
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test10()
|
||||
{
|
||||
Solar solar = new Solar(1982, 12, 22);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.AreEqual("蚯蚓结", lunar.getWuHou(), solar.toString());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
110
test/YunTest.cs
Normal file
110
test/YunTest.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
// 以下代码由 Microsoft Visual Studio 2005 生成。
|
||||
// 测试所有者应该检查每个测试的有效性。
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using com.nlf.calendar;
|
||||
using com.nlf.calendar.eightchar;
|
||||
namespace test
|
||||
{
|
||||
/// <summary>
|
||||
/// 运测试
|
||||
///</summary>
|
||||
[TestClass()]
|
||||
public class YunTest
|
||||
{
|
||||
|
||||
|
||||
private TestContext testContextInstance;
|
||||
|
||||
/// <summary>
|
||||
///获取或设置测试上下文,上下文提供
|
||||
///有关当前测试运行及其功能的信息。
|
||||
///</summary>
|
||||
public TestContext TestContext
|
||||
{
|
||||
get
|
||||
{
|
||||
return testContextInstance;
|
||||
}
|
||||
set
|
||||
{
|
||||
testContextInstance = value;
|
||||
}
|
||||
}
|
||||
#region 附加测试属性
|
||||
//
|
||||
//编写测试时,可使用以下附加属性:
|
||||
//
|
||||
//使用 ClassInitialize 在运行类中的第一个测试前先运行代码
|
||||
//
|
||||
//[ClassInitialize()]
|
||||
//public static void MyClassInitialize(TestContext testContext)
|
||||
//{
|
||||
//}
|
||||
//
|
||||
//使用 ClassCleanup 在运行完类中的所有测试后再运行代码
|
||||
//
|
||||
//[ClassCleanup()]
|
||||
//public static void MyClassCleanup()
|
||||
//{
|
||||
//}
|
||||
//
|
||||
//使用 TestInitialize 在运行每个测试前先运行代码
|
||||
//
|
||||
//[TestInitialize()]
|
||||
//public void MyTestInitialize()
|
||||
//{
|
||||
//}
|
||||
//
|
||||
//使用 TestCleanup 在运行完每个测试后运行代码
|
||||
//
|
||||
//[TestCleanup()]
|
||||
//public void MyTestCleanup()
|
||||
//{
|
||||
//}
|
||||
//
|
||||
#endregion
|
||||
|
||||
[TestMethod()]
|
||||
public void test1()
|
||||
{
|
||||
Solar solar = Solar.fromYmdHms(1981, 1, 29, 23, 37, 0);
|
||||
Lunar lunar = solar.getLunar();
|
||||
EightChar eightChar = lunar.getEightChar();
|
||||
Yun yun = eightChar.getYun(0);
|
||||
Assert.AreEqual(8, yun.getStartYear(), "起运年数");
|
||||
Assert.AreEqual(0, yun.getStartMonth(), "起运月数");
|
||||
Assert.AreEqual(20, yun.getStartDay(), "起运天数");
|
||||
Assert.AreEqual("1989-02-18", yun.getStartSolar().toYmd(), "起运阳历");
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test2()
|
||||
{
|
||||
Lunar lunar = Lunar.fromYmdHms(2019, 12, 12, 11, 22, 0);
|
||||
EightChar eightChar = lunar.getEightChar();
|
||||
Yun yun = eightChar.getYun(1);
|
||||
Assert.AreEqual(0, yun.getStartYear(), "起运年数");
|
||||
Assert.AreEqual(1, yun.getStartMonth(), "起运月数");
|
||||
Assert.AreEqual(0, yun.getStartDay(), "起运天数");
|
||||
Assert.AreEqual("2020-02-06", yun.getStartSolar().toYmd(), "起运阳历");
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void test3()
|
||||
{
|
||||
Solar solar = Solar.fromYmdHms(2020, 1, 6, 11, 22, 0);
|
||||
Lunar lunar = solar.getLunar();
|
||||
EightChar eightChar = lunar.getEightChar();
|
||||
Yun yun = eightChar.getYun(1);
|
||||
Assert.AreEqual(0, yun.getStartYear(), "起运年数");
|
||||
Assert.AreEqual(1, yun.getStartMonth(), "起运月数");
|
||||
Assert.AreEqual(0, yun.getStartDay(), "起运天数");
|
||||
Assert.AreEqual("2020-02-06", yun.getStartSolar().toYmd(), "起运阳历");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -50,6 +50,8 @@
|
||||
<Compile Include="FuTest.cs" />
|
||||
<Compile Include="LiuYaoTest.cs" />
|
||||
<Compile Include="WuHouTest.cs" />
|
||||
<Compile Include="LunarTest.cs" />
|
||||
<Compile Include="YunTest.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="AuthoringTests.txt" />
|
||||
|
||||
Reference in New Issue
Block a user