增加对无效时间的判断

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

View File

@ -13,7 +13,7 @@ func TestCarbon_Constellation1(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值1
output string // 期望输出
expected string // 期望
}{
{1, "", ""},
{2, "0", ""},
@ -36,9 +36,9 @@ func TestCarbon_Constellation1(t *testing.T) {
}
for _, test := range tests {
c := Parse(test.input)
c := SetTimezone(PRC).Parse(test.input)
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))
}
}
@ -49,22 +49,22 @@ func TestCarbon_Constellation2(t *testing.T) {
id int // 测试id
input string // 输入值
param string // 参数值
output string // 期望输出
expected string // 期望
}{
{1, "", "en", ""},
{2, "0", "en", ""},
{3, "0000-00-00", "en", ""},
{4, "2020-08-05", "en", "Leo"},
{5, "2020-08-05", "zh-CN", "狮子座"},
{6, "2020-08-05", "zh-Tw", "獅子座"},
{6, "2020-08-05", "zh-TW", "獅子座"},
{7, "2020-08-05", "jp", "ししざ"},
{8, "2020-08-05", "kr", "처녀자리"},
}
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.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))
}
}
@ -74,7 +74,7 @@ func TestCarbon_IsAries(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -88,9 +88,9 @@ func TestCarbon_IsAries(t *testing.T) {
}
for _, test := range tests {
c := Parse(test.input)
c := SetTimezone(PRC).Parse(test.input)
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))
}
}
@ -100,7 +100,7 @@ func TestCarbon_IsTaurus(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -114,9 +114,9 @@ func TestCarbon_IsTaurus(t *testing.T) {
}
for _, test := range tests {
c := Parse(test.input)
c := SetTimezone(PRC).Parse(test.input)
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))
}
}
@ -126,7 +126,7 @@ func TestCarbon_IsGemini(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -140,9 +140,9 @@ func TestCarbon_IsGemini(t *testing.T) {
}
for _, test := range tests {
c := Parse(test.input)
c := SetTimezone(PRC).Parse(test.input)
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))
}
}
@ -152,7 +152,7 @@ func TestCarbon_IsCancer(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -166,9 +166,9 @@ func TestCarbon_IsCancer(t *testing.T) {
}
for _, test := range tests {
c := Parse(test.input)
c := SetTimezone(PRC).Parse(test.input)
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))
}
}
@ -178,7 +178,7 @@ func TestCarbon_IsLeo(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -193,9 +193,9 @@ func TestCarbon_IsLeo(t *testing.T) {
}
for _, test := range tests {
c := Parse(test.input)
c := SetTimezone(PRC).Parse(test.input)
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))
}
}
@ -205,7 +205,7 @@ func TestCarbon_IsVirgo(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -219,9 +219,9 @@ func TestCarbon_IsVirgo(t *testing.T) {
}
for _, test := range tests {
c := Parse(test.input)
c := SetTimezone(PRC).Parse(test.input)
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))
}
}
@ -231,7 +231,7 @@ func TestCarbon_IsLibra(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -245,9 +245,9 @@ func TestCarbon_IsLibra(t *testing.T) {
}
for _, test := range tests {
c := Parse(test.input)
c := SetTimezone(PRC).Parse(test.input)
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))
}
}
@ -257,7 +257,7 @@ func TestCarbon_IsScorpio(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -271,9 +271,9 @@ func TestCarbon_IsScorpio(t *testing.T) {
}
for _, test := range tests {
c := Parse(test.input)
c := SetTimezone(PRC).Parse(test.input)
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))
}
}
@ -283,7 +283,7 @@ func TestCarbon_IsSagittarius(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -297,9 +297,9 @@ func TestCarbon_IsSagittarius(t *testing.T) {
}
for _, test := range tests {
c := Parse(test.input)
c := SetTimezone(PRC).Parse(test.input)
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))
}
}
@ -309,7 +309,7 @@ func TestCarbon_IsCapricorn(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -323,9 +323,9 @@ func TestCarbon_IsCapricorn(t *testing.T) {
}
for _, test := range tests {
c := Parse(test.input)
c := SetTimezone(PRC).Parse(test.input)
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))
}
}
@ -335,7 +335,7 @@ func TestCarbon_IsAquarius(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -349,9 +349,9 @@ func TestCarbon_IsAquarius(t *testing.T) {
}
for _, test := range tests {
c := Parse(test.input)
c := SetTimezone(PRC).Parse(test.input)
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))
}
}
@ -361,7 +361,7 @@ func TestCarbon_IsPisces(t *testing.T) {
tests := []struct {
id int // 测试id
input string // 输入值
output bool // 期望输出
expected bool // 期望
}{
{1, "", false},
{2, "0", false},
@ -375,8 +375,8 @@ func TestCarbon_IsPisces(t *testing.T) {
}
for _, test := range tests {
c := Parse(test.input)
c := SetTimezone(PRC).Parse(test.input)
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))
}
}