1. 补充农历覆盖到公元前104年

2. 修正时区导致的计算问题
This commit is contained in:
兔子 2025-09-15 23:40:09 +08:00
parent d6b6452304
commit 3389c33cb5
Signed by: b612
GPG Key ID: 99DD2222B612B612
6 changed files with 29 additions and 13 deletions

View File

@ -14,6 +14,10 @@ import (
var tiangan = []string{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"} var tiangan = []string{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"}
var dizhi = []string{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"} var dizhi = []string{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}
func getCst() *time.Location {
return time.FixedZone("CST", 8*3600)
}
const ( const (
JQ_春分 = 15 * iota JQ_春分 = 15 * iota
JQ_清明 JQ_清明
@ -196,8 +200,8 @@ recalc:
if useGoto { if useGoto {
calcYear++ 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())
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())
diffDay := int(target.Sub(spring).Hours() / 24) diffDay := int(target.Sub(spring).Hours() / 24)
lunarMonth := 1 lunarMonth := 1
totalDay := 0 totalDay := 0
@ -252,7 +256,7 @@ func rapidSolarModern(year, month, day int, isLeap bool) time.Time {
magic := int32(upper[idx])<<8 + int32(lower[idx]) magic := int32(upper[idx])<<8 + int32(lower[idx])
springMonth := (magic&0x800000)>>23 + 1 springMonth := (magic&0x800000)>>23 + 1
springDay := (magic & 0x7FFFFF) >> 18 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 lunarMonth := 1
totalDay := 0 totalDay := 0
leap := false leap := false
@ -483,7 +487,7 @@ func GanZhiOfYear(year int) string {
// GanZhiOfDay // GanZhiOfDay
func GanZhiOfDay(t time.Time) string { 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) diff := int(jde - 2451550.5)
if diff >= 0 { if diff >= 0 {
return tiangan[diff%10] + dizhi[diff%12] return tiangan[diff%10] + dizhi[diff%12]
@ -508,7 +512,7 @@ func commonGanZhiOfMonth(year, month int) string {
} }
func ganZhiOfDayIndex(t time.Time) (int, int) { 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) diff := int(jde - 2451550.5)
if diff >= 0 { if diff >= 0 {
return diff % 10, diff % 12 return diff % 10, diff % 12

View File

@ -42,7 +42,7 @@ recalc:
if useGoto { if useGoto {
calcYear++ 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) diffDay := int(target.Sub(springDate).Hours() / 24)
//go语言在 1582年10月4日前使用的是逆推格里高利历与实际使用的儒略历有所不同主要体现在百年闰年计算上 //go语言在 1582年10月4日前使用的是逆推格里高利历与实际使用的儒略历有所不同主要体现在百年闰年计算上
//儒略历修正 //儒略历修正

View File

@ -393,6 +393,7 @@ func innerSolarToLunarLiaoJinYuan(date Time) Time {
if year >= 947 && year <= 1279 { if year >= 947 && year <= 1279 {
lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, liaoJinYuanCals) lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, liaoJinYuanCals)
date.lunars = append(date.lunars, LunarTime{ date.lunars = append(date.lunars, LunarTime{
solarDate: date.Solar(),
year: lyear, year: lyear,
month: lmonth, month: lmonth,
day: lday, day: lday,

View File

@ -863,6 +863,7 @@ func innerSolarToLunarNanBeiChao(date Time) Time {
if year >= 384 && year <= 417 { if year >= 384 && year <= 417 {
lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, houQinCals) lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, houQinCals)
date.lunars = append(date.lunars, LunarTime{ date.lunars = append(date.lunars, LunarTime{
solarDate: date.solarTime,
year: lyear, year: lyear,
month: lmonth, month: lmonth,
day: lday, day: lday,
@ -876,6 +877,7 @@ func innerSolarToLunarNanBeiChao(date Time) Time {
if year >= 398 && year <= 589 { if year >= 398 && year <= 589 {
lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, weiZhouSuiCals) lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, weiZhouSuiCals)
date.lunars = append(date.lunars, LunarTime{ date.lunars = append(date.lunars, LunarTime{
solarDate: date.solarTime,
year: lyear, year: lyear,
month: lmonth, month: lmonth,
day: lday, day: lday,
@ -889,6 +891,7 @@ func innerSolarToLunarNanBeiChao(date Time) Time {
if year >= 397 && year <= 439 { if year >= 397 && year <= 439 {
lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, beiLiangCals) lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, beiLiangCals)
date.lunars = append(date.lunars, LunarTime{ date.lunars = append(date.lunars, LunarTime{
solarDate: date.solarTime,
year: lyear, year: lyear,
month: lmonth, month: lmonth,
day: lday, day: lday,
@ -902,6 +905,7 @@ func innerSolarToLunarNanBeiChao(date Time) Time {
if year >= 534 && year <= 577 { if year >= 534 && year <= 577 {
lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, dongWeiBeiQiCals) lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, dongWeiBeiQiCals)
date.lunars = append(date.lunars, LunarTime{ date.lunars = append(date.lunars, LunarTime{
solarDate: date.solarTime,
year: lyear, year: lyear,
month: lmonth, month: lmonth,
day: lday, day: lday,

View File

@ -99,6 +99,7 @@ func innerSolarToLunarNanMing(date Time) Time {
if year > 1644 && year < 1654 { if year > 1644 && year < 1654 {
lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, nanMingCals) lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, nanMingCals)
date.lunars = append(date.lunars, LunarTime{ date.lunars = append(date.lunars, LunarTime{
solarDate: date.solarTime,
year: lyear, year: lyear,
month: lmonth, month: lmonth,
day: lday, day: lday,
@ -112,6 +113,7 @@ func innerSolarToLunarNanMing(date Time) Time {
if year > 1646 && year < 1684 { if year > 1646 && year < 1684 {
lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, nanMingCals) lyear, lmonth, ganzhiMonth, lday, isLeap, ldesc := rapidLunarHan2Qing(year, month, day, 0, nanMingCals)
date.lunars = append(date.lunars, LunarTime{ date.lunars = append(date.lunars, LunarTime{
solarDate: date.solarTime,
year: lyear, year: lyear,
month: lmonth, month: lmonth,
day: lday, day: lday,

View File

@ -61,7 +61,7 @@ func Test_ChineseCalendarModern(t *testing.T) {
/* /*
{ {
var lyear int = v.Year 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 { if lmonth > v.Month {
lyear-- lyear--
} }
@ -102,7 +102,7 @@ func Test_ChineseCalendarModern2(t *testing.T) {
} }
for _, v := range testData { 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -126,7 +126,7 @@ func Test_ChineseCalendarModern2(t *testing.T) {
/* /*
{ {
var lyear int = v.Year 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 { if lmonth > v.Month {
lyear-- 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: 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: 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: 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: 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: "甲戌"}, {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 { 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -240,10 +241,14 @@ func Test_ChineseCalendarAncient(t *testing.T) {
} }
func TestGanZhiOfDay(t *testing.T) { func TestGanZhiOfDay(t *testing.T) {
dates := time.Date(23, 12, 31, 0, 0, 0, 0, time.Local) dates := time.Date(1083, 11, 24, 0, 0, 0, 0, getCst())
fmt.Println(SolarToLunar(dates)) 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("万历十年九月甲戌") date, err := LunarToSolar("2025年闰6月1日")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }