Skip to content

Commit

Permalink
v1.3.11 修复节气当天获取下一节气仍为当前节气的问题;修复每日宜忌存在重复项的问题;八字转阳历结果按时间先后排序,转换速度大幅提升。
Browse files Browse the repository at this point in the history
6tail committed Mar 1, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
kmbcook Kevin Cook
1 parent c974f87 commit 35f4af5
Showing 6 changed files with 94 additions and 47 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -167,3 +167,8 @@

## 1.3.10
1. 修复命宫、身宫的错误。

## 1.3.11
1. 修复节气当天获取下一节气仍为当前节气的问题;
2. 修复每日宜忌存在重复项的问题;
3. 八字转阳历结果按时间先后排序,转换速度大幅提升。
2 changes: 1 addition & 1 deletion lib/calendar/Lunar.dart
Original file line number Diff line number Diff line change
@@ -1076,7 +1076,7 @@ class Lunar {
Solar solar = entry.value;
String day = wholeDay ? solar.toYmd() : solar.toYmdHms();
if (forward) {
if (day.compareTo(today) < 0) {
if (day.compareTo(today) <= 0) {
continue;
}
if (null == near) {
109 changes: 71 additions & 38 deletions lib/calendar/Solar.dart
Original file line number Diff line number Diff line change
@@ -121,50 +121,83 @@ class Solar {
static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String dayGanZhi, String timeGanZhi, {int sect = 2, int baseYear = 1900}) {
sect = (1 == sect) ? 1 : 2;
List<Solar> l = [];
List<int> years = [];
Solar today = Solar();

int offsetYear = (today.getYear() - 4) % 60 - LunarUtil.getJiaZiIndex(yearGanZhi);
if (offsetYear < 0) {
offsetYear = offsetYear + 60;
}
int startYear = today.getYear() - offsetYear - 1;
int minYear = baseYear - 2;
while (startYear >= minYear) {
years.add(startYear);
startYear -= 60;
}
List<int> hours = [];
String timeZhi = timeGanZhi.substring(1);
for (int i = 1, j = LunarUtil.ZHI.length; i < j; i++) {
if (LunarUtil.ZHI[i] == timeZhi) {
hours.add((i - 1) * 2);
break;
}
// 月地支距寅月的偏移值
int m = LunarUtil.find(monthGanZhi.substring(1), LunarUtil.ZHI, -1) - 2;
if (m < 0) {
m += 12;
}
if ('子' == timeZhi) {
// 月天干要一致
if (((LunarUtil.find(yearGanZhi.substring(0, 1), LunarUtil.GAN, -1) + 1) *
2 + m) % 10 !=
LunarUtil.find(monthGanZhi.substring(0, 1), LunarUtil.GAN, -1)) {
return l;
}
// 1年的立春是辛酉,序号57
int y = LunarUtil.getJiaZiIndex(yearGanZhi) - 57;
if (y < 0) {
y += 60;
}
y++;
// 节令偏移值
m *= 2;
// 时辰地支转时刻,子时按零点算
int h = LunarUtil.find(timeGanZhi.substring(1), LunarUtil.ZHI, -1) * 2;
List<int> hours = [h];
if (0 == h && 2 == sect) {
hours.add(23);
}
for (int hour in hours) {
for (int y in years) {
int maxYear = y + 3;
int year = y;
int month = 11;
if (year < baseYear) {
year = baseYear;
month = 1;
}
Solar solar = Solar.fromYmdHms(year, month, 1, hour, 0, 0);
while (solar.getYear() <= maxYear) {
Lunar lunar = solar.getLunar();
String dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
if (lunar.getYearInGanZhiExact() == yearGanZhi && lunar.getMonthInGanZhiExact() == monthGanZhi && dgz == dayGanZhi && lunar.getTimeInGanZhi() == timeGanZhi) {
l.add(solar);
break;
int startYear = baseYear - 1;

// 结束年
int endYear = DateTime
.now()
.toLocal()
.year;

while (y <= endYear) {
if (y >= startYear) {
// 立春为寅月的开始
// 节令推移,年干支和月干支就都匹配上了
Solar solarTime = Lunar.fromYmd(y, 1, 1).getJieQiTable()[Lunar
.JIE_QI_IN_USE[4 + m]]!;
if (solarTime.getYear() >= baseYear) {
// 日干支和节令干支的偏移值
Lunar lunar = solarTime.getLunar();
String dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar
.getDayInGanZhiExact();
int d = LunarUtil.getJiaZiIndex(dayGanZhi) -
LunarUtil.getJiaZiIndex(dgz);
if (d < 0) {
d += 60;
}
if (d > 0) {
// 从节令推移天数
solarTime = solarTime.next(d);
}
for (int hour in hours) {
int mi = 0;
int s = 0;
if (d == 0 && hour == solarTime.getHour()) {
// 如果正好是节令当天,且小时和节令的小时数相等的极端情况,把分钟和秒钟带上
mi = solarTime.getMinute();
s = solarTime.getSecond();
}
// 验证一下
Solar solar = Solar.fromYmdHms(
solarTime.getYear(), solarTime.getMonth(), solarTime.getDay(),
hour, mi, s);
lunar = solar.getLunar();
dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar
.getDayInGanZhiExact();
if (lunar.getYearInGanZhiExact() == yearGanZhi &&
lunar.getMonthInGanZhiExact() == monthGanZhi &&
dgz == dayGanZhi && lunar.getTimeInGanZhi() == timeGanZhi) {
l.add(solar);
}
}
solar = solar.next(1);
}
}
y += 60;
}
return l;
}
11 changes: 10 additions & 1 deletion lib/calendar/util/LunarUtil.dart

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: lunar
description: a calendar library for Solar and Chinese Lunar
version: 1.3.10
version: 1.3.11
homepage: https://github.com/6tail/lunar-flutter

environment:
12 changes: 6 additions & 6 deletions test/EightChar_test.dart
Original file line number Diff line number Diff line change
@@ -323,8 +323,8 @@ void main() {
}

List<String> expected = [];
expected.add('1976-09-21 12:00:00');
expected.add('1916-10-06 12:00:00');
expected.add('1976-09-21 12:00:00');
expect(actual, expected);
});

@@ -348,8 +348,8 @@ void main() {
}

List<String> expected = [];
expected.add('1999-07-21 16:00:00');
expected.add('1939-08-05 16:00:00');
expected.add('1999-07-21 16:00:00');
expect(actual, expected);
});

@@ -361,8 +361,8 @@ void main() {
}

List<String> expected = [];
expected.add('1960-12-17 12:00:00');
expected.add('1901-01-01 12:00:00');
expected.add('1960-12-17 12:00:00');
expect(actual, expected);
});

@@ -374,8 +374,8 @@ void main() {
}

List<String> expected = [];
expected.add('2020-07-21 22:00:00');
expected.add('1960-08-05 22:00:00');
expected.add('2020-07-21 22:00:00');
expect(actual, expected);
});

@@ -387,8 +387,8 @@ void main() {
}

List<String> expected = [];
expected.add('2023-02-24 23:00:00');
expected.add('1843-02-08 23:00:00');
expected.add('2023-02-24 23:00:00');
expect(actual, expected);
});

@@ -400,8 +400,8 @@ void main() {
}

List<String> expected = [];
expected.add('1960-01-15 16:00:00');
expected.add('1900-01-29 16:00:00');
expected.add('1960-01-15 16:00:00');
expect(actual, expected);
});

0 comments on commit 35f4af5

Please sign in to comment.