增加对无效时间的判断

This commit is contained in:
gouguoyin 2021-07-23 10:55:38 +08:00
parent 7abb9a4846
commit a8ee4f7fc8
2 changed files with 85 additions and 85 deletions

View File

@ -4,7 +4,7 @@ import "strings"
// Constellation 获取星座支持i18n // Constellation 获取星座支持i18n
func (c Carbon) Constellation() string { func (c Carbon) Constellation() string {
if c.IsZero() { if c.IsInvalid() {
return "" return ""
} }
if len(c.Lang.resources) == 0 { if len(c.Lang.resources) == 0 {
@ -46,7 +46,7 @@ func (c Carbon) Constellation() string {
// IsAries 是否是白羊座 // IsAries 是否是白羊座
func (c Carbon) IsAries() bool { func (c Carbon) IsAries() bool {
if c.IsZero() { if c.IsInvalid() {
return false return false
} }
if c.Month() == 3 && c.Day() >= 21 { if c.Month() == 3 && c.Day() >= 21 {
@ -60,7 +60,7 @@ func (c Carbon) IsAries() bool {
// IsTaurus 是否是金牛座 // IsTaurus 是否是金牛座
func (c Carbon) IsTaurus() bool { func (c Carbon) IsTaurus() bool {
if c.IsZero() { if c.IsInvalid() {
return false return false
} }
if c.Month() == 4 && c.Day() >= 20 { if c.Month() == 4 && c.Day() >= 20 {
@ -74,7 +74,7 @@ func (c Carbon) IsTaurus() bool {
// IsGemini 是否是双子座 // IsGemini 是否是双子座
func (c Carbon) IsGemini() bool { func (c Carbon) IsGemini() bool {
if c.IsZero() { if c.IsInvalid() {
return false return false
} }
if c.Month() == 5 && c.Day() >= 21 { if c.Month() == 5 && c.Day() >= 21 {
@ -88,7 +88,7 @@ func (c Carbon) IsGemini() bool {
// IsCancer 是否是巨蟹座 // IsCancer 是否是巨蟹座
func (c Carbon) IsCancer() bool { func (c Carbon) IsCancer() bool {
if c.IsZero() { if c.IsInvalid() {
return false return false
} }
if c.Month() == 6 && c.Day() >= 22 { if c.Month() == 6 && c.Day() >= 22 {
@ -102,7 +102,7 @@ func (c Carbon) IsCancer() bool {
// IsLeo 是否是狮子座 // IsLeo 是否是狮子座
func (c Carbon) IsLeo() bool { func (c Carbon) IsLeo() bool {
if c.IsZero() { if c.IsInvalid() {
return false return false
} }
if c.Month() == 7 && c.Day() >= 23 { if c.Month() == 7 && c.Day() >= 23 {
@ -116,7 +116,7 @@ func (c Carbon) IsLeo() bool {
// IsVirgo 是否是处女座 // IsVirgo 是否是处女座
func (c Carbon) IsVirgo() bool { func (c Carbon) IsVirgo() bool {
if c.IsZero() { if c.IsInvalid() {
return false return false
} }
if c.Month() == 8 && c.Day() >= 23 { if c.Month() == 8 && c.Day() >= 23 {
@ -130,7 +130,7 @@ func (c Carbon) IsVirgo() bool {
// IsLibra 是否是天秤座 // IsLibra 是否是天秤座
func (c Carbon) IsLibra() bool { func (c Carbon) IsLibra() bool {
if c.IsZero() { if c.IsInvalid() {
return false return false
} }
if c.Month() == 9 && c.Day() >= 23 { if c.Month() == 9 && c.Day() >= 23 {
@ -144,7 +144,7 @@ func (c Carbon) IsLibra() bool {
// IsScorpio 是否是天蝎座 // IsScorpio 是否是天蝎座
func (c Carbon) IsScorpio() bool { func (c Carbon) IsScorpio() bool {
if c.IsZero() { if c.IsInvalid() {
return false return false
} }
if c.Month() == 10 && c.Day() >= 24 { if c.Month() == 10 && c.Day() >= 24 {
@ -158,7 +158,7 @@ func (c Carbon) IsScorpio() bool {
// IsSagittarius 是否是射手座 // IsSagittarius 是否是射手座
func (c Carbon) IsSagittarius() bool { func (c Carbon) IsSagittarius() bool {
if c.IsZero() { if c.IsInvalid() {
return false return false
} }
if c.Month() == 11 && c.Day() >= 22 { if c.Month() == 11 && c.Day() >= 22 {
@ -172,7 +172,7 @@ func (c Carbon) IsSagittarius() bool {
// IsCapricorn 是否是摩羯座 // IsCapricorn 是否是摩羯座
func (c Carbon) IsCapricorn() bool { func (c Carbon) IsCapricorn() bool {
if c.IsZero() { if c.IsInvalid() {
return false return false
} }
if c.Month() == 12 && c.Day() >= 22 { if c.Month() == 12 && c.Day() >= 22 {
@ -186,7 +186,7 @@ func (c Carbon) IsCapricorn() bool {
// IsAquarius 是否是水瓶座 // IsAquarius 是否是水瓶座
func (c Carbon) IsAquarius() bool { func (c Carbon) IsAquarius() bool {
if c.IsZero() { if c.IsInvalid() {
return false return false
} }
if c.Month() == 1 && c.Day() >= 20 { if c.Month() == 1 && c.Day() >= 20 {
@ -200,7 +200,7 @@ func (c Carbon) IsAquarius() bool {
// IsPisces 是否是双鱼座 // IsPisces 是否是双鱼座
func (c Carbon) IsPisces() bool { func (c Carbon) IsPisces() bool {
if c.IsZero() { if c.IsInvalid() {
return false return false
} }
if c.Month() == 2 && c.Day() >= 19 { if c.Month() == 2 && c.Day() >= 19 {

View File

@ -11,9 +11,9 @@ func TestCarbon_Constellation1(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值1 input string // 输入值1
output string // 期望输出 expected string // 期望
}{ }{
{1, "", ""}, {1, "", ""},
{2, "0", ""}, {2, "0", ""},
@ -36,9 +36,9 @@ func TestCarbon_Constellation1(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input) c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.Constellation(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.Constellation(), "Current test id is "+strconv.Itoa(test.id))
} }
} }
@ -46,25 +46,25 @@ func TestCarbon_Constellation2(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值 input string // 输入值
param string // 参数值 param string // 参数值
output string // 期望输出 expected string // 期望
}{ }{
{1, "", "en", ""}, {1, "", "en", ""},
{2, "0", "en", ""}, {2, "0", "en", ""},
{3, "0000-00-00", "en", ""}, {3, "0000-00-00", "en", ""},
{4, "2020-08-05", "en", "Leo"}, {4, "2020-08-05", "en", "Leo"},
{5, "2020-08-05", "zh-CN", "狮子座"}, {5, "2020-08-05", "zh-CN", "狮子座"},
{6, "2020-08-05", "zh-Tw", "獅子座"}, {6, "2020-08-05", "zh-TW", "獅子座"},
{7, "2020-08-05", "jp", "ししざ"}, {7, "2020-08-05", "jp", "ししざ"},
{8, "2020-08-05", "kr", "처녀자리"}, {8, "2020-08-05", "kr", "처녀자리"},
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input).SetLocale(test.param) c := SetTimezone(PRC).Parse(test.input).SetLocale(test.param)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.Constellation(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.Constellation(), "Current test id is "+strconv.Itoa(test.id))
} }
} }
@ -72,9 +72,9 @@ func TestCarbon_IsAries(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值 input string // 输入值
output bool // 期望输出 expected bool // 期望
}{ }{
{1, "", false}, {1, "", false},
{2, "0", false}, {2, "0", false},
@ -88,9 +88,9 @@ func TestCarbon_IsAries(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input) c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.IsAries(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.IsAries(), "Current test id is "+strconv.Itoa(test.id))
} }
} }
@ -98,9 +98,9 @@ func TestCarbon_IsTaurus(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值 input string // 输入值
output bool // 期望输出 expected bool // 期望
}{ }{
{1, "", false}, {1, "", false},
{2, "0", false}, {2, "0", false},
@ -114,9 +114,9 @@ func TestCarbon_IsTaurus(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input) c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.IsTaurus(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.IsTaurus(), "Current test id is "+strconv.Itoa(test.id))
} }
} }
@ -124,9 +124,9 @@ func TestCarbon_IsGemini(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值 input string // 输入值
output bool // 期望输出 expected bool // 期望
}{ }{
{1, "", false}, {1, "", false},
{2, "0", false}, {2, "0", false},
@ -140,9 +140,9 @@ func TestCarbon_IsGemini(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input) c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.IsGemini(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.IsGemini(), "Current test id is "+strconv.Itoa(test.id))
} }
} }
@ -150,9 +150,9 @@ func TestCarbon_IsCancer(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值 input string // 输入值
output bool // 期望输出 expected bool // 期望
}{ }{
{1, "", false}, {1, "", false},
{2, "0", false}, {2, "0", false},
@ -166,9 +166,9 @@ func TestCarbon_IsCancer(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input) c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.IsCancer(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.IsCancer(), "Current test id is "+strconv.Itoa(test.id))
} }
} }
@ -176,9 +176,9 @@ func TestCarbon_IsLeo(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值 input string // 输入值
output bool // 期望输出 expected bool // 期望
}{ }{
{1, "", false}, {1, "", false},
{2, "0", false}, {2, "0", false},
@ -193,9 +193,9 @@ func TestCarbon_IsLeo(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input) c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.IsLeo(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.IsLeo(), "Current test id is "+strconv.Itoa(test.id))
} }
} }
@ -203,9 +203,9 @@ func TestCarbon_IsVirgo(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值 input string // 输入值
output bool // 期望输出 expected bool // 期望
}{ }{
{1, "", false}, {1, "", false},
{2, "0", false}, {2, "0", false},
@ -219,9 +219,9 @@ func TestCarbon_IsVirgo(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input) c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.IsVirgo(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.IsVirgo(), "Current test id is "+strconv.Itoa(test.id))
} }
} }
@ -229,9 +229,9 @@ func TestCarbon_IsLibra(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值 input string // 输入值
output bool // 期望输出 expected bool // 期望
}{ }{
{1, "", false}, {1, "", false},
{2, "0", false}, {2, "0", false},
@ -245,9 +245,9 @@ func TestCarbon_IsLibra(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input) c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.IsLibra(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.IsLibra(), "Current test id is "+strconv.Itoa(test.id))
} }
} }
@ -255,9 +255,9 @@ func TestCarbon_IsScorpio(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值 input string // 输入值
output bool // 期望输出 expected bool // 期望
}{ }{
{1, "", false}, {1, "", false},
{2, "0", false}, {2, "0", false},
@ -271,9 +271,9 @@ func TestCarbon_IsScorpio(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input) c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.IsScorpio(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.IsScorpio(), "Current test id is "+strconv.Itoa(test.id))
} }
} }
@ -281,9 +281,9 @@ func TestCarbon_IsSagittarius(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值 input string // 输入值
output bool // 期望输出 expected bool // 期望
}{ }{
{1, "", false}, {1, "", false},
{2, "0", false}, {2, "0", false},
@ -297,9 +297,9 @@ func TestCarbon_IsSagittarius(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input) c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.IsSagittarius(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.IsSagittarius(), "Current test id is "+strconv.Itoa(test.id))
} }
} }
@ -307,9 +307,9 @@ func TestCarbon_IsCapricorn(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值 input string // 输入值
output bool // 期望输出 expected bool // 期望
}{ }{
{1, "", false}, {1, "", false},
{2, "0", false}, {2, "0", false},
@ -323,9 +323,9 @@ func TestCarbon_IsCapricorn(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input) c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.IsCapricorn(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.IsCapricorn(), "Current test id is "+strconv.Itoa(test.id))
} }
} }
@ -333,9 +333,9 @@ func TestCarbon_IsAquarius(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值 input string // 输入值
output bool // 期望输出 expected bool // 期望
}{ }{
{1, "", false}, {1, "", false},
{2, "0", false}, {2, "0", false},
@ -349,9 +349,9 @@ func TestCarbon_IsAquarius(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input) c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.IsAquarius(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.IsAquarius(), "Current test id is "+strconv.Itoa(test.id))
} }
} }
@ -359,9 +359,9 @@ func TestCarbon_IsPisces(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
id int // 测试id id int // 测试id
input string // 输入值 input string // 输入值
output bool // 期望输出 expected bool // 期望
}{ }{
{1, "", false}, {1, "", false},
{2, "0", false}, {2, "0", false},
@ -375,8 +375,8 @@ func TestCarbon_IsPisces(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
c := Parse(test.input) c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error) assert.Nil(c.Error)
assert.Equal(c.IsPisces(), test.output, "Current test id is "+strconv.Itoa(test.id)) assert.Equal(test.expected, c.IsPisces(), "Current test id is "+strconv.Itoa(test.id))
} }
} }