2021-02-18 14:32:31 +08:00
|
|
|
package carbon
|
|
|
|
|
2021-08-05 19:45:47 +08:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
2021-02-18 14:32:31 +08:00
|
|
|
|
2022-10-29 14:31:57 +08:00
|
|
|
// Now returns a Carbon instance for now.
|
|
|
|
// 当前
|
|
|
|
func (c Carbon) Now(timezone ...string) Carbon {
|
|
|
|
if len(timezone) > 0 {
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
|
|
|
|
}
|
|
|
|
if c.Error != nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
c.time = time.Now().In(c.loc)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now returns a Carbon instance for now.
|
|
|
|
// 当前
|
|
|
|
func Now(timezone ...string) Carbon {
|
|
|
|
return NewCarbon().Now(timezone...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tomorrow returns a Carbon instance for tomorrow.
|
|
|
|
// 明天
|
|
|
|
func (c Carbon) Tomorrow(timezone ...string) Carbon {
|
|
|
|
if len(timezone) > 0 {
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
|
|
|
|
}
|
|
|
|
if c.Error != nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
if c.IsZero() {
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time = c.Now().time.In(c.loc).AddDate(0, 0, 1)
|
2022-10-29 14:31:57 +08:00
|
|
|
return c
|
|
|
|
}
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time = c.time.In(c.loc).AddDate(0, 0, 1)
|
2022-10-29 14:31:57 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tomorrow returns a Carbon instance for tomorrow.
|
|
|
|
// 明天
|
|
|
|
func Tomorrow(timezone ...string) Carbon {
|
|
|
|
return NewCarbon().Tomorrow(timezone...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Yesterday returns a Carbon instance for yesterday.
|
|
|
|
// 昨天
|
|
|
|
func (c Carbon) Yesterday(timezone ...string) Carbon {
|
|
|
|
if len(timezone) > 0 {
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
|
|
|
|
}
|
|
|
|
if c.Error != nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
if c.IsZero() {
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time = c.Now().time.In(c.loc).AddDate(0, 0, -1)
|
2022-10-29 14:31:57 +08:00
|
|
|
return c
|
|
|
|
}
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time = c.time.In(c.loc).AddDate(0, 0, -1)
|
2022-10-29 14:31:57 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Yesterday returns a Carbon instance for yesterday.
|
|
|
|
// 昨天
|
|
|
|
func Yesterday(timezone ...string) Carbon {
|
|
|
|
return NewCarbon().Yesterday(timezone...)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddDuration adds one duration.
|
2022-05-24 07:05:15 +08:00
|
|
|
// 按照时长增加时间,支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddDuration(duration string) Carbon {
|
2021-08-05 19:45:47 +08:00
|
|
|
if c.IsInvalid() {
|
2021-02-18 14:32:31 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
td, err := parseByDuration(duration)
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time, c.Error = c.time.In(c.loc).Add(td), err
|
2021-02-18 14:32:31 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubDuration subtracts one duration.
|
2022-05-24 07:05:15 +08:00
|
|
|
// 按照时长减少时间,支持整数/浮点数和符号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-08-10 11:00:19 +08:00
|
|
|
// AddCenturies adds some centuries.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个世纪后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddCenturies(centuries int) Carbon {
|
2021-08-05 19:45:47 +08:00
|
|
|
return c.AddYears(centuries * YearsPerCentury)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddCenturiesNoOverflow adds some centuries without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个世纪后(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddCenturiesNoOverflow(centuries int) Carbon {
|
|
|
|
return c.AddYearsNoOverflow(centuries * YearsPerCentury)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddCentury adds one century.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个世纪后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddCentury() Carbon {
|
|
|
|
return c.AddCenturies(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddCenturyNoOverflow adds one century without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个世纪后(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddCenturyNoOverflow() Carbon {
|
|
|
|
return c.AddCenturiesNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubCenturies subtracts some centuries.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个世纪前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubCenturies(centuries int) Carbon {
|
2021-08-05 19:45:47 +08:00
|
|
|
return c.SubYears(centuries * YearsPerCentury)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubCenturiesNoOverflow subtracts some centuries without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个世纪前(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubCenturiesNoOverflow(centuries int) Carbon {
|
|
|
|
return c.SubYearsNoOverflow(centuries * YearsPerCentury)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubCentury subtracts one century.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个世纪前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubCentury() Carbon {
|
|
|
|
return c.SubCenturies(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubCenturyNoOverflow subtracts one century without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个世纪前(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubCenturyNoOverflow() Carbon {
|
|
|
|
return c.SubCenturiesNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddDecades adds some decades.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个年代后
|
|
|
|
func (c Carbon) AddDecades(decades int) Carbon {
|
|
|
|
return c.AddYears(decades * YearsPerDecade)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddDecadesNoOverflow adds some decades without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个年代后(月份不溢出)
|
|
|
|
func (c Carbon) AddDecadesNoOverflow(decades int) Carbon {
|
|
|
|
return c.AddYearsNoOverflow(decades * YearsPerDecade)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddDecade adds one decade.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个年代后
|
|
|
|
func (c Carbon) AddDecade() Carbon {
|
|
|
|
return c.AddDecades(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddDecadeNoOverflow adds one decade without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个年代后(月份不溢出)
|
|
|
|
func (c Carbon) AddDecadeNoOverflow() Carbon {
|
|
|
|
return c.AddDecadesNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubDecades subtracts some decades.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个年代后
|
|
|
|
func (c Carbon) SubDecades(decades int) Carbon {
|
|
|
|
return c.SubYears(decades * YearsPerDecade)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubDecadesNoOverflow subtracts some decades without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个年代后(月份不溢出)
|
|
|
|
func (c Carbon) SubDecadesNoOverflow(decades int) Carbon {
|
|
|
|
return c.SubYearsNoOverflow(decades * YearsPerDecade)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubDecade subtracts one decade.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个年代后
|
|
|
|
func (c Carbon) SubDecade() Carbon {
|
|
|
|
return c.SubDecades(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubDecadeNoOverflow subtracts one decade without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个年代后(月份不溢出)
|
|
|
|
func (c Carbon) SubDecadeNoOverflow() Carbon {
|
|
|
|
return c.SubDecadesNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddYears adds some years.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N年后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddYears(years int) Carbon {
|
2021-08-05 19:45:47 +08:00
|
|
|
if c.IsInvalid() {
|
2021-07-19 09:55:28 +08:00
|
|
|
return c
|
|
|
|
}
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time = c.time.In(c.loc).AddDate(years, 0, 0)
|
2021-02-18 14:32:31 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddYearsNoOverflow adds some years without overflowing month.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N年后(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddYearsNoOverflow(years int) Carbon {
|
2021-08-05 19:45:47 +08:00
|
|
|
if c.IsInvalid() {
|
2021-07-19 09:55:28 +08:00
|
|
|
return c
|
|
|
|
}
|
2022-04-12 17:39:17 +08:00
|
|
|
nanosecond := c.Nanosecond()
|
|
|
|
year, month, day, hour, minute, second := c.DateTime()
|
2021-02-18 14:32:31 +08:00
|
|
|
// 获取N年后本月的最后一天
|
2022-04-12 23:24:49 +08:00
|
|
|
lastYear, lastMonth, lastDay := c.create(year+years, month+1, 0, hour, minute, second, nanosecond).Date()
|
2022-04-12 17:39:17 +08:00
|
|
|
if day > lastDay {
|
|
|
|
day = lastDay
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
2022-04-12 23:24:49 +08:00
|
|
|
return c.create(lastYear, lastMonth, day, hour, minute, second, nanosecond)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddYear adds one year.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1年后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddYear() Carbon {
|
|
|
|
return c.AddYears(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddYearNoOverflow adds one year without overflowing month.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1年后(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddYearNoOverflow() Carbon {
|
|
|
|
return c.AddYearsNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubYears subtracts some years.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N年前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubYears(years int) Carbon {
|
2021-08-05 19:45:47 +08:00
|
|
|
if c.IsInvalid() {
|
2021-07-19 09:55:28 +08:00
|
|
|
return c
|
|
|
|
}
|
2021-02-18 14:32:31 +08:00
|
|
|
return c.AddYears(-years)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubYearsNoOverflow subtracts some years without overflowing month.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N年前(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubYearsNoOverflow(years int) Carbon {
|
|
|
|
return c.AddYearsNoOverflow(-years)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubYear subtracts one year.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1年前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubYear() Carbon {
|
|
|
|
return c.SubYears(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubYearNoOverflow subtracts one year without overflowing month.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1年前(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubYearNoOverflow() Carbon {
|
|
|
|
return c.SubYearsNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddQuarters adds some quarters
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个季度后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddQuarters(quarters int) Carbon {
|
|
|
|
return c.AddMonths(quarters * MonthsPerQuarter)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddQuartersNoOverflow adds quarters without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个季度后(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddQuartersNoOverflow(quarters int) Carbon {
|
|
|
|
return c.AddMonthsNoOverflow(quarters * MonthsPerQuarter)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddQuarter adds one quarter
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个季度后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddQuarter() Carbon {
|
|
|
|
return c.AddQuarters(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddQuarterNoOverflow adds one quarter without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个季度后(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddQuarterNoOverflow() Carbon {
|
|
|
|
return c.AddQuartersNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubQuarters subtracts some quarters.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个季度前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubQuarters(quarters int) Carbon {
|
|
|
|
return c.AddQuarters(-quarters)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubQuartersNoOverflow subtracts some quarters without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个季度前(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubQuartersNoOverflow(quarters int) Carbon {
|
|
|
|
return c.AddMonthsNoOverflow(-quarters * MonthsPerQuarter)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubQuarter subtracts one quarter.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个季度前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubQuarter() Carbon {
|
|
|
|
return c.SubQuarters(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubQuarterNoOverflow subtracts one quarter without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个季度前(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubQuarterNoOverflow() Carbon {
|
|
|
|
return c.SubQuartersNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddMonths adds some months.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个月后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddMonths(months int) Carbon {
|
2021-08-05 19:45:47 +08:00
|
|
|
if c.IsInvalid() {
|
2021-07-19 09:55:28 +08:00
|
|
|
return c
|
|
|
|
}
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time = c.time.In(c.loc).AddDate(0, months, 0)
|
2021-02-18 14:32:31 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddMonthsNoOverflow adds some months without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个月后(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddMonthsNoOverflow(months int) Carbon {
|
2021-08-05 19:45:47 +08:00
|
|
|
if c.IsInvalid() {
|
2021-07-19 09:55:28 +08:00
|
|
|
return c
|
|
|
|
}
|
2022-04-12 17:39:17 +08:00
|
|
|
nanosecond := c.Nanosecond()
|
|
|
|
year, month, day, hour, minute, second := c.DateTime()
|
2021-02-18 14:32:31 +08:00
|
|
|
// 获取N月后的最后一天
|
2022-04-12 23:24:49 +08:00
|
|
|
lastYear, lastMonth, lastDay := c.create(year, month+months+1, 0, hour, minute, second, nanosecond).Date()
|
2022-04-12 17:39:17 +08:00
|
|
|
if day > lastDay {
|
|
|
|
day = lastDay
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
2022-04-12 23:24:49 +08:00
|
|
|
return c.create(lastYear, lastMonth, day, hour, minute, second, nanosecond)
|
2021-02-18 14:32:31 +08:00
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddMonth adds one month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个月后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddMonth() Carbon {
|
|
|
|
return c.AddMonths(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddMonthNoOverflow adds one month without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个月后(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddMonthNoOverflow() Carbon {
|
|
|
|
return c.AddMonthsNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubMonths subtracts some months.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个月前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubMonths(months int) Carbon {
|
|
|
|
return c.AddMonths(-months)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubMonthsNoOverflow subtracts some months without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// N个月前(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubMonthsNoOverflow(months int) Carbon {
|
|
|
|
return c.AddMonthsNoOverflow(-months)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubMonth subtracts one month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个月前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubMonth() Carbon {
|
|
|
|
return c.SubMonths(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubMonthNoOverflow subtracts one month without overflowing month.
|
2021-08-05 19:45:47 +08:00
|
|
|
// 1个月前(月份不溢出)
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubMonthNoOverflow() Carbon {
|
|
|
|
return c.SubMonthsNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddWeeks adds some weeks.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N周后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddWeeks(weeks int) Carbon {
|
|
|
|
return c.AddDays(weeks * DaysPerWeek)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddWeek adds one week.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1周后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddWeek() Carbon {
|
|
|
|
return c.AddWeeks(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubWeeks subtracts some weeks.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N周前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubWeeks(weeks int) Carbon {
|
|
|
|
return c.SubDays(weeks * DaysPerWeek)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubWeek subtracts one week.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1周前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubWeek() Carbon {
|
|
|
|
return c.SubWeeks(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddDays adds some days.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N天后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddDays(days int) Carbon {
|
2021-08-05 19:45:47 +08:00
|
|
|
if c.IsInvalid() {
|
2021-07-19 09:55:28 +08:00
|
|
|
return c
|
|
|
|
}
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time = c.time.In(c.loc).AddDate(0, 0, days)
|
2021-02-18 14:32:31 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddDay adds one day.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1天后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddDay() Carbon {
|
|
|
|
return c.AddDays(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubDays subtracts some days.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N天前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubDays(days int) Carbon {
|
|
|
|
return c.AddDays(-days)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubDay subtracts one day.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1天前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubDay() Carbon {
|
|
|
|
return c.SubDays(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddHours adds some hours.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N小时后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddHours(hours int) Carbon {
|
2021-08-05 19:45:47 +08:00
|
|
|
if c.IsInvalid() {
|
2021-07-19 09:55:28 +08:00
|
|
|
return c
|
|
|
|
}
|
2021-02-18 14:32:31 +08:00
|
|
|
td := time.Duration(hours) * time.Hour
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time = c.time.In(c.loc).Add(td)
|
2021-02-18 14:32:31 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddHour adds one hour.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1小时后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddHour() Carbon {
|
|
|
|
return c.AddHours(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubHours subtracts some hours.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N小时前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubHours(hours int) Carbon {
|
|
|
|
return c.AddHours(-hours)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubHour subtracts one hour.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1小时前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubHour() Carbon {
|
|
|
|
return c.SubHours(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddMinutes adds some minutes.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N分钟后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddMinutes(minutes int) Carbon {
|
2021-08-05 19:45:47 +08:00
|
|
|
if c.IsInvalid() {
|
2021-07-19 09:55:28 +08:00
|
|
|
return c
|
|
|
|
}
|
2021-02-18 14:32:31 +08:00
|
|
|
td := time.Duration(minutes) * time.Minute
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time = c.time.In(c.loc).Add(td)
|
2021-02-18 14:32:31 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddMinute adds one minute.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1分钟后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddMinute() Carbon {
|
|
|
|
return c.AddMinutes(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubMinutes subtracts some minutes.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N分钟前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubMinutes(minutes int) Carbon {
|
|
|
|
return c.AddMinutes(-minutes)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubMinute subtracts one minute.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1分钟前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubMinute() Carbon {
|
|
|
|
return c.SubMinutes(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddSeconds adds some seconds.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N秒钟后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddSeconds(seconds int) Carbon {
|
2021-08-05 19:45:47 +08:00
|
|
|
if c.IsInvalid() {
|
2021-07-19 09:55:28 +08:00
|
|
|
return c
|
|
|
|
}
|
2021-02-18 14:32:31 +08:00
|
|
|
td := time.Duration(seconds) * time.Second
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time = c.time.In(c.loc).Add(td)
|
2021-02-18 14:32:31 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// AddSecond adds one second.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1秒钟后
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) AddSecond() Carbon {
|
|
|
|
return c.AddSeconds(1)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubSeconds subtracts some seconds.
|
2021-07-28 15:18:05 +08:00
|
|
|
// N秒钟前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubSeconds(seconds int) Carbon {
|
|
|
|
return c.AddSeconds(-seconds)
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:00:19 +08:00
|
|
|
// SubSecond subtracts one second.
|
2021-07-28 15:18:05 +08:00
|
|
|
// 1秒钟前
|
2021-02-18 14:32:31 +08:00
|
|
|
func (c Carbon) SubSecond() Carbon {
|
|
|
|
return c.SubSeconds(1)
|
|
|
|
}
|
2022-05-01 09:09:06 +08:00
|
|
|
|
|
|
|
// AddMilliseconds adds some milliseconds.
|
|
|
|
// N毫秒后
|
|
|
|
func (c Carbon) AddMilliseconds(milliseconds int) Carbon {
|
|
|
|
if c.IsInvalid() {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
td := time.Duration(milliseconds) * time.Millisecond
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time = c.time.In(c.loc).Add(td)
|
2022-05-01 09:09:06 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddMillisecond adds one millisecond.
|
|
|
|
// 1毫秒后
|
|
|
|
func (c Carbon) AddMillisecond() Carbon {
|
|
|
|
return c.AddMilliseconds(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubMilliseconds subtracts some milliseconds.
|
|
|
|
// N毫秒前
|
|
|
|
func (c Carbon) SubMilliseconds(milliseconds int) Carbon {
|
|
|
|
return c.AddMilliseconds(-milliseconds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubMillisecond subtracts one millisecond.
|
|
|
|
// 1毫秒前
|
|
|
|
func (c Carbon) SubMillisecond() Carbon {
|
|
|
|
return c.SubMilliseconds(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddMicroseconds adds some microseconds.
|
|
|
|
// N微秒后
|
|
|
|
func (c Carbon) AddMicroseconds(microseconds int) Carbon {
|
|
|
|
if c.IsInvalid() {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
td := time.Duration(microseconds) * time.Microsecond
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time = c.time.In(c.loc).Add(td)
|
2022-05-01 09:09:06 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddMicrosecond adds one microsecond.
|
|
|
|
// 1微秒后
|
|
|
|
func (c Carbon) AddMicrosecond() Carbon {
|
|
|
|
return c.AddMicroseconds(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubMicroseconds subtracts some microseconds.
|
|
|
|
// N微秒前
|
|
|
|
func (c Carbon) SubMicroseconds(microseconds int) Carbon {
|
|
|
|
return c.AddMicroseconds(-microseconds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubMicrosecond subtracts one microsecond.
|
|
|
|
// 1微秒前
|
|
|
|
func (c Carbon) SubMicrosecond() Carbon {
|
|
|
|
return c.SubMicroseconds(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddNanoseconds adds some nanoseconds.
|
|
|
|
// N纳秒后
|
|
|
|
func (c Carbon) AddNanoseconds(nanoseconds int) Carbon {
|
|
|
|
if c.IsInvalid() {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
td := time.Duration(nanoseconds) * time.Nanosecond
|
2023-01-07 21:50:09 +08:00
|
|
|
c.time = c.time.In(c.loc).Add(td)
|
2022-05-01 09:09:06 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-05-03 23:28:35 +08:00
|
|
|
// AddNanosecond adds one nanosecond.
|
2022-05-01 09:09:06 +08:00
|
|
|
// 1纳秒后
|
|
|
|
func (c Carbon) AddNanosecond() Carbon {
|
|
|
|
return c.AddNanoseconds(1)
|
|
|
|
}
|
|
|
|
|
2022-05-03 13:44:40 +08:00
|
|
|
// SubNanoseconds subtracts some nanoseconds.
|
2022-05-01 09:09:06 +08:00
|
|
|
// N纳秒前
|
2022-05-03 13:44:40 +08:00
|
|
|
func (c Carbon) SubNanoseconds(nanoseconds int) Carbon {
|
|
|
|
return c.AddNanoseconds(-nanoseconds)
|
2022-05-01 09:09:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubNanosecond subtracts one nanosecond.
|
|
|
|
// 1纳秒前
|
|
|
|
func (c Carbon) SubNanosecond() Carbon {
|
|
|
|
return c.SubNanoseconds(1)
|
|
|
|
}
|