Skip to content

Commit

Permalink
v1.3.11 修复命宫、身宫的错误。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Jan 30, 2024
1 parent 1411d80 commit e15a916
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.3.10</version>
<version>1.3.11</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ lunar is a calendar library for Solar and Chinese Lunar.
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.3.10</version>
<version>1.3.11</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<packaging>jar</packaging>
<version>1.3.10</version>
<version>1.3.11</version>
<name>${project.groupId}:${project.artifactId}</name>
<url>https://github.com/6tail/lunar-java</url>
<description>a calendar library for Solar and Chinese Lunar</description>
Expand Down
56 changes: 29 additions & 27 deletions src/main/java/com/nlf/calendar/EightChar.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,27 +501,27 @@ public String getTaiXiNaYin() {
public String getMingGong() {
int monthZhiIndex = 0;
int timeZhiIndex = 0;
String monthZhi = getMonthZhi();
String timeZhi = getTimeZhi();
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
String zhi = MONTH_ZHI[i];
if (lunar.getMonthZhiExact().equals(zhi)) {
if (monthZhi.equals(MONTH_ZHI[i])) {
monthZhiIndex = i;
break;
}
if (lunar.getTimeZhi().equals(zhi)) {
}
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
if (timeZhi.equals(MONTH_ZHI[i])) {
timeZhiIndex = i;
break;
}
}
int zhiIndex = 26 - (monthZhiIndex + timeZhiIndex);
if (zhiIndex > 12) {
zhiIndex -= 12;
}
int jiaZiIndex = LunarUtil.getJiaZiIndex(lunar.getMonthInGanZhiExact()) - (monthZhiIndex - zhiIndex);
if (jiaZiIndex >= 60) {
jiaZiIndex -= 60;
}
if (jiaZiIndex < 0) {
jiaZiIndex += 60;
int offset = monthZhiIndex + timeZhiIndex;
offset = (offset >= 14 ? 26 : 14) - offset;
int ganIndex = (lunar.getYearGanIndexExact() + 1) * 2 + offset;
while (ganIndex > 10) {
ganIndex -= 10;
}
return LunarUtil.JIA_ZI[jiaZiIndex];
return LunarUtil.GAN[ganIndex] + MONTH_ZHI[offset];
}

/**
Expand All @@ -541,27 +541,29 @@ public String getMingGongNaYin() {
public String getShenGong() {
int monthZhiIndex = 0;
int timeZhiIndex = 0;
String monthZhi = getMonthZhi();
String timeZhi = getTimeZhi();
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
String zhi = MONTH_ZHI[i];
if (lunar.getMonthZhiExact().equals(zhi)) {
if (monthZhi.equals(MONTH_ZHI[i])) {
monthZhiIndex = i;
break;
}
if (lunar.getTimeZhi().equals(zhi)) {
}
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
if (timeZhi.equals(LunarUtil.ZHI[i])) {
timeZhiIndex = i;
break;
}
}
int zhiIndex = 2 + monthZhiIndex + timeZhiIndex;
if (zhiIndex > 12) {
zhiIndex -= 12;
int offset = monthZhiIndex + timeZhiIndex;
while (offset > 12) {
offset -= 12;
}
int jiaZiIndex = LunarUtil.getJiaZiIndex(lunar.getMonthInGanZhiExact()) - (monthZhiIndex - zhiIndex);
if (jiaZiIndex >= 60) {
jiaZiIndex -= 60;
}
if (jiaZiIndex < 0) {
jiaZiIndex += 60;
int ganIndex = (lunar.getYearGanIndexExact() + 1) * 2 + (offset % 12);
while (ganIndex > 10) {
ganIndex -= 10;
}
return LunarUtil.JIA_ZI[jiaZiIndex];
return LunarUtil.GAN[ganIndex] + MONTH_ZHI[offset];
}

/**
Expand Down
60 changes: 58 additions & 2 deletions src/test/java/test/BaZiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void testShenGong() {
@Test
public void testShenGong1() {
Lunar lunar = new Solar(1994, 12, 6, 2, 0, 0).getLunar();
Assert.assertEquals("身宫", "丁丑", lunar.getEightChar().getShenGong());
Assert.assertEquals("身宫", "乙丑", lunar.getEightChar().getShenGong());
}

@Test
Expand Down Expand Up @@ -412,7 +412,7 @@ public void test17() {
Solar solar = new Solar(1986, 5, 29, 13, 37, 0);
Lunar lunar = solar.getLunar();
EightChar eightChar = lunar.getEightChar();
Assert.assertEquals("辛丑", eightChar.getShenGong());
Assert.assertEquals("己丑", eightChar.getShenGong());
}

@Test
Expand All @@ -437,4 +437,60 @@ public void test19() {
Assert.assertEquals(expected, actual);
}

@Test
public void test20() {
List<Solar> l = Solar.fromBaZi("乙未","己卯","丁丑","甲辰");
List<String> actual = new ArrayList<String>();
for (Solar solar : l) {
actual.add(solar.toYmdHms());
}

List<String> expected = new ArrayList<String>();
expected.add("1955-03-17 08:00:00");
Assert.assertEquals(expected, actual);
}

@Test
public void test21() {
Lunar lunar = Solar.fromYmdHms(2024, 1, 29, 9, 30, 0).getLunar();
EightChar eightChar = lunar.getEightChar();
Assert.assertEquals("命宫", "癸亥", eightChar.getMingGong());
Assert.assertEquals("身宫", "己未", eightChar.getShenGong());
}

@Test
public void test22() {
Assert.assertEquals("身宫", "丙寅", Solar.fromYmdHms(1990, 1, 27, 0, 0, 0).getLunar().getEightChar().getShenGong());
}

@Test
public void test23() {
Assert.assertEquals("甲戌", Solar.fromYmdHms(2019, 3, 7, 8, 0, 0).getLunar().getEightChar().getMingGong());
}

@Test
public void test24() {
Assert.assertEquals("丁丑", Solar.fromYmdHms(2019, 3, 27, 2, 0, 0).getLunar().getEightChar().getMingGong());
}

@Test
public void test25() {
Assert.assertEquals("丙寅", Lunar.fromYmdHms(1994, 5, 20, 18, 0 ,0).getEightChar().getMingGong());
}

@Test
public void test26() {
Lunar lunar = Solar.fromYmdHms(1986, 2, 16, 8, 0, 0).getLunar();
EightChar eightChar = lunar.getEightChar();
Assert.assertEquals("命宫", "己亥", eightChar.getMingGong());
Assert.assertEquals("身宫", "乙未", eightChar.getShenGong());
}

@Test
public void test27() {
Lunar lunar = Solar.fromYmdHms(1972, 11, 27, 10, 0, 0).getLunar();
EightChar eightChar = lunar.getEightChar();
Assert.assertEquals("身宫", "乙巳", eightChar.getShenGong());
}

}

0 comments on commit e15a916

Please sign in to comment.