mirror of
https://gitee.com/6tail/lunar-csharp.git
synced 2025-12-06 10:19:07 +08:00
v1.5.10 修复节气当天获取下一节气仍为当前节气的问题;修复每日宜忌存在重复项的问题;八字转阳历结果按时间先后排序,转换速度大幅提升。
This commit is contained in:
7
CHANGELOG.md
Normal file
7
CHANGELOG.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# Changelog
|
||||
|
||||
|
||||
## [1.5.10] - 2024-03-01
|
||||
1. 修复节气当天获取下一节气仍为当前节气的问题。
|
||||
2. 修复每日宜忌存在重复项的问题。
|
||||
3. 八字转阳历结果按时间先后排序,转换速度大幅提升。
|
||||
@@ -1704,7 +1704,7 @@ namespace Lunar
|
||||
var day = wholeDay ? current.Ymd : current.YmdHms;
|
||||
if (forward)
|
||||
{
|
||||
if (string.Compare(day, today, StringComparison.Ordinal) < 0)
|
||||
if (string.Compare(day, today, StringComparison.Ordinal) <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
115
lunar/Solar.cs
115
lunar/Solar.cs
@@ -223,64 +223,97 @@ namespace Lunar
|
||||
/// <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 = 2, int baseYear = 1900)
|
||||
public static List<Solar> FromBaZi(string yearGanZhi, string monthGanZhi, string dayGanZhi, string timeGanZhi,
|
||||
int sect = 2, int baseYear = 1900)
|
||||
{
|
||||
sect = (1 == sect) ? 1 : 2;
|
||||
var l = new List<Solar>();
|
||||
var years = new List<int>();
|
||||
var today = new Solar();
|
||||
var offsetYear = (today.Year - 4) % 60 - LunarUtil.GetJiaZiIndex(yearGanZhi);
|
||||
if (offsetYear < 0)
|
||||
// 月地支距寅月的偏移值
|
||||
var m = LunarUtil.find(monthGanZhi.Substring(1), LunarUtil.ZHI, -1) - 2;
|
||||
if (m < 0)
|
||||
{
|
||||
offsetYear += 60;
|
||||
}
|
||||
var startYear = today.Year - offsetYear - 1;
|
||||
var minYear = baseYear - 2;
|
||||
while (startYear >= minYear)
|
||||
{
|
||||
years.Add(startYear);
|
||||
startYear -= 60;
|
||||
}
|
||||
var hours = new List<int>();
|
||||
var timeZhi = timeGanZhi.Substring(1);
|
||||
for (int i = 1, j = LunarUtil.ZHI.Length; i < j; i++)
|
||||
{
|
||||
if (LunarUtil.ZHI[i].Equals(timeZhi))
|
||||
{
|
||||
hours.Add((i - 1) * 2);
|
||||
}
|
||||
m += 12;
|
||||
}
|
||||
|
||||
if ("子".Equals(timeZhi))
|
||||
// 月天干要一致
|
||||
if (((LunarUtil.find(yearGanZhi.Substring(0, 1), LunarUtil.GAN, -1) + 1) * 2 + m) % 10 !=
|
||||
LunarUtil.find(monthGanZhi.Substring(0, 1), LunarUtil.GAN, -1))
|
||||
{
|
||||
hours.Add(23);
|
||||
return l;
|
||||
}
|
||||
|
||||
foreach (var hour in hours)
|
||||
// 1年的立春是辛酉,序号57
|
||||
var y = LunarUtil.GetJiaZiIndex(yearGanZhi) - 57;
|
||||
if (y < 0)
|
||||
{
|
||||
foreach (var y in years)
|
||||
y += 60;
|
||||
}
|
||||
|
||||
y++;
|
||||
// 节令偏移值
|
||||
m *= 2;
|
||||
// 时辰地支转时刻,子时按零点算
|
||||
var h = LunarUtil.find(timeGanZhi.Substring(1), LunarUtil.ZHI, -1) * 2;
|
||||
int[] hours = { h };
|
||||
if (0 == h && 2 == sect)
|
||||
{
|
||||
hours = new [] { 0, 23 };
|
||||
}
|
||||
|
||||
var startYear = baseYear - 1;
|
||||
|
||||
// 结束年
|
||||
var endYear = DateTime.Now.Year;
|
||||
|
||||
while (y <= endYear)
|
||||
{
|
||||
if (y >= startYear)
|
||||
{
|
||||
var maxYear = y + 3;
|
||||
var year = y;
|
||||
var month = 11;
|
||||
if (year < baseYear)
|
||||
// 立春为寅月的开始
|
||||
var solarTime = Lunar.FromYmdHms(y, 1, 1).JieQiTable[Lunar.JIE_QI_IN_USE[4 + m]];
|
||||
// 节令推移,年干支和月干支就都匹配上了
|
||||
if (solarTime.Year >= baseYear)
|
||||
{
|
||||
year = baseYear;
|
||||
month = 1;
|
||||
}
|
||||
var solar = new Solar(year, month, 1, hour);
|
||||
while (solar.Year <= maxYear)
|
||||
{
|
||||
var lunar = solar.Lunar;
|
||||
// 日干支和节令干支的偏移值
|
||||
var lunar = solarTime.Lunar;
|
||||
var dgz = (2 == sect) ? lunar.DayInGanZhiExact2 : lunar.DayInGanZhiExact;
|
||||
if (lunar.YearInGanZhiExact.Equals(yearGanZhi) && lunar.MonthInGanZhiExact.Equals(monthGanZhi) && dgz.Equals(dayGanZhi) && lunar.TimeInGanZhi.Equals(timeGanZhi))
|
||||
var d = LunarUtil.GetJiaZiIndex(dayGanZhi) - LunarUtil.GetJiaZiIndex(dgz);
|
||||
if (d < 0)
|
||||
{
|
||||
l.Add(solar);
|
||||
break;
|
||||
d += 60;
|
||||
}
|
||||
|
||||
if (d > 0)
|
||||
{
|
||||
// 从节令推移天数
|
||||
solarTime = solarTime.Next(d);
|
||||
}
|
||||
|
||||
foreach (var hour in hours)
|
||||
{
|
||||
var mi = 0;
|
||||
var s = 0;
|
||||
if (d == 0 && hour == solarTime.Hour)
|
||||
{
|
||||
// 如果正好是节令当天,且小时和节令的小时数相等的极端情况,把分钟和秒钟带上
|
||||
mi = solarTime.Minute;
|
||||
s = solarTime.Second;
|
||||
}
|
||||
|
||||
// 验证一下
|
||||
var solar = Solar.FromYmdHms(solarTime.Year, solarTime.Month, solarTime.Day, hour, mi, s);
|
||||
lunar = solar.Lunar;
|
||||
dgz = (2 == sect) ? lunar.DayInGanZhiExact2 : lunar.DayInGanZhiExact;
|
||||
if (lunar.YearInGanZhiExact.Equals(yearGanZhi) &&
|
||||
lunar.MonthInGanZhiExact.Equals(monthGanZhi) && dgz.Equals(dayGanZhi) &&
|
||||
lunar.TimeInGanZhi.Equals(timeGanZhi))
|
||||
{
|
||||
l.Add(solar);
|
||||
}
|
||||
}
|
||||
solar = solar.Next(1);
|
||||
}
|
||||
}
|
||||
y += 60;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<RootNamespace>Lunar</RootNamespace>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
<PackageId>lunar-csharp</PackageId>
|
||||
<PackageVersion>1.5.9</PackageVersion>
|
||||
<PackageVersion>1.5.10</PackageVersion>
|
||||
<Title>无依赖的阳历、阴历、道历和佛历工具库</Title>
|
||||
<Authors>6tail</Authors>
|
||||
<Description>日历、公历(阳历)、农历(阴历、老黄历)、佛历、道历,节假日、星座、儒略日、干支、生肖、节气、节日、彭祖百忌、每日宜忌、吉神宜趋凶煞宜忌、吉神(喜神/福神/财神/阳贵神/阴贵神)方位、胎神方位、冲煞、纳音、星宿、八字、五行、十神、建除十二值星、青龙名堂等十二神、黄道日及吉凶等。</Description>
|
||||
@@ -16,9 +16,9 @@
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<PackageTags>日历;公历;阳历;农历;阴历;老黄历;佛历;道历;法定假日</PackageTags>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<AssemblyVersion>1.5.9</AssemblyVersion>
|
||||
<FileVersion>1.5.9</FileVersion>
|
||||
<PackageReleaseNotes>修复命宫、身宫的错误。</PackageReleaseNotes>
|
||||
<AssemblyVersion>1.5.10</AssemblyVersion>
|
||||
<FileVersion>1.5.10</FileVersion>
|
||||
<PackageReleaseNotes>修复节气当天获取下一节气仍为当前节气的问题;修复每日宜忌存在重复项的问题;八字转阳历结果按时间先后排序,转换速度大幅提升。</PackageReleaseNotes>
|
||||
<PackageIcon>lunar.png</PackageIcon>
|
||||
<PackageLicenseUrl>https://raw.githubusercontent.com/6tail/lunar-csharp/master/LICENSE</PackageLicenseUrl>
|
||||
</PropertyGroup>
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -251,7 +251,7 @@ namespace test
|
||||
public void TestEightChar2Solar()
|
||||
{
|
||||
var l = Solar.FromBaZi("辛丑", "丁酉", "丙寅", "戊戌");
|
||||
string[] solarList = { "2021-09-15 20:00:00 星期三 处女座", "1961-09-30 20:00:00 星期六 天秤座" };
|
||||
string[] solarList = { "1961-09-30 20:00:00 星期六 天秤座", "2021-09-15 20:00:00 星期三 处女座" };
|
||||
for (int i = 0, j = solarList.Length; i < j; i++)
|
||||
{
|
||||
_testOutputHelper.WriteLine(l[i].JulianDay.ToString());
|
||||
@@ -351,8 +351,8 @@ namespace test
|
||||
|
||||
var expected = new List<string>
|
||||
{
|
||||
"1976-09-21 12:00:00",
|
||||
"1916-10-06 12:00:00"
|
||||
"1916-10-06 12:00:00",
|
||||
"1976-09-21 12:00:00"
|
||||
};
|
||||
|
||||
Assert.Equal(expected, actual);
|
||||
@@ -380,8 +380,8 @@ namespace test
|
||||
|
||||
var expected = new List<string>
|
||||
{
|
||||
"1999-07-21 16:00:00",
|
||||
"1939-08-05 16:00:00"
|
||||
"1939-08-05 16:00:00",
|
||||
"1999-07-21 16:00:00"
|
||||
};
|
||||
|
||||
Assert.Equal(expected, actual);
|
||||
@@ -395,8 +395,8 @@ namespace test
|
||||
|
||||
var expected = new List<string>
|
||||
{
|
||||
"1960-12-17 12:00:00",
|
||||
"1901-01-01 12:00:00"
|
||||
"1901-01-01 12:00:00",
|
||||
"1960-12-17 12:00:00"
|
||||
};
|
||||
|
||||
Assert.Equal(expected, actual);
|
||||
@@ -410,8 +410,8 @@ namespace test
|
||||
|
||||
var expected = new List<string>
|
||||
{
|
||||
"2020-07-21 22:00:00",
|
||||
"1960-08-05 22:00:00"
|
||||
"1960-08-05 22:00:00",
|
||||
"2020-07-21 22:00:00"
|
||||
};
|
||||
|
||||
Assert.Equal(expected, actual);
|
||||
@@ -425,8 +425,8 @@ namespace test
|
||||
|
||||
var expected = new List<string>
|
||||
{
|
||||
"2023-02-24 23:00:00",
|
||||
"1843-02-08 23:00:00"
|
||||
"1843-02-08 23:00:00",
|
||||
"2023-02-24 23:00:00"
|
||||
};
|
||||
|
||||
Assert.Equal(expected, actual);
|
||||
@@ -440,8 +440,8 @@ namespace test
|
||||
|
||||
var expected = new List<string>
|
||||
{
|
||||
"1960-01-15 16:00:00",
|
||||
"1900-01-29 16:00:00"
|
||||
"1900-01-29 16:00:00",
|
||||
"1960-01-15 16:00:00"
|
||||
};
|
||||
|
||||
Assert.Equal(expected, actual);
|
||||
|
||||
Reference in New Issue
Block a user