From 3389c33cb5ff2c54c92e157bbb9bd8d5459d7914 Mon Sep 17 00:00:00 2001 From: starainrt Date: Mon, 15 Sep 2025 23:40:09 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E8=A1=A5=E5=85=85=E5=86=9C=E5=8E=86?= =?UTF-8?q?=E8=A6=86=E7=9B=96=E5=88=B0=E5=85=AC=E5=85=83=E5=89=8D104?= =?UTF-8?q?=E5=B9=B4=202.=20=E4=BF=AE=E6=AD=A3=E6=97=B6=E5=8C=BA=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=E8=AE=A1=E7=AE=97=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- calendar/chinese.go | 14 +++++++++----- calendar/chineseHantoQing.go | 2 +- calendar/chineseLiaoJinYuan.go | 1 + calendar/chineseNanBeiChao.go | 4 ++++ calendar/chineseNanMing.go | 2 ++ calendar/chinese_test.go | 19 ++++++++++++------- 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/calendar/chinese.go b/calendar/chinese.go index 6e3ac2e..f9a8839 100644 --- a/calendar/chinese.go +++ b/calendar/chinese.go @@ -14,6 +14,10 @@ import ( var tiangan = []string{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"} var dizhi = []string{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"} +func getCst() *time.Location { + return time.FixedZone("CST", 8*3600) +} + const ( JQ_春分 = 15 * iota JQ_清明 @@ -196,8 +200,8 @@ recalc: if useGoto { calcYear++ } - target := time.Date(calcYear, time.Month(month), day, 0, 0, 0, 0, time.Local) - spring := time.Date(year, time.Month(int(springMonth)), int(springDay), 0, 0, 0, 0, time.Local) + target := time.Date(calcYear, time.Month(month), day, 0, 0, 0, 0, getCst()) + spring := time.Date(year, time.Month(int(springMonth)), int(springDay), 0, 0, 0, 0, getCst()) diffDay := int(target.Sub(spring).Hours() / 24) lunarMonth := 1 totalDay := 0 @@ -252,7 +256,7 @@ func rapidSolarModern(year, month, day int, isLeap bool) time.Time { magic := int32(upper[idx])<<8 + int32(lower[idx]) springMonth := (magic&0x800000)>>23 + 1 springDay := (magic & 0x7FFFFF) >> 18 - spring := time.Date(year, time.Month(int(springMonth)), int(springDay), 0, 0, 0, 0, time.Local) + spring := time.Date(year, time.Month(int(springMonth)), int(springDay), 0, 0, 0, 0, getCst()) lunarMonth := 1 totalDay := 0 leap := false @@ -483,7 +487,7 @@ func GanZhiOfYear(year int) string { // GanZhiOfDay func GanZhiOfDay(t time.Time) string { - jde := Date2JDE(time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)) + jde := Date2JDE(time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, getCst())) diff := int(jde - 2451550.5) if diff >= 0 { return tiangan[diff%10] + dizhi[diff%12] @@ -508,7 +512,7 @@ func commonGanZhiOfMonth(year, month int) string { } func ganZhiOfDayIndex(t time.Time) (int, int) { - jde := Date2JDE(time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)) + jde := Date2JDE(time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, getCst())) diff := int(jde - 2451550.5) if diff >= 0 { return diff % 10, diff % 12 diff --git a/calendar/chineseHantoQing.go b/calendar/chineseHantoQing.go index 817c8be..d984c72 100644 --- a/calendar/chineseHantoQing.go +++ b/calendar/chineseHantoQing.go @@ -42,7 +42,7 @@ recalc: if useGoto { calcYear++ } - target := time.Date(calcYear, time.Month(month), day, 0, 0, 0, 0, time.Local) + target := time.Date(calcYear, time.Month(month), day, 0, 0, 0, 0, getCst()) diffDay := int(target.Sub(springDate).Hours() / 24) //go语言在; 1582年10月4日前,使用的是逆推格里高利历,与实际使用的儒略历有所不同,主要体现在百年闰年计算上! //儒略历修正 diff --git a/calendar/chineseLiaoJinYuan.go b/calendar/chineseLiaoJinYuan.go index d95f992..3d96923 100644 --- a/calendar/chineseLiaoJinYuan.go +++ b/calendar/chineseLiaoJinYuan.go @@ -393,6 +393,7 @@ func innerSolarToLunarLiaoJinYuan(date Time) Time { if year >= 947 && year <= 1279 { lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, liaoJinYuanCals) date.lunars = append(date.lunars, LunarTime{ + solarDate: date.Solar(), year: lyear, month: lmonth, day: lday, diff --git a/calendar/chineseNanBeiChao.go b/calendar/chineseNanBeiChao.go index 01933c7..9ba3acf 100644 --- a/calendar/chineseNanBeiChao.go +++ b/calendar/chineseNanBeiChao.go @@ -863,6 +863,7 @@ func innerSolarToLunarNanBeiChao(date Time) Time { if year >= 384 && year <= 417 { lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, houQinCals) date.lunars = append(date.lunars, LunarTime{ + solarDate: date.solarTime, year: lyear, month: lmonth, day: lday, @@ -876,6 +877,7 @@ func innerSolarToLunarNanBeiChao(date Time) Time { if year >= 398 && year <= 589 { lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, weiZhouSuiCals) date.lunars = append(date.lunars, LunarTime{ + solarDate: date.solarTime, year: lyear, month: lmonth, day: lday, @@ -889,6 +891,7 @@ func innerSolarToLunarNanBeiChao(date Time) Time { if year >= 397 && year <= 439 { lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, beiLiangCals) date.lunars = append(date.lunars, LunarTime{ + solarDate: date.solarTime, year: lyear, month: lmonth, day: lday, @@ -902,6 +905,7 @@ func innerSolarToLunarNanBeiChao(date Time) Time { if year >= 534 && year <= 577 { lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, dongWeiBeiQiCals) date.lunars = append(date.lunars, LunarTime{ + solarDate: date.solarTime, year: lyear, month: lmonth, day: lday, diff --git a/calendar/chineseNanMing.go b/calendar/chineseNanMing.go index 1bc590d..0ee4488 100644 --- a/calendar/chineseNanMing.go +++ b/calendar/chineseNanMing.go @@ -99,6 +99,7 @@ func innerSolarToLunarNanMing(date Time) Time { if year > 1644 && year < 1654 { lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, nanMingCals) date.lunars = append(date.lunars, LunarTime{ + solarDate: date.solarTime, year: lyear, month: lmonth, day: lday, @@ -112,6 +113,7 @@ func innerSolarToLunarNanMing(date Time) Time { if year > 1646 && year < 1684 { lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, nanMingCals) date.lunars = append(date.lunars, LunarTime{ + solarDate: date.solarTime, year: lyear, month: lmonth, day: lday, diff --git a/calendar/chinese_test.go b/calendar/chinese_test.go index b506cee..156a81a 100644 --- a/calendar/chinese_test.go +++ b/calendar/chinese_test.go @@ -61,7 +61,7 @@ func Test_ChineseCalendarModern(t *testing.T) { /* { var lyear int = v.Year - lmonth, lday, leap, desp := RapidSolarToLunar(time.Date(v.Year, time.Month(v.Month), v.Day, 0, 0, 0, 0, time.Local)) + lmonth, lday, leap, desp := RapidSolarToLunar(time.Date(v.Year, time.Month(v.Month), v.Day, 0, 0, 0, 0, getCst())) if lmonth > v.Month { lyear-- } @@ -102,7 +102,7 @@ func Test_ChineseCalendarModern2(t *testing.T) { } for _, v := range testData { { - res, err := SolarToLunar(time.Date(v.Year, time.Month(v.Month), v.Day, 0, 0, 0, 0, time.Local)) + res, err := SolarToLunar(time.Date(v.Year, time.Month(v.Month), v.Day, 0, 0, 0, 0, getCst())) if err != nil { t.Fatal(err) } @@ -126,7 +126,7 @@ func Test_ChineseCalendarModern2(t *testing.T) { /* { var lyear int = v.Year - lmonth, lday, leap, desp := RapidSolarToLunar(time.Date(v.Year, time.Month(v.Month), v.Day, 0, 0, 0, 0, time.Local)) + lmonth, lday, leap, desp := RapidSolarToLunar(time.Date(v.Year, time.Month(v.Month), v.Day, 0, 0, 0, 0, getCst())) if lmonth > v.Month { lyear-- } @@ -183,6 +183,7 @@ func Test_ChineseCalendarAncient(t *testing.T) { {Lyear: 700, Lmonth: 2, Lday: 6, Leap: false, Year: 700, Month: 1, Day: 1, Desc: "圣历三年腊月初六", GanZhiYear: "庚子", GanZhiMonth: "丁丑", GanZhiDay: "丙戌"}, {Lyear: 700, Lmonth: 12, Lday: 6, Leap: false, Year: 701, Month: 1, Day: 19, Desc: "圣历三年腊月初六", GanZhiYear: "庚子", GanZhiMonth: "己丑", GanZhiDay: "庚戌"}, {Lyear: 700, Lmonth: 11, Lday: 1, Leap: false, Year: 700, Month: 12, Day: 15, Desc: "圣历三年冬月初一", GanZhiYear: "庚子", GanZhiMonth: "戊子", GanZhiDay: "乙亥"}, + {Lyear: 1083, Lmonth: 10, Lday: 12, Leap: false, Year: 1083, Month: 11, Day: 24, Desc: "元丰六年十月十二", GanZhiYear: "癸亥", GanZhiMonth: "癸亥", GanZhiDay: "甲申"}, //格里高利历改革 {Lyear: 1582, Lmonth: 9, Lday: 18, Leap: false, Year: 1582, Month: 10, Day: 4, Desc: "万历十年九月十八", GanZhiYear: "壬午", GanZhiMonth: "庚戌", GanZhiDay: "癸酉"}, {Lyear: 1582, Lmonth: 9, Lday: 19, Leap: false, Year: 1582, Month: 10, Day: 15, Desc: "万历十年九月十九", GanZhiYear: "壬午", GanZhiMonth: "庚戌", GanZhiDay: "甲戌"}, @@ -191,7 +192,7 @@ func Test_ChineseCalendarAncient(t *testing.T) { } for _, v := range testData { { - dates, err := SolarToLunar(time.Date(v.Year, time.Month(v.Month), v.Day, 0, 0, 0, 0, time.Local)) + dates, err := SolarToLunar(time.Date(v.Year, time.Month(v.Month), v.Day, 0, 0, 0, 0, getCst())) if err != nil { t.Fatal(err) } @@ -240,10 +241,14 @@ func Test_ChineseCalendarAncient(t *testing.T) { } func TestGanZhiOfDay(t *testing.T) { - dates := time.Date(23, 12, 31, 0, 0, 0, 0, time.Local) - fmt.Println(SolarToLunar(dates)) + dates := time.Date(1083, 11, 24, 0, 0, 0, 0, getCst()) + fmt.Println(dates.Weekday()) + jde := Date2JDE(dates) + fmt.Println(int(jde+1.5) % 7) + d, _ := SolarToLunar(dates) + fmt.Println(d.LunarInfo()) //date, err := LunarToSolar("久视元年腊月辛亥") - date, err := LunarToSolar("万历十年九月甲戌") + date, err := LunarToSolar("2025年闰6月1日") if err != nil { t.Fatal(err) }