c.IsInvalid() -> c.Error != nil

This commit is contained in:
Peleus 2024-04-09 20:13:42 +08:00
parent 429acbc077
commit e23cf63019

View File

@ -19,7 +19,14 @@ func (c Carbon) IsZero() bool {
// IsValid reports whether is valid time.
// 是否是有效时间
func (c Carbon) IsValid() bool {
if c.Error == nil && !c.IsZero() {
if c.Error != nil {
return false
}
if c.time.IsZero() {
return false
}
// 大于零值时间
if c.StdTime().Unix() > -62135596800 {
return true
}
return false
@ -46,7 +53,7 @@ func (c Carbon) IsPM() bool {
// IsNow reports whether is now time.
// 是否是当前时间
func (c Carbon) IsNow() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Timestamp() == c.Now().Timestamp()
@ -55,7 +62,7 @@ func (c Carbon) IsNow() bool {
// IsFuture reports whether is future time.
// 是否是未来时间
func (c Carbon) IsFuture() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Timestamp() > c.Now().Timestamp()
@ -64,7 +71,7 @@ func (c Carbon) IsFuture() bool {
// IsPast reports whether is past time.
// 是否是过去时间
func (c Carbon) IsPast() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Timestamp() < c.Now().Timestamp()
@ -73,7 +80,7 @@ func (c Carbon) IsPast() bool {
// IsLeapYear reports whether is a leap year.
// 是否是闰年
func (c Carbon) IsLeapYear() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
year := c.Year()
@ -86,7 +93,7 @@ func (c Carbon) IsLeapYear() bool {
// IsLongYear reports whether is a long year, see https://en.wikipedia.org/wiki/ISO_8601#Week_dates.
// 是否是长年
func (c Carbon) IsLongYear() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
_, w := time.Date(c.Year(), 12, 31, 0, 0, 0, 0, c.loc).ISOWeek()
@ -96,7 +103,7 @@ func (c Carbon) IsLongYear() bool {
// IsJanuary reports whether is January.
// 是否是一月
func (c Carbon) IsJanuary() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Month() == int(time.January)
@ -105,7 +112,7 @@ func (c Carbon) IsJanuary() bool {
// IsFebruary reports whether is February.
// 是否是二月
func (c Carbon) IsFebruary() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Month() == int(time.February)
@ -114,7 +121,7 @@ func (c Carbon) IsFebruary() bool {
// IsMarch reports whether is March.
// 是否是三月
func (c Carbon) IsMarch() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Month() == int(time.March)
@ -123,7 +130,7 @@ func (c Carbon) IsMarch() bool {
// IsApril reports whether is April.
// 是否是四月
func (c Carbon) IsApril() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Month() == int(time.April)
@ -132,7 +139,7 @@ func (c Carbon) IsApril() bool {
// IsMay reports whether is May.
// 是否是五月
func (c Carbon) IsMay() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Month() == int(time.May)
@ -141,7 +148,7 @@ func (c Carbon) IsMay() bool {
// IsJune reports whether is June.
// 是否是六月
func (c Carbon) IsJune() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Month() == int(time.June)
@ -150,7 +157,7 @@ func (c Carbon) IsJune() bool {
// IsJuly reports whether is July.
// 是否是七月
func (c Carbon) IsJuly() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Month() == int(time.July)
@ -159,7 +166,7 @@ func (c Carbon) IsJuly() bool {
// IsAugust reports whether is August.
// 是否是八月
func (c Carbon) IsAugust() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Month() == int(time.August)
@ -168,7 +175,7 @@ func (c Carbon) IsAugust() bool {
// IsSeptember reports whether is September.
// 是否是九月
func (c Carbon) IsSeptember() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Month() == int(time.September)
@ -177,7 +184,7 @@ func (c Carbon) IsSeptember() bool {
// IsOctober reports whether is October.
// 是否是十月
func (c Carbon) IsOctober() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Month() == int(time.October)
@ -186,7 +193,7 @@ func (c Carbon) IsOctober() bool {
// IsNovember reports whether is November.
// 是否是十一月
func (c Carbon) IsNovember() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Month() == int(time.November)
@ -195,7 +202,7 @@ func (c Carbon) IsNovember() bool {
// IsDecember reports whether is December.
// 是否是十二月
func (c Carbon) IsDecember() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.Month() == int(time.December)
@ -204,7 +211,7 @@ func (c Carbon) IsDecember() bool {
// IsMonday reports whether is Monday.
// 是否是周一
func (c Carbon) IsMonday() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.StdTime().Weekday() == time.Monday
@ -213,7 +220,7 @@ func (c Carbon) IsMonday() bool {
// IsTuesday reports whether is Tuesday.
// 是否是周二
func (c Carbon) IsTuesday() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.StdTime().Weekday() == time.Tuesday
@ -222,7 +229,7 @@ func (c Carbon) IsTuesday() bool {
// IsWednesday reports whether is Wednesday.
// 是否是周三
func (c Carbon) IsWednesday() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.StdTime().Weekday() == time.Wednesday
@ -231,7 +238,7 @@ func (c Carbon) IsWednesday() bool {
// IsThursday reports whether is Thursday.
// 是否是周四
func (c Carbon) IsThursday() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.StdTime().Weekday() == time.Thursday
@ -240,7 +247,7 @@ func (c Carbon) IsThursday() bool {
// IsFriday reports whether is Friday.
// 是否是周五
func (c Carbon) IsFriday() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.StdTime().Weekday() == time.Friday
@ -249,7 +256,7 @@ func (c Carbon) IsFriday() bool {
// IsSaturday reports whether is Saturday.
// 是否是周六
func (c Carbon) IsSaturday() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.StdTime().Weekday() == time.Saturday
@ -258,7 +265,7 @@ func (c Carbon) IsSaturday() bool {
// IsSunday reports whether is Sunday.
// 是否是周日
func (c Carbon) IsSunday() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.StdTime().Weekday() == time.Sunday
@ -267,7 +274,7 @@ func (c Carbon) IsSunday() bool {
// IsWeekday reports whether is weekday.
// 是否是工作日
func (c Carbon) IsWeekday() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return !c.IsSaturday() && !c.IsSunday()
@ -276,7 +283,7 @@ func (c Carbon) IsWeekday() bool {
// IsWeekend reports whether is weekend.
// 是否是周末
func (c Carbon) IsWeekend() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.IsSaturday() || c.IsSunday()
@ -285,7 +292,7 @@ func (c Carbon) IsWeekend() bool {
// IsYesterday reports whether is yesterday.
// 是否是昨天
func (c Carbon) IsYesterday() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.ToDateString() == Yesterday().ToDateString()
@ -294,7 +301,7 @@ func (c Carbon) IsYesterday() bool {
// IsToday reports whether is today.
// 是否是今天
func (c Carbon) IsToday() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.ToDateString() == Now().ToDateString()
@ -303,7 +310,7 @@ func (c Carbon) IsToday() bool {
// IsTomorrow reports whether is tomorrow.
// 是否是明天
func (c Carbon) IsTomorrow() bool {
if c.IsInvalid() {
if c.Error != nil {
return false
}
return c.ToDateString() == Tomorrow().ToDateString()
@ -312,7 +319,7 @@ func (c Carbon) IsTomorrow() bool {
// IsSameCentury reports whether is same century.
// 是否是同一世纪
func (c Carbon) IsSameCentury(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.Century() == t.Century()
@ -321,7 +328,7 @@ func (c Carbon) IsSameCentury(t Carbon) bool {
// IsSameDecade reports whether is same decade.
// 是否是同一年代
func (c Carbon) IsSameDecade(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.Decade() == t.Decade()
@ -330,7 +337,7 @@ func (c Carbon) IsSameDecade(t Carbon) bool {
// IsSameYear reports whether is same year.
// 是否是同一年
func (c Carbon) IsSameYear(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.Year() == t.Year()
@ -339,7 +346,7 @@ func (c Carbon) IsSameYear(t Carbon) bool {
// IsSameQuarter reports whether is same quarter.
// 是否是同一季节
func (c Carbon) IsSameQuarter(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.Quarter() == t.Quarter()
@ -348,7 +355,7 @@ func (c Carbon) IsSameQuarter(t Carbon) bool {
// IsSameMonth reports whether is same month.
// 是否是同一月
func (c Carbon) IsSameMonth(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.Format("Ym") == t.Format("Ym")
@ -357,7 +364,7 @@ func (c Carbon) IsSameMonth(t Carbon) bool {
// IsSameDay reports whether is same day.
// 是否是同一天
func (c Carbon) IsSameDay(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.Format("Ymd") == t.Format("Ymd")
@ -366,7 +373,7 @@ func (c Carbon) IsSameDay(t Carbon) bool {
// IsSameHour reports whether is same hour.
// 是否是同一小时
func (c Carbon) IsSameHour(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.Format("YmdH") == t.Format("YmdH")
@ -375,7 +382,7 @@ func (c Carbon) IsSameHour(t Carbon) bool {
// IsSameMinute reports whether is same minute.
// 是否是同一分钟
func (c Carbon) IsSameMinute(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.Format("YmdHi") == t.Format("YmdHi")
@ -384,7 +391,7 @@ func (c Carbon) IsSameMinute(t Carbon) bool {
// IsSameSecond reports whether is same second.
// 是否是同一秒
func (c Carbon) IsSameSecond(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.Format("YmdHis") == t.Format("YmdHis")
@ -394,7 +401,7 @@ func (c Carbon) IsSameSecond(t Carbon) bool {
// Compare compares by an operator.
// 时间比较
func (c Carbon) Compare(operator string, t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
switch operator {
@ -417,7 +424,7 @@ func (c Carbon) Compare(operator string, t Carbon) bool {
// Gt reports whether greater than.
// 是否大于
func (c Carbon) Gt(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.time.After(t.time)
@ -426,7 +433,7 @@ func (c Carbon) Gt(t Carbon) bool {
// Lt reports whether less than.
// 是否小于
func (c Carbon) Lt(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.time.Before(t.time)
@ -435,7 +442,7 @@ func (c Carbon) Lt(t Carbon) bool {
// Eq reports whether equal.
// 是否等于
func (c Carbon) Eq(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.time.Equal(t.time)
@ -450,7 +457,7 @@ func (c Carbon) Ne(t Carbon) bool {
// Gte reports whether greater than or equal.
// 是否大于等于
func (c Carbon) Gte(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.Gt(t) || c.Eq(t)
@ -459,7 +466,7 @@ func (c Carbon) Gte(t Carbon) bool {
// Lte reports whether less than or equal.
// 是否小于等于
func (c Carbon) Lte(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
if c.Error != nil || t.Error != nil {
return false
}
return c.Lt(t) || c.Eq(t)
@ -468,7 +475,7 @@ func (c Carbon) Lte(t Carbon) bool {
// Between reports whether between two times, excluded the start and end time.
// 是否在两个时间之间(不包括这两个时间)
func (c Carbon) Between(start Carbon, end Carbon) bool {
if c.IsInvalid() || start.IsInvalid() || end.IsInvalid() {
if c.Error != nil || start.Error != nil || end.Error != nil {
return false
}
if c.Gt(start) && c.Lt(end) {
@ -480,7 +487,7 @@ func (c Carbon) Between(start Carbon, end Carbon) bool {
// BetweenIncludedStart reports whether between two times, included the start time.
// 是否在两个时间之间(包括开始时间)
func (c Carbon) BetweenIncludedStart(start Carbon, end Carbon) bool {
if c.IsInvalid() || start.IsInvalid() || end.IsInvalid() {
if c.Error != nil || start.Error != nil || end.Error != nil {
return false
}
if c.Gte(start) && c.Lt(end) {
@ -492,7 +499,7 @@ func (c Carbon) BetweenIncludedStart(start Carbon, end Carbon) bool {
// BetweenIncludedEnd reports whether between two times, included the end time.
// 是否在两个时间之间(包括结束时间)
func (c Carbon) BetweenIncludedEnd(start Carbon, end Carbon) bool {
if c.IsInvalid() || start.IsInvalid() || end.IsInvalid() {
if c.Error != nil || start.Error != nil || end.Error != nil {
return false
}
if c.Gt(start) && c.Lte(end) {
@ -504,7 +511,7 @@ func (c Carbon) BetweenIncludedEnd(start Carbon, end Carbon) bool {
// BetweenIncludedBoth reports whether between two times, included the start and end time.
// 是否在两个时间之间(包括这两个时间)
func (c Carbon) BetweenIncludedBoth(start Carbon, end Carbon) bool {
if c.IsInvalid() || start.IsInvalid() || end.IsInvalid() {
if c.Error != nil || start.Error != nil || end.Error != nil {
return false
}
if c.Gte(start) && c.Lte(end) {