优化农历算法

This commit is contained in:
gouguoyin 2021-07-23 10:48:37 +08:00
parent 79231528e4
commit 77d54d497d
2 changed files with 105 additions and 100 deletions

View File

@ -56,6 +56,10 @@ type lunar struct {
// Lunar 将公历转为农历
func (c Carbon) Lunar() (l lunar) {
if c.IsInvalid() {
return
}
c = c.SetTimezone(PRC)
// leapMonths:闰月总数daysOfYear:年天数daysOfMonth:月天数leapMonth:闰月月份
leapMonths, daysInYear, daysInMonth, leapMonth := 14, 365, 30, 0
// 有效范围检验

View File

@ -14,7 +14,7 @@ func TestLunar_Animal(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出
expected string // 期望
}{
{1, "", ""},
{2, "0", ""},
@ -39,12 +39,13 @@ func TestLunar_Animal(t *testing.T) {
{20, "2020-08-05", "鼠"},
{21, "2021-05-12", "牛"},
{22, "2021-08-05", "牛"},
{22, "2200-08-05", ""},
}
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().Animal(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().Animal(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -54,7 +55,7 @@ func TestLunar_Festival(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出
expected string // 期望
}{
{1, "", ""},
{2, "0", ""},
@ -81,7 +82,7 @@ func TestLunar_Festival(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().Festival(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().Festival(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -91,7 +92,7 @@ func TestLunar_Year(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output int // 期望输出
expected int // 期望
}{
{1, "", 0},
{2, "0", 0},
@ -110,7 +111,7 @@ func TestLunar_Year(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().Year(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().Year(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -120,7 +121,7 @@ func TestLunar_Month(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output int // 期望输出
expected int // 期望
}{
{1, "", 0},
{2, "0", 0},
@ -137,7 +138,7 @@ func TestLunar_Month(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().Month(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().Month(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -147,7 +148,7 @@ func TestLunar_Day(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output int // 期望输出
expected int // 期望
}{
{1, "", 0},
{2, "0", 0},
@ -164,7 +165,7 @@ func TestLunar_Day(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().Day(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().Day(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -174,7 +175,7 @@ func TestLunar_LeapMonth(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output int // 期望输出
expected int // 期望
}{
{1, "", 0},
{2, "0", 0},
@ -191,7 +192,7 @@ func TestLunar_LeapMonth(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().LeapMonth(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().LeapMonth(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -201,7 +202,7 @@ func TestLunar_ToYearString(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出
expected string // 期望
}{
{1, "", ""},
{2, "0", ""},
@ -220,7 +221,7 @@ func TestLunar_ToYearString(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().ToYearString(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().ToYearString(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -230,7 +231,7 @@ func TestLunar_ToMonthString(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出
expected string // 期望
}{
{1, "", ""},
{2, "0", ""},
@ -260,7 +261,7 @@ func TestLunar_ToMonthString(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().ToMonthString(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().ToMonthString(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -270,7 +271,7 @@ func TestLunar_ToDayString(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出
expected string // 期望
}{
{1, "", ""},
{2, "0", ""},
@ -298,7 +299,7 @@ func TestLunar_ToDayString(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().ToDayString(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().ToDayString(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -308,7 +309,7 @@ func TestLunar_String(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出
expected string // 期望
}{
{1, "", ""},
{2, "0", ""},
@ -334,7 +335,7 @@ func TestLunar_String(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(fmt.Sprintf("%s", c.Lunar()), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, fmt.Sprintf("%s", c.Lunar()), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -344,7 +345,7 @@ func TestLunar_ToString(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output string // 期望输出
expected string // 期望
}{
{1, "", ""},
{2, "0", ""},
@ -370,7 +371,7 @@ func TestLunar_ToString(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().ToString(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().ToString(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -380,7 +381,7 @@ func TestLunar_IsLeapYear(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -398,7 +399,7 @@ func TestLunar_IsLeapYear(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsLeapYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsLeapYear(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -408,7 +409,7 @@ func TestLunar_IsLeapMonth(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -426,7 +427,7 @@ func TestLunar_IsLeapMonth(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsLeapMonth(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsLeapMonth(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -436,7 +437,7 @@ func TestLunar_IsRatYear(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -453,7 +454,7 @@ func TestLunar_IsRatYear(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsRatYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsRatYear(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -463,7 +464,7 @@ func TestLunar_IsOxYear(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -480,7 +481,7 @@ func TestLunar_IsOxYear(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsOxYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsOxYear(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -490,7 +491,7 @@ func TestLunar_IsTigerYear(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -508,7 +509,7 @@ func TestLunar_IsTigerYear(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsTigerYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsTigerYear(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -518,7 +519,7 @@ func TestLunar_IsRabbitYear(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -536,7 +537,7 @@ func TestLunar_IsRabbitYear(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsRabbitYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsRabbitYear(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -546,7 +547,7 @@ func TestLunar_IsDragonYear(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -564,7 +565,7 @@ func TestLunar_IsDragonYear(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsDragonYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsDragonYear(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -574,7 +575,7 @@ func TestLunar_IsSnakeYear(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -592,7 +593,7 @@ func TestLunar_IsSnakeYear(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsSnakeYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsSnakeYear(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -602,7 +603,7 @@ func TestLunar_IsHorseYear(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -620,7 +621,7 @@ func TestLunar_IsHorseYear(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsHorseYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsHorseYear(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -630,7 +631,7 @@ func TestLunar_IsGoatYear(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -648,7 +649,7 @@ func TestLunar_IsGoatYear(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsGoatYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsGoatYear(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -658,7 +659,7 @@ func TestLunar_IsMonkeyYear(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -676,7 +677,7 @@ func TestLunar_IsMonkeyYear(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsMonkeyYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsMonkeyYear(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -686,7 +687,7 @@ func TestLunar_IsRoosterYear(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -704,7 +705,7 @@ func TestLunar_IsRoosterYear(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsRoosterYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsRoosterYear(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -714,7 +715,7 @@ func TestLunar_IsDogYear(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -732,7 +733,7 @@ func TestLunar_IsDogYear(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsDogYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsDogYear(), "Current test id is "+strconv.Itoa(test.id))
}
}
@ -742,7 +743,7 @@ func TestLunar_IsPigYear(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -760,6 +761,6 @@ func TestLunar_IsPigYear(t *testing.T) {
for _, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(c.Lunar().IsPigYear(), test.output, "Current test id is "+strconv.Itoa(test.id))
assert.Equal(test.expected, c.Lunar().IsPigYear(), "Current test id is "+strconv.Itoa(test.id))
}
}