carbon/comparer.go

515 lines
11 KiB
Go
Raw Normal View History

2021-02-18 14:32:31 +08:00
package carbon
2021-08-05 19:35:09 +08:00
import (
"time"
)
2021-02-18 14:32:31 +08:00
2023-11-06 20:17:40 +08:00
// IsDST reports whether is daylight saving time.
// 是否是夏令时
func (c Carbon) IsDST() bool {
2024-02-01 11:24:43 +08:00
return c.time.IsDST()
2023-11-06 20:17:40 +08:00
}
2021-09-16 09:52:35 +08:00
// IsZero reports whether is zero time.
2021-08-10 10:57:22 +08:00
// 是否是零值时间
2021-02-18 14:32:31 +08:00
func (c Carbon) IsZero() bool {
2023-11-06 20:17:40 +08:00
return c.time.IsZero()
}
2022-04-21 15:04:03 +08:00
// IsValid reports whether is valid time.
// 是否是有效时间
func (c Carbon) IsValid() bool {
if c.Error == nil && !c.IsZero() {
return true
}
return false
2021-02-18 14:32:31 +08:00
}
2022-04-21 15:04:03 +08:00
// IsInvalid reports whether is invalid time.
// 是否是无效时间
func (c Carbon) IsInvalid() bool {
return !c.IsValid()
}
2024-01-12 10:57:10 +08:00
// IsAM reports whether is before noon.
// 是否是上午
func (c Carbon) IsAM() bool {
return c.Format("a") == "am"
}
// IsPM reports whether is after noon.
// 是否是下午
func (c Carbon) IsPM() bool {
return c.Format("a") == "pm"
}
2021-09-16 09:52:35 +08:00
// IsNow reports whether is now time.
2021-07-28 15:18:05 +08:00
// 是否是当前时间
2021-02-18 14:32:31 +08:00
func (c Carbon) IsNow() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
return c.Timestamp() == c.Now().Timestamp()
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsFuture reports whether is future time.
2021-07-28 15:18:05 +08:00
// 是否是未来时间
2021-02-18 14:32:31 +08:00
func (c Carbon) IsFuture() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
return c.Timestamp() > c.Now().Timestamp()
2021-02-18 14:32:31 +08:00
}
2022-04-21 15:04:03 +08:00
// IsPast reports whether is past time.
2021-07-28 15:18:05 +08:00
// 是否是过去时间
2021-02-18 14:32:31 +08:00
func (c Carbon) IsPast() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
return c.Timestamp() < c.Now().Timestamp()
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsLeapYear reports whether is a leap year.
2021-07-28 15:18:05 +08:00
// 是否是闰年
2021-02-18 14:32:31 +08:00
func (c Carbon) IsLeapYear() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
year := c.Year()
2021-02-18 14:32:31 +08:00
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsLongYear reports whether is a long year, see https://en.wikipedia.org/wiki/ISO_8601#Week_dates.
2021-07-28 15:18:05 +08:00
// 是否是长年
2021-02-18 14:32:31 +08:00
func (c Carbon) IsLongYear() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
_, w := time.Date(c.Year(), 12, 31, 0, 0, 0, 0, c.loc).ISOWeek()
2021-02-18 14:32:31 +08:00
return w == weeksPerLongYear
}
2021-09-16 09:52:35 +08:00
// IsJanuary reports whether is January.
2021-07-28 15:18:05 +08:00
// 是否是一月
2021-02-18 14:32:31 +08:00
func (c Carbon) IsJanuary() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
return c.Month() == int(time.January)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsFebruary reports whether is February.
2021-07-28 15:18:05 +08:00
// 是否是二月
2021-02-18 14:32:31 +08:00
func (c Carbon) IsFebruary() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
return c.Month() == int(time.February)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsMarch reports whether is March.
2021-07-28 15:18:05 +08:00
// 是否是三月
2021-02-18 14:32:31 +08:00
func (c Carbon) IsMarch() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
return c.Month() == int(time.March)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsApril reports whether is April.
2021-07-28 15:18:05 +08:00
// 是否是四月
2021-02-18 14:32:31 +08:00
func (c Carbon) IsApril() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
return c.Month() == int(time.April)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsMay reports whether is May.
2021-07-28 15:18:05 +08:00
// 是否是五月
2021-02-18 14:32:31 +08:00
func (c Carbon) IsMay() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
return c.Month() == int(time.May)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsJune reports whether is June.
2021-07-28 15:18:05 +08:00
// 是否是六月
2021-02-18 14:32:31 +08:00
func (c Carbon) IsJune() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
return c.Month() == int(time.June)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsJuly reports whether is July.
2021-07-28 15:18:05 +08:00
// 是否是七月
2021-02-18 14:32:31 +08:00
func (c Carbon) IsJuly() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
return c.Month() == int(time.July)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsAugust reports whether is August.
2021-07-28 15:18:05 +08:00
// 是否是八月
2021-02-18 14:32:31 +08:00
func (c Carbon) IsAugust() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
return c.Month() == int(time.August)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsSeptember reports whether is September.
2021-07-28 15:18:05 +08:00
// 是否是九月
2021-02-18 14:32:31 +08:00
func (c Carbon) IsSeptember() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
return c.Month() == int(time.September)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsOctober reports whether is October.
2021-07-28 15:18:05 +08:00
// 是否是十月
2021-02-18 14:32:31 +08:00
func (c Carbon) IsOctober() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
return c.Month() == int(time.October)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsNovember reports whether is November.
2021-07-28 15:18:05 +08:00
// 是否是十一月
2021-02-18 14:32:31 +08:00
func (c Carbon) IsNovember() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
return c.Month() == int(time.November)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsDecember reports whether is December.
2021-07-28 15:18:05 +08:00
// 是否是十二月
2021-02-18 14:32:31 +08:00
func (c Carbon) IsDecember() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-12 17:17:00 +08:00
return c.Month() == int(time.December)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsMonday reports whether is Monday.
2021-07-28 15:18:05 +08:00
// 是否是周一
2021-02-18 14:32:31 +08:00
func (c Carbon) IsMonday() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Weekday() == time.Monday
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsTuesday reports whether is Tuesday.
2021-07-28 15:18:05 +08:00
// 是否是周二
2021-02-18 14:32:31 +08:00
func (c Carbon) IsTuesday() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Weekday() == time.Tuesday
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsWednesday reports whether is Wednesday.
2021-07-28 15:18:05 +08:00
// 是否是周三
2021-02-18 14:32:31 +08:00
func (c Carbon) IsWednesday() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Weekday() == time.Wednesday
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsThursday reports whether is Thursday.
2021-07-28 15:18:05 +08:00
// 是否是周四
2021-02-18 14:32:31 +08:00
func (c Carbon) IsThursday() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Weekday() == time.Thursday
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsFriday reports whether is Friday.
2021-07-28 15:18:05 +08:00
// 是否是周五
2021-02-18 14:32:31 +08:00
func (c Carbon) IsFriday() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Weekday() == time.Friday
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsSaturday reports whether is Saturday.
2021-07-28 15:18:05 +08:00
// 是否是周六
2021-02-18 14:32:31 +08:00
func (c Carbon) IsSaturday() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Weekday() == time.Saturday
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsSunday reports whether is Sunday.
2021-07-28 15:18:05 +08:00
// 是否是周日
2021-02-18 14:32:31 +08:00
func (c Carbon) IsSunday() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Weekday() == time.Sunday
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsWeekday reports whether is weekday.
2021-07-28 15:18:05 +08:00
// 是否是工作日
2021-02-18 14:32:31 +08:00
func (c Carbon) IsWeekday() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2021-02-18 14:32:31 +08:00
return !c.IsSaturday() && !c.IsSunday()
}
2021-09-16 09:52:35 +08:00
// IsWeekend reports whether is weekend.
2021-07-28 15:18:05 +08:00
// 是否是周末
2021-02-18 14:32:31 +08:00
func (c Carbon) IsWeekend() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2021-02-18 14:32:31 +08:00
return c.IsSaturday() || c.IsSunday()
}
2021-09-16 09:52:35 +08:00
// IsYesterday reports whether is yesterday.
2021-07-28 15:18:05 +08:00
// 是否是昨天
2021-02-18 14:32:31 +08:00
func (c Carbon) IsYesterday() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-19 23:50:36 +08:00
return c.ToDateString() == Yesterday().ToDateString()
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsToday reports whether is today.
2021-07-28 15:18:05 +08:00
// 是否是今天
2021-02-18 14:32:31 +08:00
func (c Carbon) IsToday() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-19 23:50:36 +08:00
return c.ToDateString() == Now().ToDateString()
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// IsTomorrow reports whether is tomorrow.
2021-07-28 15:18:05 +08:00
// 是否是明天
2021-02-18 14:32:31 +08:00
func (c Carbon) IsTomorrow() bool {
if c.IsInvalid() {
2021-07-09 15:11:48 +08:00
return false
}
2022-04-19 23:50:36 +08:00
return c.ToDateString() == Tomorrow().ToDateString()
2021-02-18 14:32:31 +08:00
}
2022-05-20 00:02:30 +08:00
// IsSameCentury reports whether is same century.
// 是否是同一世纪
func (c Carbon) IsSameCentury(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Century() == t.Century()
}
// IsSameDecade reports whether is same decade.
// 是否是同一年代
func (c Carbon) IsSameDecade(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Decade() == t.Decade()
}
// IsSameYear reports whether is same year.
// 是否是同一年
func (c Carbon) IsSameYear(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Year() == t.Year()
}
// IsSameQuarter reports whether is same quarter.
// 是否是同一季节
func (c Carbon) IsSameQuarter(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Quarter() == t.Quarter()
}
// IsSameMonth reports whether is same month.
// 是否是同一月
func (c Carbon) IsSameMonth(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
2022-05-20 21:43:24 +08:00
return c.Format("Ym") == t.Format("Ym")
2022-05-20 00:02:30 +08:00
}
// IsSameDay reports whether is same day.
// 是否是同一天
func (c Carbon) IsSameDay(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
2022-05-20 21:43:24 +08:00
return c.Format("Ymd") == t.Format("Ymd")
2022-05-20 00:02:30 +08:00
}
// IsSameHour reports whether is same hour.
// 是否是同一小时
func (c Carbon) IsSameHour(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
2022-05-20 21:43:24 +08:00
return c.Format("YmdH") == t.Format("YmdH")
2022-05-20 00:02:30 +08:00
}
// IsSameMinute reports whether is same minute.
// 是否是同一分钟
func (c Carbon) IsSameMinute(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
2022-05-20 21:43:24 +08:00
return c.Format("YmdHi") == t.Format("YmdHi")
2022-05-20 00:02:30 +08:00
}
// IsSameSecond reports whether is same second.
// 是否是同一秒
func (c Carbon) IsSameSecond(t Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
2022-05-20 21:43:24 +08:00
return c.Format("YmdHis") == t.Format("YmdHis")
2022-05-20 00:02:30 +08:00
}
2021-09-16 09:52:35 +08:00
// Compare compares by an operator.
2021-07-28 15:18:05 +08:00
// 时间比较
2021-02-18 14:32:31 +08:00
func (c Carbon) Compare(operator string, t Carbon) bool {
2022-04-21 15:04:03 +08:00
if c.IsInvalid() || t.IsInvalid() {
return false
}
2021-02-18 14:32:31 +08:00
switch operator {
case "=":
return c.Eq(t)
case "<>", "!=":
2021-02-18 14:32:31 +08:00
return !c.Eq(t)
case ">":
return c.Gt(t)
case ">=":
return c.Gte(t)
case "<":
return c.Lt(t)
case "<=":
return c.Lte(t)
}
return false
}
2021-09-16 09:52:35 +08:00
// Gt reports whether greater than.
2021-07-28 15:18:05 +08:00
// 是否大于
2021-02-18 14:32:31 +08:00
func (c Carbon) Gt(t Carbon) bool {
2022-04-21 15:04:03 +08:00
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.time.After(t.time)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// Lt reports whether less than.
2021-07-28 15:18:05 +08:00
// 是否小于
2021-02-18 14:32:31 +08:00
func (c Carbon) Lt(t Carbon) bool {
2022-04-21 15:04:03 +08:00
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.time.Before(t.time)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// Eq reports whether equal.
2021-07-28 15:18:05 +08:00
// 是否等于
2021-02-18 14:32:31 +08:00
func (c Carbon) Eq(t Carbon) bool {
2022-04-21 15:04:03 +08:00
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.time.Equal(t.time)
2021-02-18 14:32:31 +08:00
}
2021-09-16 09:52:35 +08:00
// Ne reports whether not equal.
2021-07-28 15:18:05 +08:00
// 是否不等于
2021-02-18 14:32:31 +08:00
func (c Carbon) Ne(t Carbon) bool {
return !c.Eq(t)
}
2021-09-16 09:52:35 +08:00
// Gte reports whether greater than or equal.
2021-07-28 15:18:05 +08:00
// 是否大于等于
2021-02-18 14:32:31 +08:00
func (c Carbon) Gte(t Carbon) bool {
2022-04-21 15:04:03 +08:00
if c.IsInvalid() || t.IsInvalid() {
return false
}
2021-02-18 14:32:31 +08:00
return c.Gt(t) || c.Eq(t)
}
2021-09-16 09:52:35 +08:00
// Lte reports whether less than or equal.
2021-07-28 15:18:05 +08:00
// 是否小于等于
2021-02-18 14:32:31 +08:00
func (c Carbon) Lte(t Carbon) bool {
2022-04-21 15:04:03 +08:00
if c.IsInvalid() || t.IsInvalid() {
return false
}
2021-02-18 14:32:31 +08:00
return c.Lt(t) || c.Eq(t)
}
2021-09-16 09:52:35 +08:00
// Between reports whether between two times, excluded the start and end time.
2021-07-28 15:18:05 +08:00
// 是否在两个时间之间(不包括这两个时间)
2021-02-18 14:32:31 +08:00
func (c Carbon) Between(start Carbon, end Carbon) bool {
2022-04-21 15:04:03 +08:00
if c.IsInvalid() || start.IsInvalid() || end.IsInvalid() {
return false
}
2021-02-18 14:32:31 +08:00
if c.Gt(start) && c.Lt(end) {
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// BetweenIncludedStart reports whether between two times, included the start time.
2021-07-28 15:18:05 +08:00
// 是否在两个时间之间(包括开始时间)
func (c Carbon) BetweenIncludedStart(start Carbon, end Carbon) bool {
2022-04-21 15:04:03 +08:00
if c.IsInvalid() || start.IsInvalid() || end.IsInvalid() {
return false
}
2021-02-18 14:32:31 +08:00
if c.Gte(start) && c.Lt(end) {
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// BetweenIncludedEnd reports whether between two times, included the end time.
2021-07-28 15:18:05 +08:00
// 是否在两个时间之间(包括结束时间)
func (c Carbon) BetweenIncludedEnd(start Carbon, end Carbon) bool {
2022-04-21 15:04:03 +08:00
if c.IsInvalid() || start.IsInvalid() || end.IsInvalid() {
return false
}
2021-02-18 14:32:31 +08:00
if c.Gt(start) && c.Lte(end) {
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// BetweenIncludedBoth reports whether between two times, included the start and end time.
2021-07-28 15:18:05 +08:00
// 是否在两个时间之间(包括这两个时间)
func (c Carbon) BetweenIncludedBoth(start Carbon, end Carbon) bool {
2022-04-21 15:04:03 +08:00
if c.IsInvalid() || start.IsInvalid() || end.IsInvalid() {
return false
}
2021-02-18 14:32:31 +08:00
if c.Gte(start) && c.Lte(end) {
return true
}
return false
}