mirror of
https://gitee.com/dromara/carbon.git
synced 2024-12-02 04:07:36 +08:00
优化 IsValid
方法判断逻辑
This commit is contained in:
parent
cc0bbf8c8e
commit
ecda49b9b2
@ -19,14 +19,10 @@ func (c Carbon) IsZero() bool {
|
||||
// IsValid reports whether is valid time.
|
||||
// 是否是有效时间
|
||||
func (c Carbon) IsValid() bool {
|
||||
if c.Error != nil {
|
||||
if c.Error != nil || c.IsZero() {
|
||||
return false
|
||||
}
|
||||
if c.time.IsZero() {
|
||||
return false
|
||||
}
|
||||
// 大于零值时间
|
||||
if c.StdTime().Unix() > -62135596800 {
|
||||
if c.Year() >= MinCarbon().Year() && c.Year() <= MaxCarbon().Year() && c.Month() > 0 && c.Day() > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
@ -83,6 +83,11 @@ func TestCarbon_IsZero(t *testing.T) {
|
||||
carbon: Parse("2020-08-05 00:00:00"),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "case6",
|
||||
carbon: Parse("0000-01-01 13:14:15"),
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@ -115,8 +120,8 @@ func TestCarbon_IsValid(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "case4",
|
||||
carbon: Parse("0001-01-01 00:00:00 +0000 UTC"),
|
||||
want: false,
|
||||
carbon: Parse("0001-01-01 13:14:15 +0000 UTC"),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "case5",
|
||||
@ -155,8 +160,8 @@ func TestCarbon_IsInvalid(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "case4",
|
||||
carbon: Parse("0001-01-01 00:00:00 +0000 UTC"),
|
||||
want: true,
|
||||
carbon: Parse("0001-01-01 13:14:15 +0000 UTC"),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "case5",
|
||||
|
Loading…
Reference in New Issue
Block a user