34 Star 426 Fork 70

GVPdromara/carbon

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
comparer.go 11.91 KB
一键复制 编辑 原始数据 按行查看 历史
kuafuRace 提交于 27天前 . v2.6.3
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
package carbon
import (
"time"
)
// HasError reports whether it has error.
// 是否有错误
func (c *Carbon) HasError() bool {
if c.IsNil() {
return false
}
return c.Error != nil
}
// IsNil reports whether is a nil time.
// 是否是空时间
func (c *Carbon) IsNil() bool {
if c == nil || &c.time == nil {
return true
}
return false
}
// IsZero reports whether is a zero time(0001-01-01 00:00:00 +0000 UTC).
// 是否是零值时间(0001-01-01 00:00:00 +0000 UTC)
func (c *Carbon) IsZero() bool {
if c.IsNil() || c.HasError() {
return false
}
return c.time.IsZero()
}
// IsEpoch reports whether is a unix epoch time(1970-01-01 00:00:00 +0000 UTC).
// 是否是 UNIX 纪元时间(1970-01-01 00:00:00 +0000 UTC)
func (c *Carbon) IsEpoch() bool {
if c.IsInvalid() {
return false
}
return c.StdTime().Equal(time.Date(EpochYear, 1, 1, 0, 0, 0, 0, time.UTC))
}
// IsValid reports whether is a valid time.
// 是否是有效时间
func (c *Carbon) IsValid() bool {
if !c.IsNil() && !c.HasError() {
return true
}
return false
}
// IsInvalid reports whether is an invalid time.
// 是否是无效时间
func (c *Carbon) IsInvalid() bool {
return !c.IsValid()
}
// IsDST reports whether is a daylight saving time.
// 是否是夏令时
func (c *Carbon) IsDST() bool {
if c.IsInvalid() {
return false
}
return c.StdTime().IsDST()
}
// IsAM reports whether is before noon.
// 是否是上午
func (c *Carbon) IsAM() bool {
if c.IsInvalid() {
return false
}
return c.Format("a") == "am"
}
// IsPM reports whether is after noon.
// 是否是下午
func (c *Carbon) IsPM() bool {
if c.IsInvalid() {
return false
}
return c.Format("a") == "pm"
}
// IsLeapYear reports whether is a leap year.
// 是否是闰年
func (c *Carbon) IsLeapYear() bool {
if c.IsInvalid() {
return false
}
year := c.Year()
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
return true
}
return false
}
// 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() {
return false
}
_, w := time.Date(c.Year(), 12, 31, 0, 0, 0, 0, c.loc).ISOWeek()
return w == WeeksPerLongYear
}
// IsJanuary reports whether is January.
// 是否是一月
func (c *Carbon) IsJanuary() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.January)
}
// IsFebruary reports whether is February.
// 是否是二月
func (c *Carbon) IsFebruary() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.February)
}
// IsMarch reports whether is March.
// 是否是三月
func (c *Carbon) IsMarch() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.March)
}
// IsApril reports whether is April.
// 是否是四月
func (c *Carbon) IsApril() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.April)
}
// IsMay reports whether is May.
// 是否是五月
func (c *Carbon) IsMay() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.May)
}
// IsJune reports whether is June.
// 是否是六月
func (c *Carbon) IsJune() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.June)
}
// IsJuly reports whether is July.
// 是否是七月
func (c *Carbon) IsJuly() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.July)
}
// IsAugust reports whether is August.
// 是否是八月
func (c *Carbon) IsAugust() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.August)
}
// IsSeptember reports whether is September.
// 是否是九月
func (c *Carbon) IsSeptember() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.September)
}
// IsOctober reports whether is October.
// 是否是十月
func (c *Carbon) IsOctober() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.October)
}
// IsNovember reports whether is November.
// 是否是十一月
func (c *Carbon) IsNovember() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.November)
}
// IsDecember reports whether is December.
// 是否是十二月
func (c *Carbon) IsDecember() bool {
if c.IsInvalid() {
return false
}
return c.Month() == int(time.December)
}
// IsMonday reports whether is Monday.
// 是否是周一
func (c *Carbon) IsMonday() bool {
if c.IsInvalid() {
return false
}
return c.StdTime().Weekday() == time.Monday
}
// IsTuesday reports whether is Tuesday.
// 是否是周二
func (c *Carbon) IsTuesday() bool {
if c.IsInvalid() {
return false
}
return c.StdTime().Weekday() == time.Tuesday
}
// IsWednesday reports whether is Wednesday.
// 是否是周三
func (c *Carbon) IsWednesday() bool {
if c.IsInvalid() {
return false
}
return c.StdTime().Weekday() == time.Wednesday
}
// IsThursday reports whether is Thursday.
// 是否是周四
func (c *Carbon) IsThursday() bool {
if c.IsInvalid() {
return false
}
return c.StdTime().Weekday() == time.Thursday
}
// IsFriday reports whether is Friday.
// 是否是周五
func (c *Carbon) IsFriday() bool {
if c.IsInvalid() {
return false
}
return c.StdTime().Weekday() == time.Friday
}
// IsSaturday reports whether is Saturday.
// 是否是周六
func (c *Carbon) IsSaturday() bool {
if c.IsInvalid() {
return false
}
return c.StdTime().Weekday() == time.Saturday
}
// IsSunday reports whether is Sunday.
// 是否是周日
func (c *Carbon) IsSunday() bool {
if c.IsInvalid() {
return false
}
return c.StdTime().Weekday() == time.Sunday
}
// IsWeekday reports whether is weekday.
// 是否是工作日
func (c *Carbon) IsWeekday() bool {
if c.IsInvalid() {
return false
}
return !c.IsWeekend()
}
// IsWeekend reports whether is weekend.
// 是否是周末
func (c *Carbon) IsWeekend() bool {
if c.IsInvalid() {
return false
}
d := c.StdTime().Weekday()
for _, wd := range c.weekendDays {
if d == wd {
return true
}
}
return false
}
// IsNow reports whether is now time.
// 是否是当前时间
func (c *Carbon) IsNow() bool {
if c.IsInvalid() {
return false
}
return c.Timestamp() == Now(c.Timezone()).Timestamp()
}
// IsFuture reports whether is future time.
// 是否是未来时间
func (c *Carbon) IsFuture() bool {
if c.IsInvalid() {
return false
}
if c.IsZero() {
return false
}
return c.Timestamp() > Now(c.Timezone()).Timestamp()
}
// IsPast reports whether is past time.
// 是否是过去时间
func (c *Carbon) IsPast() bool {
if c.IsInvalid() {
return false
}
if c.IsZero() {
return true
}
return c.Timestamp() < Now(c.Timezone()).Timestamp()
}
// IsYesterday reports whether is yesterday.
// 是否是昨天
func (c *Carbon) IsYesterday() bool {
if c.IsInvalid() {
return false
}
return c.ToDateString() == Yesterday().ToDateString()
}
// IsToday reports whether is today.
// 是否是今天
func (c *Carbon) IsToday() bool {
if c.IsInvalid() {
return false
}
return c.ToDateString() == Now().ToDateString()
}
// IsTomorrow reports whether is tomorrow.
// 是否是明天
func (c *Carbon) IsTomorrow() bool {
if c.IsInvalid() {
return false
}
return c.ToDateString() == Tomorrow().ToDateString()
}
// 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.Year() == t.Year() && c.Quarter() == t.Quarter()
}
// IsSameMonth reports whether is same month.
// 是否是同一月
func (c *Carbon) IsSameMonth(t *Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Format("Ym") == t.Format("Ym")
}
// IsSameDay reports whether is same day.
// 是否是同一天
func (c *Carbon) IsSameDay(t *Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Format("Ymd") == t.Format("Ymd")
}
// IsSameHour reports whether is same hour.
// 是否是同一小时
func (c *Carbon) IsSameHour(t *Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Format("YmdH") == t.Format("YmdH")
}
// IsSameMinute reports whether is same minute.
// 是否是同一分钟
func (c *Carbon) IsSameMinute(t *Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Format("YmdHi") == t.Format("YmdHi")
}
// IsSameSecond reports whether is same second.
// 是否是同一秒
func (c *Carbon) IsSameSecond(t *Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Format("YmdHis") == t.Format("YmdHis")
}
// Compare compares by an operator.
// 时间比较
func (c *Carbon) Compare(operator string, t *Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
switch operator {
case "=":
return c.Eq(t)
case "<>", "!=":
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
}
// Gt reports whether greater than.
// 是否大于
func (c *Carbon) Gt(t *Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.time.After(t.time)
}
// Lt reports whether less than.
// 是否小于
func (c *Carbon) Lt(t *Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.time.Before(t.time)
}
// Eq reports whether equal.
// 是否等于
func (c *Carbon) Eq(t *Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.time.Equal(t.time)
}
// Ne reports whether not equal.
// 是否不等于
func (c *Carbon) Ne(t *Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return !c.Eq(t)
}
// Gte reports whether greater than or equal.
// 是否大于等于
func (c *Carbon) Gte(t *Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Gt(t) || c.Eq(t)
}
// Lte reports whether less than or equal.
// 是否小于等于
func (c *Carbon) Lte(t *Carbon) bool {
if c.IsInvalid() || t.IsInvalid() {
return false
}
return c.Lt(t) || c.Eq(t)
}
// 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() {
return false
}
if start.Gt(end) {
return false
}
if c.Gt(start) && c.Lt(end) {
return true
}
return false
}
// 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() {
return false
}
if start.Gt(end) {
return false
}
if c.Gte(start) && c.Lt(end) {
return true
}
return false
}
// 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() {
return false
}
if start.Gt(end) {
return false
}
if c.Gt(start) && c.Lte(end) {
return true
}
return false
}
// 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() {
return false
}
if start.Gt(end) {
return false
}
if c.Gte(start) && c.Lte(end) {
return true
}
return false
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dromara/carbon.git
git@gitee.com:dromara/carbon.git
dromara
carbon
carbon
v2.6.3

搜索帮助