From b83fbe8cc7c3190b2c84e1bec2f312936240eeed Mon Sep 17 00:00:00 2001 From: 6tail <6tail@6tail.cn> Date: Sun, 23 May 2021 22:33:25 +0800 Subject: [PATCH] =?UTF-8?q?v1.2.3=20=E4=BF=AE=E6=AD=A32016=E5=B9=B4?= =?UTF-8?q?=E5=9B=BD=E5=BA=86=E8=8A=82=E6=95=B0=E6=8D=AE=EF=BC=9B=E5=88=A0?= =?UTF-8?q?=E9=99=A45=E6=9C=8823=E6=97=A5=E4=B8=96=E7=95=8C=E8=AF=BB?= =?UTF-8?q?=E4=B9=A6=E6=97=A5=EF=BC=9B=E4=BF=AE=E5=A4=8Dv1.2.0=E5=92=8Cv1.?= =?UTF-8?q?2.1=E7=9A=84=E9=87=8D=E5=A4=A7bug=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + __tests__/EightChar.test.js | 11 +- __tests__/Holiday.test.js | 4 + __tests__/Lunar.test.js | 181 +++++++++++++++++++++- __tests__/Yun.test.js | 23 ++- lunar.js | 294 ++++++++++++++++-------------------- package.json | 2 +- 7 files changed, 345 insertions(+), 172 deletions(-) diff --git a/.gitignore b/.gitignore index 1062418..ef1714b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .idea/ *.iml +node_modules/ +package-lock.json diff --git a/__tests__/EightChar.test.js b/__tests__/EightChar.test.js index 81f0ec4..a903a73 100644 --- a/__tests__/EightChar.test.js +++ b/__tests__/EightChar.test.js @@ -1,4 +1,4 @@ -var {Solar} = require('../lunar'); +var {Solar, Lunar} = require('../lunar'); test('test1', () => { const solar = Solar.fromYmdHms(2005, 12, 23, 8, 37, 0); @@ -53,6 +53,15 @@ test('test4', () => { expect(eightChar.getTime()).toBe('辛亥'); }); +test('test5', () => { + const lunar = Lunar.fromYmdHms(2019,12,12,11,22,0); + const eightChar = lunar.getEightChar(); + expect(eightChar.getYear()).toBe('己亥'); + expect(eightChar.getMonth()).toBe('丁丑'); + expect(eightChar.getDay()).toBe('戊申'); + expect(eightChar.getTime()).toBe('戊午'); +}); + test('地支藏干', () => { const solar = Solar.fromYmdHms(2005, 12, 23, 8, 37, 0); const lunar = solar.getLunar(); diff --git a/__tests__/Holiday.test.js b/__tests__/Holiday.test.js index 85702ea..53d4ddc 100644 --- a/__tests__/Holiday.test.js +++ b/__tests__/Holiday.test.js @@ -38,3 +38,7 @@ test('test1', () => { expect(HolidayUtil.getHoliday('2021-11-11') + '').toBe('2021-11-11 结婚纪念日 2021-11-11'); expect(HolidayUtil.getHoliday('2021-12-01') + '').toBe('2021-12-01 她的生日 2021-12-01'); }); + +test('test2', () => { + expect(HolidayUtil.getHoliday('2016-10-04').getTarget()).toBe('2016-10-01'); +}); diff --git a/__tests__/Lunar.test.js b/__tests__/Lunar.test.js index c0a88bb..cfe1c34 100644 --- a/__tests__/Lunar.test.js +++ b/__tests__/Lunar.test.js @@ -1,4 +1,4 @@ -var {Solar} = require('../lunar'); +var {Solar, Lunar} = require('../lunar'); test('干支', () => { let solar = Solar.fromYmdHms(2020, 1, 1, 13, 22, 0); @@ -182,3 +182,182 @@ expect(lunar.getYearInGanZhiExact()).toBe('丙寅'); expect(lunar.getMonthInGanZhi()).toBe('癸巳'); expect(lunar.getMonthInGanZhiExact()).toBe('癸巳'); }); + +test('test8', () => { + const lunar = Lunar.fromYmdHms(2020,12,10,13,0,0); + expect(lunar.toString()).toBe('二〇二〇年腊月初十'); + expect(lunar.getSolar().toString()).toBe('2021-01-22'); +}); + +test('test9', () => { + const lunar = Lunar.fromYmdHms(1500,1,1,12,0,0); + expect(lunar.getSolar().toString()).toBe('1500-01-31'); +}); + +test('test10', () => { + const lunar = Lunar.fromYmdHms(1500,12,29,12,0,0); + expect(lunar.getSolar().toString()).toBe('1501-01-18'); +}); + +test('test11', () => { + const solar = Solar.fromYmdHms(1500,1,1,12,0,0); + expect(solar.getLunar().toString()).toBe('一四九九年腊月初一'); +}); + +test('test12', () => { + const solar = Solar.fromYmdHms(1500,12,31,12,0,0); + expect(solar.getLunar().toString()).toBe('一五〇〇年腊月十一'); +}); + +test('test13', () => { + const solar = Solar.fromYmdHms(1582,10,4,12,0,0); + expect(solar.getLunar().toString()).toBe('一五八二年九月十八'); +}); + +test('test14', () => { + const solar = Solar.fromYmdHms(1582,10,15,12,0,0); + expect(solar.getLunar().toString()).toBe('一五八二年九月十九'); +}); + +test('test15', () => { + const lunar = Lunar.fromYmdHms(1582,9,18,12,0,0); + expect(lunar.getSolar().toString()).toBe('1582-10-04'); +}); + +test('test16', () => { + const lunar = Lunar.fromYmdHms(1582,9,19,12,0,0); + expect(lunar.getSolar().toString()).toBe('1582-10-15'); +}); + +test('test17', () => { + const lunar = Lunar.fromYmdHms(2019,12,12,11,22,0); + expect(lunar.getSolar().toString()).toBe('2020-01-06'); +}); + +test('test18', () => { + const solar = Solar.fromYmdHms(2020,2,4,13,22,0); + const lunar = solar.getLunar(); + expect(lunar.getYearInGanZhi()).toBe('庚子'); + expect(lunar.getYearInGanZhiByLiChun()).toBe('庚子'); + expect(lunar.getYearInGanZhiExact()).toBe('己亥'); + expect(lunar.getMonthInGanZhi()).toBe('戊寅'); + expect(lunar.getMonthInGanZhiExact()).toBe('丁丑'); +}); + +test('test19', () => { + const solar = Solar.fromYmdHms(2019, 2, 8, 13, 22, 0); + const lunar = solar.getLunar(); + expect(lunar.getYearInGanZhi()).toBe('己亥'); + expect(lunar.getYearInGanZhiByLiChun()).toBe('己亥'); + expect(lunar.getYearInGanZhiExact()).toBe('己亥'); + expect(lunar.getMonthInGanZhi()).toBe('丙寅'); + expect(lunar.getMonthInGanZhiExact()).toBe('丙寅'); +}); + +test('test20', () => { + const solar = Solar.fromYmdHms(1988, 2, 15, 23, 30,0); + const lunar = solar.getLunar(); + expect(lunar.getYearInGanZhi()).toBe('丁卯'); + expect(lunar.getYearInGanZhiByLiChun()).toBe('戊辰'); + expect(lunar.getYearInGanZhiExact()).toBe('戊辰'); +}); + +test('test21', () => { + const solar = Solar.fromYmd(1988, 2, 15); + const lunar = solar.getLunar(); + expect(lunar.getYearInGanZhi()).toBe('丁卯'); +}); + +test('test22', () => { + const solar = Solar.fromYmd(2012, 12, 27); + const lunar = solar.getLunar(); + expect(lunar.getYearInGanZhi()).toBe('壬辰'); + expect(lunar.getMonthInGanZhi()).toBe('壬子'); + expect(lunar.getDayInGanZhi()).toBe('壬戌'); +}); + +test('test23', () => { + const solar = Solar.fromYmd(2012, 12, 20); + const lunar = solar.getLunar(); + expect(lunar.getYearInGanZhi()).toBe('壬辰'); + expect(lunar.getMonthInGanZhi()).toBe('壬子'); + expect(lunar.getDayInGanZhi()).toBe('乙卯'); +}); + +test('test24', () => { + const solar = Solar.fromYmd(2012, 11, 20); + const lunar = solar.getLunar(); + expect(lunar.getYearInGanZhi()).toBe('壬辰'); + expect(lunar.getMonthInGanZhi()).toBe('辛亥'); + expect(lunar.getDayInGanZhi()).toBe('乙酉'); +}); + +test('test25', () => { + const solar = Solar.fromYmd(2012, 10, 20); + const lunar = solar.getLunar(); + expect(lunar.getYearInGanZhi()).toBe('壬辰'); + expect(lunar.getMonthInGanZhi()).toBe('庚戌'); + expect(lunar.getDayInGanZhi()).toBe('甲寅'); +}); + +test('test26', () => { + const solar = Solar.fromYmd(2012, 9, 20); + const lunar = solar.getLunar(); + expect(lunar.getYearInGanZhi()).toBe('壬辰'); + expect(lunar.getMonthInGanZhi()).toBe('己酉'); + expect(lunar.getDayInGanZhi()).toBe('甲申'); +}); + +test('test26', () => { + const solar = Solar.fromYmd(2012, 8, 5); + const lunar = solar.getLunar(); + expect(lunar.getDayInGanZhi()).toBe('戊戌'); +}); + +test('test27', () => { + const solar = Solar.fromYmd(2000, 2, 2); + const lunar = solar.getLunar(); + expect(lunar.getDayInGanZhi()).toBe('庚寅'); +}); + +test('test28', () => { + const solar = Solar.fromYmd(1996, 1, 16); + const lunar = solar.getLunar(); + expect(lunar.getDayInGanZhi()).toBe('壬子'); +}); + +test('test29', () => { + const solar = Solar.fromYmd(1997, 2, 16); + const lunar = solar.getLunar(); + expect(lunar.getDayInGanZhi()).toBe('己丑'); +}); + +test('test30', () => { + const solar = Solar.fromYmd(1998, 3, 16); + const lunar = solar.getLunar(); + expect(lunar.getDayInGanZhi()).toBe('壬戌'); +}); + +test('test31', () => { + const solar = Solar.fromYmd(1999, 4, 16); + const lunar = solar.getLunar(); + expect(lunar.getDayInGanZhi()).toBe('戊戌'); +}); + +test('test32', () => { + const solar = Solar.fromYmd(2000, 7, 16); + const lunar = solar.getLunar(); + expect(lunar.getDayInGanZhi()).toBe('乙亥'); +}); + +test('test33', () => { + const solar = Solar.fromYmd(2000, 1, 6); + const lunar = solar.getLunar(); + expect(lunar.getDayInGanZhi()).toBe('癸亥'); +}); + +test('test34', () => { + const solar = Solar.fromYmd(2000, 1, 9); + const lunar = solar.getLunar(); + expect(lunar.getDayInGanZhi()).toBe('丙寅'); +}); diff --git a/__tests__/Yun.test.js b/__tests__/Yun.test.js index 9217d64..1cb0052 100644 --- a/__tests__/Yun.test.js +++ b/__tests__/Yun.test.js @@ -1,4 +1,4 @@ -var {Solar} = require('../lunar'); +var {Solar, Lunar} = require('../lunar'); it('起运', () => { const solar = Solar.fromYmdHms(1981, 1, 29, 23, 37, 0); @@ -10,3 +10,24 @@ it('起运', () => { expect(yun.getStartDay()).toBe(20); expect(yun.getStartSolar().toYmd()).toBe('1989-02-18'); }); + +it('test2', () => { + const lunar = Lunar.fromYmdHms(2019, 12, 12, 11, 22, 0); + const eightChar = lunar.getEightChar(); + const yun = eightChar.getYun(1); + expect(yun.getStartYear()).toBe(0); + expect(yun.getStartMonth()).toBe(1); + expect(yun.getStartDay()).toBe(0); + expect(yun.getStartSolar().toYmd()).toBe('2020-02-06'); +}); + +it('test3', () => { + const solar = Solar.fromYmdHms(2020, 1, 6, 11, 22, 0); + const lunar = solar.getLunar(); + const eightChar = lunar.getEightChar(); + const yun = eightChar.getYun(1); + expect(yun.getStartYear()).toBe(0); + expect(yun.getStartMonth()).toBe(1); + expect(yun.getStartDay()).toBe(0); + expect(yun.getStartSolar().toYmd()).toBe('2020-02-06'); +}); diff --git a/lunar.js b/lunar.js index efd0b18..791d992 100644 --- a/lunar.js +++ b/lunar.js @@ -18,6 +18,7 @@ date.setMonth(m-1); date.setDate(d); } + date.setMilliseconds(0); return date; }, fromYmd:function(y,m,d){ @@ -67,6 +68,11 @@ return _fromYmdHms(year,month,day,hour,minute,second); }; var _fromYmdHms = function(y,m,d,hour,minute,second){ + if(y===1582&&m==10){ + if(d>=15){ + d -= 10; + } + } return { _p:{ year:y, @@ -167,7 +173,13 @@ return SolarUtil.XINGZUO[index]; }, toYmd:function(){ - return [this._p.year,(this._p.month<10?'0':'')+this._p.month,(this._p.day<10?'0':'')+this._p.day].join('-'); + var d = this._p.day; + if(this._p.year===1582&&this._p.month==10){ + if(d>=5){ + d += 10; + } + } + return [this._p.year,(this._p.month<10?'0':'')+this._p.month,(d<10?'0':'')+d].join('-'); }, toYmdHms:function(){ return this.toYmd()+' '+[(this._p.hour<10?'0':'')+this._p.hour,(this._p.minute<10?'0':'')+this._p.minute,(this._p.second<10?'0':'')+this._p.second].join(':'); @@ -344,15 +356,20 @@ var gExact = yearGanIndex; var zExact = yearZhiIndex; - //获取立春的阳历时刻 - var liChun = o['jieQi']['立春']; - var liChunYmd = liChun.toYmd(); - var liChunYmdHms = liChun.toYmdHms(); + var solarYear = solar.getYear(); var solarYmd = solar.toYmd(); var solarYmdHms = solar.toYmdHms(); + //获取立春的阳历时刻 + var liChun = o['jieQi']['立春']; + if (liChun.getYear() != solarYear) { + liChun = o['jieQi']['LI_CHUN']; + } + var liChunYmd = liChun.toYmd(); + var liChunYmdHms = liChun.toYmdHms(); + //阳历和阴历年份相同代表正月初一及以后 - if(year===solar.getYear()){ + if(year===solarYear){ //立春日期判断 if(solarYmd=liChunYmd) { g++; z++; @@ -373,96 +390,49 @@ zExact++; } } - if(yearGanIndex<0){ - yearGanIndex += 10; - } - if(yearGanIndex>=10){ - yearGanIndex-=10; - } - if(yearZhiIndex<0){ - yearZhiIndex += 12; - } - if(yearZhiIndex>=12){ - yearZhiIndex-=12; - } - if(g<0){ - g += 10; - } - if(g>=10){ - g-=10; - } - if(z<0){ - z += 12; - } - if(z>=12){ - z-=12; - } - if(gExact<0){ - gExact += 10; - } - if(gExact>=10){ - gExact-=10; - } - if(zExact<0){ - zExact += 12; - } - if(zExact>=12){ - zExact-=12; - } o['yearGanIndex'] = yearGanIndex; o['yearZhiIndex'] = yearZhiIndex; - o['yearGanIndexByLiChun'] = g; - o['yearZhiIndexByLiChun'] = z; - o['yearGanIndexExact'] = gExact; - o['yearZhiIndexExact'] = zExact; + o['yearGanIndexByLiChun'] = (g<0?g+10:g)%10; + o['yearZhiIndexByLiChun'] = (z<0?z+12:z)%12; + o['yearGanIndexExact'] = (gExact<0?gExact+10:gExact)%10; + o['yearZhiIndexExact'] = (zExact<0?zExact+12:zExact)%12; }; var _computeMonth = function(o,solar){ - var start = null,i,j,jie; + var start = null,i,j; var end; - var gOffset = ((o.yearGanIndexByLiChun%5+1)*2)%10; - var gOffsetExact = ((o.yearGanIndexExact%5+1)*2)%10; - //序号:大雪到小寒之间-2,小寒到立春之间-1,立春之后0 - var index = -2; - for(i=0,j=LunarUtil.JIE.length;i=symd&&ymd=symd&&ymd=stime&&time=stime&&time