Skip to content

Commit

Permalink
v1.1.3 优化代码;修复吉神宜趋、凶神宜忌错误。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Sep 27, 2024
1 parent 02a4136 commit 4c1e0a5
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 105 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@
## [1.1.2] - 2024-08-19
1. 优化:十神计算逻辑。
2. 修复:获取起运时间报错的问题。

## [1.1.3] - 2024-09-27
1. 优化:代码。
2. 修复:吉神宜趋、凶神宜忌错误。
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Tyme是一个非常强大的日历工具库,可以看作 [Lunar](https://6tail
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>tyme4j</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cn.6tail</groupId>
<artifactId>tyme4j</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>
<packaging>jar</packaging>

<name>${project.artifactId}</name>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/tyme/culture/God.java

Large diffs are not rendered by default.

37 changes: 13 additions & 24 deletions src/main/java/com/tyme/culture/Taboo.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ public Taboo next(int n) {
return fromIndex(nextIndex(n));
}

private static List<Taboo> getTaboos(String[] data, int supIndex, int subIndex, int index) {
List<Taboo> l = new ArrayList<>();
String d = data[supIndex].split(";", -1)[subIndex].split(",", -1)[index];
for (int i = 0, j = d.length(); i < j; i += 2) {
l.add(Taboo.fromIndex(Integer.parseInt(d.substring(i, i + 2), 16)));
}
return l;
}

/**
* 日宜
*
Expand All @@ -79,12 +88,7 @@ public Taboo next(int n) {
* @return 宜忌列表
*/
public static List<Taboo> getDayRecommends(SixtyCycle month, SixtyCycle day) {
List<Taboo> l = new ArrayList<>();
String data = dayTaboo[month.getEarthBranch().getIndex()].split(";", -1)[day.getIndex()].split(",", -1)[0];
for (int i = 0, j = data.length(); i < j; i += 2) {
l.add(Taboo.fromIndex(Integer.parseInt(data.substring(i, i + 2), 16)));
}
return l;
return getTaboos(dayTaboo, month.getEarthBranch().getIndex(), day.getIndex(), 0);
}

/**
Expand All @@ -95,12 +99,7 @@ public static List<Taboo> getDayRecommends(SixtyCycle month, SixtyCycle day) {
* @return 宜忌列表
*/
public static List<Taboo> getDayAvoids(SixtyCycle month, SixtyCycle day) {
List<Taboo> l = new ArrayList<>();
String data = dayTaboo[month.getEarthBranch().getIndex()].split(";", -1)[day.getIndex()].split(",", -1)[1];
for (int i = 0, j = data.length(); i < j; i += 2) {
l.add(Taboo.fromIndex(Integer.parseInt(data.substring(i, i + 2), 16)));
}
return l;
return getTaboos(dayTaboo, month.getEarthBranch().getIndex(), day.getIndex(), 1);
}

/**
Expand All @@ -111,12 +110,7 @@ public static List<Taboo> getDayAvoids(SixtyCycle month, SixtyCycle day) {
* @return 宜忌列表
*/
public static List<Taboo> getHourRecommends(SixtyCycle day, SixtyCycle hour) {
List<Taboo> l = new ArrayList<>();
String data = hourTaboo[hour.getEarthBranch().getIndex()].split(";", -1)[day.getIndex()].split(",", -1)[0];
for (int i = 0, j = data.length(); i < j; i += 2) {
l.add(Taboo.fromIndex(Integer.parseInt(data.substring(i, i + 2), 16)));
}
return l;
return getTaboos(hourTaboo, hour.getEarthBranch().getIndex(), day.getIndex(), 0);
}

/**
Expand All @@ -127,11 +121,6 @@ public static List<Taboo> getHourRecommends(SixtyCycle day, SixtyCycle hour) {
* @return 宜忌列表
*/
public static List<Taboo> getHourAvoids(SixtyCycle day, SixtyCycle hour) {
List<Taboo> l = new ArrayList<>();
String data = hourTaboo[hour.getEarthBranch().getIndex()].split(";", -1)[day.getIndex()].split(",", -1)[1];
for (int i = 0, j = data.length(); i < j; i += 2) {
l.add(Taboo.fromIndex(Integer.parseInt(data.substring(i, i + 2), 16)));
}
return l;
return getTaboos(hourTaboo, hour.getEarthBranch().getIndex(), day.getIndex(), 1);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/tyme/culture/fetus/FetusDay.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public String getName() {
/**
* 内外
*
* @return
* @return 内外
*/
public Side getSide() {
return side;
Expand Down
43 changes: 2 additions & 41 deletions src/main/java/com/tyme/jd/JulianDay.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,46 +68,7 @@ public JulianDay next(int n) {
* @return 公历日
*/
public SolarDay getSolarDay() {
int d = (int) (this.day + 0.5);
double f = this.day + 0.5 - d;

if (d >= 2299161) {
int c = (int) ((d - 1867216.25) / 36524.25);
d += 1 + c - (int) (c * 1D / 4);
}
d += 1524;
int year = (int) ((d - 122.1) / 365.25);
d -= (int) (365.25 * year);
int month = (int) (d * 1D / 30.601);
d -= (int) (30.601 * month);
int day = d;
if (month > 13) {
month -= 13;
year -= 4715;
} else {
month -= 1;
year -= 4716;
}
f *= 24;
int hour = (int) f;

f -= hour;
f *= 60;
int minute = (int) f;

f -= minute;
f *= 60;
int second = (int) Math.round(f);
if (second > 59) {
minute++;
}
if (minute > 59) {
hour++;
}
if (hour > 23) {
day += 1;
}
return SolarDay.fromYmd(year, month, day);
return getSolarTime().getSolarDay();
}

/**
Expand Down Expand Up @@ -156,7 +117,7 @@ public SolarTime getSolarTime() {
}
if (hour > 23) {
hour -= 24;
day += 1;
day++;
}
return SolarTime.fromYmdHms(year, month, day, hour, minute, second);
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/tyme/lunar/LunarHour.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public LunarDay getLunarDay() {
* @return 年
*/
public int getYear() {
return day.getLunarMonth().getYear();
return day.getYear();
}

/**
Expand All @@ -105,7 +105,7 @@ public int getYear() {
* @return 月
*/
public int getMonth() {
return day.getLunarMonth().getMonthWithLeap();
return day.getMonth();
}

/**
Expand Down Expand Up @@ -163,6 +163,9 @@ public int getIndexInDay() {
}

public LunarHour next(int n) {
if (n == 0) {
return fromYmdHms(getYear(), getMonth(), getDay(), hour, minute, second);
}
int h = hour + n * 2;
int diff = h < 0 ? -1 : 1;
int hour = Math.abs(h);
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/tyme/solar/SolarHalfYear.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ public String toString() {
}

public SolarHalfYear next(int n) {
if (n == 0) {
return fromIndex(getYear(), index);
}
int i = index + n;
int y = getYear() + i / 2;
i %= 2;
if (i < 0) {
i += 2;
y -= 1;
int i = index;
int y = getYear();
if (n != 0) {
i += n;
y += i / 2;
i %= 2;
if (i < 0) {
i += 2;
y -= 1;
}
}
return fromIndex(y, i);
}
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/tyme/solar/SolarMonth.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,16 @@ public String toString() {
}

public SolarMonth next(int n) {
if (n == 0) {
return fromYm(getYear(), month);
}
int m = month + n;
int y = getYear() + m / 12;
m %= 12;
if (m < 1) {
m += 12;
y--;
int m = month;
int y = getYear();
if (n != 0) {
m += n;
y += m / 12;
m %= 12;
if (m < 1) {
m += 12;
y--;
}
}
return fromYm(y, m);
}
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/tyme/solar/SolarSeason.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ public String toString() {
}

public SolarSeason next(int n) {
if (n == 0) {
return fromIndex(getYear(), index);
}
int i = index + n;
int y = getYear() + i / 4;
i %= 4;
if (i < 0) {
i += 4;
y -= 1;
int i = index;
int y = getYear();
if (n != 0) {
i += n;
y += i / 4;
i %= 4;
if (i < 0) {
i += 4;
y -= 1;
}
}
return fromIndex(y, i);
}
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/tyme/solar/SolarWeek.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,10 @@ public String toString() {

public SolarWeek next(int n) {
int startIndex = start.getIndex();
if (n == 0) {
return fromYm(getYear(), getMonth(), index, startIndex);
}
int d = index + n;
int d = index;
SolarMonth m = month;
if (n > 0) {
d += n;
int weekCount = m.getWeekCount(startIndex);
while (d >= weekCount) {
d -= weekCount;
Expand All @@ -146,7 +144,8 @@ public SolarWeek next(int n) {
}
weekCount = m.getWeekCount(startIndex);
}
} else {
} else if (n < 0) {
d += n;
while (d < 0) {
if (!SolarDay.fromYmd(m.getYear(), m.getMonth(), 1).getWeek().equals(start)) {
d -= 1;
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/com/tyme/test/EightCharTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ public void test14() {
Assert.assertEquals(2001, decadeFortune.getStartLunarYear().getYear());
// 结束年
Assert.assertEquals(2010, decadeFortune.getEndLunarYear().getYear());
System.out.println(decadeFortune.getEndLunarYear());
// 干支
Assert.assertEquals("庚子", decadeFortune.getName());
// 下一大运
Expand Down Expand Up @@ -709,4 +710,11 @@ public void test44() {
// 童限结束(即开始起运)的公历时刻
Assert.assertEquals("1998年3月1日 19:47:17", childLimit.getEndTime().toString());
}

@Test
public void test45() {
// 童限
ChildLimit childLimit = ChildLimit.fromSolarTime(SolarTime.fromYmdHms(1994, 10, 16, 1, 0, 0), Gender.MAN);
Assert.assertEquals("壬午", childLimit.getStartDecadeFortune().getStartLunarYear().getSixtyCycle().getName());
}
}
21 changes: 21 additions & 0 deletions src/test/java/com/tyme/test/GodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,25 @@ public void test3() {
Assert.assertEquals(Arrays.asList("三丧", "鬼哭"), xiong);
}

@Test
public void test4() {
LunarDay lunar = SolarDay.fromYmd(2024, 9, 27).getLunarDay();
List<God> gods = lunar.getGods();
List<String> ji = new ArrayList<>();
for (God god : gods) {
if ("吉".equals(god.getLuck().getName())) {
ji.add(god.getName());
}
}

List<String> xiong = new ArrayList<>();
for (God god : gods) {
if ("凶".equals(god.getLuck().getName())) {
xiong.add(god.getName());
}
}
Assert.assertEquals(Arrays.asList("月空", "不将", "福生", "金匮", "鸣吠"), ji);
Assert.assertEquals(Arrays.asList("天罡", "大时", "大败", "咸池", "天贼", "九坎", "九焦"), xiong);
}

}
4 changes: 4 additions & 0 deletions src/test/java/com/tyme/test/SolarTermTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,8 @@ public void test4() {
Assert.assertEquals("冬至第1天", SolarDay.fromYmd(2023, 12, 22).getTermDay().toString());
}

@Test
public void test5() {
Assert.assertEquals("2024年1月6日 04:49:20", SolarTerm.fromName(2024, "小寒").getJulianDay().getSolarTime().toString());
}
}

0 comments on commit 4c1e0a5

Please sign in to comment.