carbon/traveler.go

385 lines
8.8 KiB
Go
Raw Normal View History

2021-02-18 14:32:31 +08:00
package carbon
import "time"
2021-07-28 15:18:05 +08:00
// AddDuration add duration
// 按照持续时长字符串增加时间,支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
2021-02-18 14:32:31 +08:00
func (c Carbon) AddDuration(duration string) Carbon {
2021-07-19 09:55:28 +08:00
if c.IsZero() {
2021-02-18 14:32:31 +08:00
return c
}
td, err := parseByDuration(duration)
2021-07-23 11:08:24 +08:00
c.Time = c.Time.In(c.Loc).Add(td)
2021-02-18 14:32:31 +08:00
c.Error = err
return c
}
2021-07-28 15:18:05 +08:00
// SubDuration subtraction duration
// SubDuration 按照持续时长字符串减少时间,支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
2021-02-18 14:32:31 +08:00
func (c Carbon) SubDuration(duration string) Carbon {
return c.AddDuration("-" + duration)
}
2021-07-28 15:18:05 +08:00
// AddCenturies add centuries
// N世纪后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddCenturies(centuries int) Carbon {
return c.AddYears(YearsPerCentury * centuries)
}
2021-07-28 15:18:05 +08:00
// AddCenturiesNoOverflow add centuries without overflowing month
// N世纪后(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) AddCenturiesNoOverflow(centuries int) Carbon {
return c.AddYearsNoOverflow(centuries * YearsPerCentury)
}
2021-07-28 15:18:05 +08:00
// AddCentury add century
// 1世纪后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddCentury() Carbon {
return c.AddCenturies(1)
}
2021-07-28 15:18:05 +08:00
// AddCenturyNoOverflow add century without overflowing month
// 1世纪后(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) AddCenturyNoOverflow() Carbon {
return c.AddCenturiesNoOverflow(1)
}
2021-07-28 15:18:05 +08:00
// SubCenturies subtraction centuries
// N世纪前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubCenturies(centuries int) Carbon {
return c.SubYears(YearsPerCentury * centuries)
}
2021-07-28 15:18:05 +08:00
// SubCenturiesNoOverflow subtraction centuries without overflowing month
// N世纪前(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) SubCenturiesNoOverflow(centuries int) Carbon {
return c.SubYearsNoOverflow(centuries * YearsPerCentury)
}
2021-07-28 15:18:05 +08:00
// SubCentury subtraction century
// 1世纪前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubCentury() Carbon {
return c.SubCenturies(1)
}
2021-07-28 15:18:05 +08:00
// SubCenturyNoOverflow subtraction century without overflowing month
// 1世纪前(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) SubCenturyNoOverflow() Carbon {
return c.SubCenturiesNoOverflow(1)
}
2021-07-28 15:18:05 +08:00
// AddYears add years
// N年后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddYears(years int) Carbon {
2021-07-19 09:55:28 +08:00
if c.IsZero() {
return c
}
2021-07-23 11:08:24 +08:00
c.Time = c.Time.In(c.Loc).AddDate(years, 0, 0)
2021-02-18 14:32:31 +08:00
return c
}
2021-07-28 15:18:05 +08:00
// AddYearsNoOverflow add years without overflowing month
// N年后(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) AddYearsNoOverflow(years int) Carbon {
2021-07-19 09:55:28 +08:00
if c.IsZero() {
return c
}
2021-02-18 14:32:31 +08:00
// 获取N年后本月的最后一天
2021-02-23 09:32:55 +08:00
last := time.Date(c.Year()+years, time.Month(c.Month()), 1, c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Loc).AddDate(0, 1, -1)
day := c.Day()
if c.Day() > last.Day() {
2021-02-18 14:32:31 +08:00
day = last.Day()
}
2021-02-23 09:32:55 +08:00
c.Time = time.Date(last.Year(), last.Month(), day, c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Loc)
2021-02-18 14:32:31 +08:00
return c
}
2021-07-28 15:18:05 +08:00
// AddYear add year
// 1年后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddYear() Carbon {
return c.AddYears(1)
}
2021-07-28 15:18:05 +08:00
// AddYearNoOverflow add year without overflowing month
// 1年后(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) AddYearNoOverflow() Carbon {
return c.AddYearsNoOverflow(1)
}
2021-07-28 15:18:05 +08:00
// SubYears subtraction years
// N年前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubYears(years int) Carbon {
2021-07-19 09:55:28 +08:00
if c.IsZero() {
return c
}
2021-02-18 14:32:31 +08:00
return c.AddYears(-years)
}
2021-07-28 15:18:05 +08:00
// SubYearsNoOverflow subtraction years without overflowing month
// N年前(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) SubYearsNoOverflow(years int) Carbon {
return c.AddYearsNoOverflow(-years)
}
2021-07-28 15:18:05 +08:00
// SubYear subtraction year
// 1年前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubYear() Carbon {
return c.SubYears(1)
}
2021-07-28 15:18:05 +08:00
// SubYearNoOverflow subtraction year without overflowing month
// 1年前(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) SubYearNoOverflow() Carbon {
return c.SubYearsNoOverflow(1)
}
2021-07-28 15:18:05 +08:00
// AddQuarters add quarters
// N季度后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddQuarters(quarters int) Carbon {
return c.AddMonths(quarters * MonthsPerQuarter)
}
2021-07-28 15:18:05 +08:00
// AddQuartersNoOverflow add quarters without overflowing month
// N季度后(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) AddQuartersNoOverflow(quarters int) Carbon {
return c.AddMonthsNoOverflow(quarters * MonthsPerQuarter)
}
2021-07-28 15:18:05 +08:00
// AddQuarter add quarter
// 1季度后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddQuarter() Carbon {
return c.AddQuarters(1)
}
2021-07-28 15:18:05 +08:00
// AddQuarterNoOverflow add quarter without overflowing month
// 1季度后(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) AddQuarterNoOverflow() Carbon {
return c.AddQuartersNoOverflow(1)
}
2021-07-28 15:18:05 +08:00
// SubQuarters subtraction quarters
// N季度前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubQuarters(quarters int) Carbon {
return c.AddQuarters(-quarters)
}
2021-07-28 15:18:05 +08:00
// SubQuartersNoOverflow subtraction quarters without overflowing month
// N季度前(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) SubQuartersNoOverflow(quarters int) Carbon {
return c.AddMonthsNoOverflow(-quarters * MonthsPerQuarter)
}
2021-07-28 15:18:05 +08:00
// SubQuarter subtraction quarter
// 1季度前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubQuarter() Carbon {
return c.SubQuarters(1)
}
2021-07-28 15:18:05 +08:00
// SubQuarterNoOverflow subtraction quarter without overflowing month
// 1季度前(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) SubQuarterNoOverflow() Carbon {
return c.SubQuartersNoOverflow(1)
}
2021-07-28 15:18:05 +08:00
// AddMonths add months
// N月后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddMonths(months int) Carbon {
2021-07-19 09:55:28 +08:00
if c.IsZero() {
return c
}
2021-07-08 09:48:43 +08:00
c.Time = c.Time.In(c.Loc).AddDate(0, months, 0)
2021-02-18 14:32:31 +08:00
return c
}
2021-07-28 15:18:05 +08:00
// AddMonthsNoOverflow add months without overflowing month
// N月后(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) AddMonthsNoOverflow(months int) Carbon {
2021-07-19 09:55:28 +08:00
if c.IsZero() {
return c
}
2021-07-23 11:08:24 +08:00
month := c.Month() + months
2021-02-18 14:32:31 +08:00
// 获取N月后的最后一天
2021-07-23 11:08:24 +08:00
last := time.Date(c.Year(), time.Month(month), 1, c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Loc).AddDate(0, 1, -1)
2021-02-23 09:32:55 +08:00
day := c.Day()
if c.Day() > last.Day() {
2021-02-18 14:32:31 +08:00
day = last.Day()
}
2021-02-23 09:32:55 +08:00
c.Time = time.Date(last.Year(), last.Month(), day, c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Loc)
2021-02-18 14:32:31 +08:00
return c
}
2021-07-28 15:18:05 +08:00
// AddMonth add month
// 1月后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddMonth() Carbon {
return c.AddMonths(1)
}
2021-07-28 15:18:05 +08:00
// AddMonthNoOverflow add month without overflowing month
// 1月后(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) AddMonthNoOverflow() Carbon {
return c.AddMonthsNoOverflow(1)
}
2021-07-28 15:18:05 +08:00
// SubMonths subtraction months
// N月前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubMonths(months int) Carbon {
return c.AddMonths(-months)
}
2021-07-28 15:18:05 +08:00
// SubMonthsNoOverflow subtraction months without overflowing month
// N月前(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) SubMonthsNoOverflow(months int) Carbon {
return c.AddMonthsNoOverflow(-months)
}
2021-07-28 15:18:05 +08:00
// SubMonth subtraction months
// 1月前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubMonth() Carbon {
return c.SubMonths(1)
}
2021-07-28 15:18:05 +08:00
// SubMonthNoOverflow subtraction months without overflowing month
// 1月前(月份不溢出)
2021-02-18 14:32:31 +08:00
func (c Carbon) SubMonthNoOverflow() Carbon {
return c.SubMonthsNoOverflow(1)
}
2021-07-28 15:18:05 +08:00
// AddWeeks add weeks
// N周后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddWeeks(weeks int) Carbon {
return c.AddDays(weeks * DaysPerWeek)
}
2021-07-28 15:18:05 +08:00
// AddWeek add week
// 1周后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddWeek() Carbon {
return c.AddWeeks(1)
}
2021-07-28 15:18:05 +08:00
// SubWeeks subtraction weeks
// N周前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubWeeks(weeks int) Carbon {
return c.SubDays(weeks * DaysPerWeek)
}
2021-07-28 15:18:05 +08:00
// SubWeek subtraction week
// 1周前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubWeek() Carbon {
return c.SubWeeks(1)
}
2021-07-28 15:18:05 +08:00
// AddDays add days
// N天后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddDays(days int) Carbon {
2021-07-19 09:55:28 +08:00
if c.IsZero() {
return c
}
2021-07-08 09:48:43 +08:00
c.Time = c.Time.In(c.Loc).AddDate(0, 0, days)
2021-02-18 14:32:31 +08:00
return c
}
2021-07-28 15:18:05 +08:00
// AddDay add day
// 1天后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddDay() Carbon {
return c.AddDays(1)
}
2021-07-28 15:18:05 +08:00
// SubDays subtraction days
// N天前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubDays(days int) Carbon {
return c.AddDays(-days)
}
2021-07-28 15:18:05 +08:00
// SubDay subtraction day
// 1天前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubDay() Carbon {
return c.SubDays(1)
}
2021-07-28 15:18:05 +08:00
// AddHours add hours
// N小时后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddHours(hours int) Carbon {
2021-07-19 09:55:28 +08:00
if c.IsZero() {
return c
}
2021-02-18 14:32:31 +08:00
td := time.Duration(hours) * time.Hour
2021-07-23 11:08:24 +08:00
c.Time = c.Time.In(c.Loc).Add(td)
2021-02-18 14:32:31 +08:00
return c
}
2021-07-28 15:18:05 +08:00
// AddHour add hour
// 1小时后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddHour() Carbon {
return c.AddHours(1)
}
2021-07-28 15:18:05 +08:00
// SubHours subtraction hours
// N小时前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubHours(hours int) Carbon {
return c.AddHours(-hours)
}
2021-07-28 15:18:05 +08:00
// SubHour subtraction hour
// 1小时前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubHour() Carbon {
return c.SubHours(1)
}
2021-07-28 15:18:05 +08:00
// AddMinutes add minutes
// N分钟后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddMinutes(minutes int) Carbon {
2021-07-19 09:55:28 +08:00
if c.IsZero() {
return c
}
2021-02-18 14:32:31 +08:00
td := time.Duration(minutes) * time.Minute
c.Time = c.Time.Add(td)
return c
}
2021-07-28 15:18:05 +08:00
// AddMinute add minute
// 1分钟后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddMinute() Carbon {
return c.AddMinutes(1)
}
2021-07-28 15:18:05 +08:00
// SubMinutes subtraction ninutes
// N分钟前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubMinutes(minutes int) Carbon {
return c.AddMinutes(-minutes)
}
2021-07-28 15:18:05 +08:00
// SubMinute subtraction minute
// 1分钟前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubMinute() Carbon {
return c.SubMinutes(1)
}
2021-07-28 15:18:05 +08:00
// AddSeconds add seconds
// N秒钟后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddSeconds(seconds int) Carbon {
2021-07-19 09:55:28 +08:00
if c.IsZero() {
return c
}
2021-02-18 14:32:31 +08:00
td := time.Duration(seconds) * time.Second
c.Time = c.Time.Add(td)
return c
}
2021-07-28 15:18:05 +08:00
// AddSecond add second
// 1秒钟后
2021-02-18 14:32:31 +08:00
func (c Carbon) AddSecond() Carbon {
return c.AddSeconds(1)
}
2021-07-28 15:18:05 +08:00
// SubSeconds subtraction seconds
// N秒钟前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubSeconds(seconds int) Carbon {
return c.AddSeconds(-seconds)
}
2021-07-28 15:18:05 +08:00
// SubSecond subtraction second
// 1秒钟前
2021-02-18 14:32:31 +08:00
func (c Carbon) SubSecond() Carbon {
return c.SubSeconds(1)
}