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

This commit is contained in:
Peleus 2024-04-09 20:26:57 +08:00
parent 9ba6fd8d9c
commit 3aaffef6c0
2 changed files with 24 additions and 24 deletions

View File

@ -3,7 +3,7 @@ package carbon
// StartOfCentury returns a Carbon instance for start of the century.
// 本世纪开始时间
func (c Carbon) StartOfCentury() Carbon {
if c.IsZero() {
if c.Error != nil {
return c
}
return c.create(c.Year()/YearsPerCentury*YearsPerCentury, 1, 1, 0, 0, 0, 0)
@ -12,7 +12,7 @@ func (c Carbon) StartOfCentury() Carbon {
// EndOfCentury returns a Carbon instance for end of the century.
// 本世纪结束时间
func (c Carbon) EndOfCentury() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
return c.create(c.Year()/YearsPerCentury*YearsPerCentury+99, 12, 31, 23, 59, 59, 999999999)
@ -21,7 +21,7 @@ func (c Carbon) EndOfCentury() Carbon {
// StartOfDecade returns a Carbon instance for start of the decade.
// 本年代开始时间
func (c Carbon) StartOfDecade() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
return c.create(c.Year()/YearsPerDecade*YearsPerDecade, 1, 1, 0, 0, 0, 0)
@ -30,7 +30,7 @@ func (c Carbon) StartOfDecade() Carbon {
// EndOfDecade returns a Carbon instance for end of the decade.
// 本年代结束时间
func (c Carbon) EndOfDecade() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
return c.create(c.Year()/YearsPerDecade*YearsPerDecade+9, 12, 31, 23, 59, 59, 999999999)
@ -39,7 +39,7 @@ func (c Carbon) EndOfDecade() Carbon {
// StartOfYear returns a Carbon instance for start of the year.
// 本年开始时间
func (c Carbon) StartOfYear() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
return c.create(c.Year(), 1, 1, 0, 0, 0, 0)
@ -48,7 +48,7 @@ func (c Carbon) StartOfYear() Carbon {
// EndOfYear returns a Carbon instance for end of the year.
// 本年结束时间
func (c Carbon) EndOfYear() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
return c.create(c.Year(), 12, 31, 23, 59, 59, 999999999)
@ -57,7 +57,7 @@ func (c Carbon) EndOfYear() Carbon {
// StartOfQuarter returns a Carbon instance for start of the quarter.
// 本季度开始时间
func (c Carbon) StartOfQuarter() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
year, quarter, day := c.Year(), c.Quarter(), 1
@ -67,7 +67,7 @@ func (c Carbon) StartOfQuarter() Carbon {
// EndOfQuarter returns a Carbon instance for end of the quarter.
// 本季度结束时间
func (c Carbon) EndOfQuarter() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
year, quarter, day := c.Year(), c.Quarter(), 30
@ -83,7 +83,7 @@ func (c Carbon) EndOfQuarter() Carbon {
// StartOfMonth returns a Carbon instance for start of the month.
// 本月开始时间
func (c Carbon) StartOfMonth() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
year, month, _ := c.Date()
@ -93,7 +93,7 @@ func (c Carbon) StartOfMonth() Carbon {
// EndOfMonth returns a Carbon instance for end of the month.
// 本月结束时间
func (c Carbon) EndOfMonth() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
year, month, _ := c.Date()
@ -103,7 +103,7 @@ func (c Carbon) EndOfMonth() Carbon {
// StartOfWeek returns a Carbon instance for start of the week.
// 本周开始时间
func (c Carbon) StartOfWeek() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
dayOfWeek, weekStartsAt := c.DayOfWeek(), int(c.weekStartsAt)
@ -113,7 +113,7 @@ func (c Carbon) StartOfWeek() Carbon {
// EndOfWeek returns a Carbon instance for end of the week.
// 本周结束时间
func (c Carbon) EndOfWeek() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
dayOfWeek, weekEndsAt := c.DayOfWeek(), int(c.weekStartsAt)+DaysPerWeek-1
@ -123,7 +123,7 @@ func (c Carbon) EndOfWeek() Carbon {
// StartOfDay returns a Carbon instance for start of the day.
// 本日开始时间
func (c Carbon) StartOfDay() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
year, month, day := c.Date()
@ -133,7 +133,7 @@ func (c Carbon) StartOfDay() Carbon {
// EndOfDay returns a Carbon instance for end of the day.
// 本日结束时间
func (c Carbon) EndOfDay() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
year, month, day := c.Date()
@ -143,7 +143,7 @@ func (c Carbon) EndOfDay() Carbon {
// StartOfHour returns a Carbon instance for start of the hour.
// 小时开始时间
func (c Carbon) StartOfHour() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
year, month, day := c.Date()
@ -153,7 +153,7 @@ func (c Carbon) StartOfHour() Carbon {
// EndOfHour returns a Carbon instance for end of the hour.
// 小时结束时间
func (c Carbon) EndOfHour() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
year, month, day := c.Date()
@ -163,7 +163,7 @@ func (c Carbon) EndOfHour() Carbon {
// StartOfMinute returns a Carbon instance for start of the minute.
// 分钟开始时间
func (c Carbon) StartOfMinute() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
year, month, day, hour, minute, _ := c.DateTime()
@ -173,7 +173,7 @@ func (c Carbon) StartOfMinute() Carbon {
// EndOfMinute returns a Carbon instance for end of the minute.
// 分钟结束时间
func (c Carbon) EndOfMinute() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
year, month, day, hour, minute, _ := c.DateTime()
@ -183,7 +183,7 @@ func (c Carbon) EndOfMinute() Carbon {
// StartOfSecond returns a Carbon instance for start of the second.
// 秒开始时间
func (c Carbon) StartOfSecond() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
year, month, day, hour, minute, second := c.DateTime()
@ -193,7 +193,7 @@ func (c Carbon) StartOfSecond() Carbon {
// EndOfSecond returns a Carbon instance for end of the second.
// 秒结束时间
func (c Carbon) EndOfSecond() Carbon {
if c.Error != nil || c.IsZero() {
if c.Error != nil {
return c
}
year, month, day, hour, minute, second := c.DateTime()

View File

@ -3,10 +3,10 @@ package carbon
// Closest returns the closest Carbon instance from the given Carbon instance.
// 返回离给定 carbon 实例最近的 Carbon 实例
func (c Carbon) Closest(c1 Carbon, c2 Carbon) Carbon {
if c1.IsZero() || c1.IsInvalid() {
if c1.Error != nil {
return c2
}
if c2.IsZero() || c2.IsInvalid() {
if c2.Error != nil {
return c1
}
if c.DiffAbsInSeconds(c1) < c.DiffAbsInSeconds(c2) {
@ -18,10 +18,10 @@ func (c Carbon) Closest(c1 Carbon, c2 Carbon) Carbon {
// Farthest returns the farthest Carbon instance from the given Carbon instance.
// 返回离给定 carbon 实例最远的 Carbon 实例
func (c Carbon) Farthest(c1 Carbon, c2 Carbon) Carbon {
if c1.IsZero() || c1.IsInvalid() {
if c1.Error != nil {
return c2
}
if c2.IsZero() || c2.IsInvalid() {
if c2.Error != nil {
return c1
}
if c.DiffAbsInSeconds(c1) > c.DiffAbsInSeconds(c2) {