Browse Source

fix: lunar first day month calculation

vsme 9 months ago
parent
commit
1beedc83f4
2 changed files with 23 additions and 6 deletions
  1. 1 1
      src/solar_lunar/index.ts
  2. 22 5
      test/solar_lunar/index.test.ts

+ 1 - 1
src/solar_lunar/index.ts

@@ -146,7 +146,7 @@ export const getLunarDate = (date: ConfigType): LunarDateDetail => {
   leap = yearLeapMonth(i); // 闰哪个月
   leap = yearLeapMonth(i); // 闰哪个月
   lunarDate[6] = 0; // 闰月标记,初始为0
   lunarDate[6] = 0; // 闰月标记,初始为0
 
 
-  for (let j = 1; j < 13 && offset > 0; j++) {
+  for (let j = 1; j < 13 && offset >= 0; j++) {
     if (leap > 0 && j === (leap + 1) && lunarDate[6] === 0) {
     if (leap > 0 && j === (leap + 1) && lunarDate[6] === 0) {
       --j;
       --j;
       lunarDate[6] = 1;
       lunarDate[6] = 1;

+ 22 - 5
test/solar_lunar/index.test.ts

@@ -6,7 +6,24 @@ import {
 
 
 describe("solar_lunar", () => {
 describe("solar_lunar", () => {
   test("getLunarDate should return correct lunar date for a given solar date", () => {
   test("getLunarDate should return correct lunar date for a given solar date", () => {
-    let result = getLunarDate("2057-09-28");
+    // 闰月第一天
+    let result = getLunarDate("2014-10-24");
+    expect(result).toEqual({
+      date: '2014-10-24',
+      lunarYear: 2014,
+      lunarMon: 9,
+      lunarDay: 1,
+      isLeap: true,
+      zodiac: '马',
+      yearCyl: '甲午',
+      monCyl: '甲戌',
+      dayCyl: '戊辰',
+      lunarYearCN: '二零一四',
+      lunarMonCN: '九月',
+      lunarDayCN: '初一'
+    })
+
+    result = getLunarDate("2057-09-28");
     expect(result).toEqual({
     expect(result).toEqual({
       date: "2057-09-28",
       date: "2057-09-28",
       lunarYear: 2057,
       lunarYear: 2057,
@@ -87,15 +104,15 @@ describe("solar_lunar", () => {
       {
       {
         date: "2001-05-23",
         date: "2001-05-23",
         lunarYear: 2001,
         lunarYear: 2001,
-        lunarMon: 5,
+        lunarMon: 4,
         lunarDay: 1,
         lunarDay: 1,
-        isLeap: false,
+        isLeap: true,
         zodiac: "蛇",
         zodiac: "蛇",
         yearCyl: "辛巳",
         yearCyl: "辛巳",
-        monCyl: "甲午",
+        monCyl: "癸巳",
         dayCyl: "丙戌",
         dayCyl: "丙戌",
         lunarYearCN: "二零零一",
         lunarYearCN: "二零零一",
-        lunarMonCN: "月",
+        lunarMonCN: "月",
         lunarDayCN: "初一",
         lunarDayCN: "初一",
       },
       },
       {
       {