2020-09-07 17:10:33 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
import (
|
2020-11-02 10:36:04 +08:00
|
|
|
"strconv"
|
2020-09-07 17:10:33 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-09-12 08:32:15 +08:00
|
|
|
type Carbon struct {
|
2021-01-25 17:56:01 +08:00
|
|
|
Time time.Time
|
|
|
|
Loc *time.Location
|
|
|
|
Error error
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// Now 当前
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) Now() Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
c.Time = time.Now()
|
|
|
|
return c
|
2020-09-12 08:53:59 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
// Now 当前(默认时区)
|
|
|
|
func Now() Carbon {
|
|
|
|
return SetTimezone(Local).Now()
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// Tomorrow 明天
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) Tomorrow() Carbon {
|
2021-01-29 09:42:51 +08:00
|
|
|
if c.Time.IsZero() {
|
|
|
|
c.Time = time.Now().AddDate(0, 0, 1)
|
|
|
|
} else {
|
|
|
|
c.Time = c.Time.AddDate(0, 0, 1)
|
|
|
|
}
|
2021-01-26 17:08:27 +08:00
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
// Tomorrow 明天(默认时区)
|
|
|
|
func Tomorrow() Carbon {
|
|
|
|
return SetTimezone(Local).Tomorrow()
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// Yesterday 昨天
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) Yesterday() Carbon {
|
2021-01-29 09:42:51 +08:00
|
|
|
if c.Time.IsZero() {
|
|
|
|
c.Time = time.Now().AddDate(0, 0, -1)
|
|
|
|
} else {
|
|
|
|
c.Time = c.Time.AddDate(0, 0, -1)
|
|
|
|
}
|
2021-01-26 17:08:27 +08:00
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
// Yesterday 昨天(默认时区)
|
|
|
|
func Yesterday() Carbon {
|
|
|
|
return SetTimezone(Local).Yesterday()
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// CreateFromTimestamp 从时间戳创建Carbon实例
|
2021-01-26 17:08:27 +08:00
|
|
|
func (c Carbon) CreateFromTimestamp(timestamp int64) Carbon {
|
2020-11-02 10:36:04 +08:00
|
|
|
ts := timestamp
|
|
|
|
switch len(strconv.FormatInt(timestamp, 10)) {
|
|
|
|
case 10:
|
|
|
|
ts = timestamp
|
|
|
|
case 13:
|
|
|
|
ts = timestamp / 1e3
|
|
|
|
case 16:
|
|
|
|
ts = timestamp / 1e6
|
|
|
|
case 19:
|
|
|
|
ts = timestamp / 1e9
|
|
|
|
default:
|
|
|
|
ts = 0
|
|
|
|
}
|
2021-01-26 17:08:27 +08:00
|
|
|
c.Time = time.Unix(ts, 0)
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
// CreateFromTimestamp 从时间戳创建Carbon实例(默认时区)
|
|
|
|
func CreateFromTimestamp(timestamp int64) Carbon {
|
|
|
|
return SetTimezone(Local).CreateFromTimestamp(timestamp)
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// CreateFromDateTime 从年月日时分秒创建Carbon实例
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) CreateFromDateTime(year int, month int, day int, hour int, minute int, second int) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
c.Time = time.Date(year, time.Month(month), day, hour, minute, second, 0, c.Loc)
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
// CreateFromDateTime 从年月日时分秒创建Carbon实例(默认时区)
|
|
|
|
func CreateFromDateTime(year int, month int, day int, hour int, minute int, second int) Carbon {
|
|
|
|
return SetTimezone(Local).CreateFromDateTime(year, month, day, hour, minute, second)
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// CreateFromDate 从年月日创建Carbon实例
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) CreateFromDate(year int, month int, day int) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
hour, minute, second := time.Now().Clock()
|
|
|
|
c.Time = time.Date(year, time.Month(month), day, hour, minute, second, 0, c.Loc)
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
// CreateFromDate 从年月日创建Carbon实例(默认时区)
|
|
|
|
func CreateFromDate(year int, month int, day int) Carbon {
|
|
|
|
return SetTimezone(Local).CreateFromDate(year, month, day)
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// CreateFromTime 从时分秒创建Carbon实例
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) CreateFromTime(hour int, minute int, second int) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
year, month, day := time.Now().Date()
|
|
|
|
c.Time = time.Date(year, month, day, hour, minute, second, 0, c.Loc)
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
// CreateFromTime 从时分秒创建Carbon实例(默认时区)
|
|
|
|
func CreateFromTime(hour int, minute int, second int) Carbon {
|
|
|
|
return SetTimezone(Local).CreateFromTime(hour, minute, second)
|
2020-10-10 16:11:22 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// Parse 将标准格式时间字符串解析成 Carbon 实例
|
2021-01-26 17:08:27 +08:00
|
|
|
func (c Carbon) Parse(value string) Carbon {
|
|
|
|
if c.Error != nil {
|
|
|
|
return c
|
|
|
|
}
|
2020-09-07 17:10:33 +08:00
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
layout := DateTimeFormat
|
|
|
|
|
2020-10-10 16:11:22 +08:00
|
|
|
if value == "" || value == "0" || value == "0000-00-00 00:00:00" || value == "0000-00-00" || value == "00:00:00" {
|
2021-01-26 17:08:27 +08:00
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-10 16:11:22 +08:00
|
|
|
if len(value) == 10 && strings.Count(value, "-") == 2 {
|
|
|
|
layout = DateFormat
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-11 10:51:15 +08:00
|
|
|
if strings.Index(value, "T") == 10 {
|
2020-10-10 16:11:22 +08:00
|
|
|
layout = RFC3339Format
|
2020-10-07 17:15:46 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
if _, err := strconv.ParseInt(value, 10, 64); err == nil {
|
|
|
|
switch len(value) {
|
|
|
|
case 8:
|
|
|
|
layout = ShortDateFormat
|
|
|
|
case 14:
|
|
|
|
layout = ShortDateTimeFormat
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
tt, err := parseByLayout(value, layout)
|
|
|
|
c.Time = tt
|
|
|
|
c.Error = err
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// Parse 将标准格式时间字符串解析成 Carbon 实例(默认时区)
|
2021-01-26 17:08:27 +08:00
|
|
|
func Parse(value string) Carbon {
|
|
|
|
return SetTimezone(Local).Parse(value)
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// ParseByFormat 将特殊格式时间字符串解析成 Carbon 实例
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) ParseByFormat(value string, format string) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
if c.Error != nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
layout := format2layout(format)
|
2021-02-01 21:29:21 +08:00
|
|
|
return c.ParseByLayout(value, layout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseByFormat 将特殊格式时间字符串解析成 Carbon 实例(默认时区)
|
|
|
|
func ParseByFormat(value string, format string) Carbon {
|
|
|
|
return SetTimezone(Local).ParseByFormat(value, format)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseByLayout 将布局时间字符串解析成 Carbon 实例
|
|
|
|
func (c Carbon) ParseByLayout(value string, layout string) Carbon {
|
|
|
|
if c.Error != nil {
|
|
|
|
return c
|
|
|
|
}
|
2021-01-26 17:08:27 +08:00
|
|
|
tt, err := parseByLayout(value, layout)
|
|
|
|
c.Time = tt
|
|
|
|
c.Error = err
|
|
|
|
return c
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// ParseByLayout 将布局时间字符串解析成 Carbon 实例(默认时区)
|
|
|
|
func ParseByLayout(value string, layout string) Carbon {
|
|
|
|
return SetTimezone(Local).ParseByLayout(value, layout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Time2Carbon 将 time.Time 转换成 Carbon
|
|
|
|
func Time2Carbon(tt time.Time) Carbon {
|
|
|
|
loc, _ := time.LoadLocation(Local)
|
|
|
|
return Carbon{Time: tt, Loc: loc}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Carbon2Time 将 Carbon 转换成 time.Time
|
|
|
|
func (c Carbon) Carbon2Time() time.Time {
|
|
|
|
return c.Time
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
2020-11-03 07:41:50 +08:00
|
|
|
// AddDurations 按照持续时间字符串增加时间
|
|
|
|
// 支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
|
|
|
|
func (c Carbon) AddDuration(duration string) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
if c.Error != nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
td, err := parseByDuration(duration)
|
|
|
|
c.Time = c.Time.Add(td)
|
|
|
|
c.Error = err
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 07:41:50 +08:00
|
|
|
// SubDurations 按照持续时间字符串减少时间
|
|
|
|
// 支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
|
|
|
|
func (c Carbon) SubDuration(duration string) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
if c.Error != nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
td, err := parseByDuration("-" + duration)
|
|
|
|
c.Time = c.Time.Add(td)
|
|
|
|
c.Error = err
|
2020-11-03 07:41:50 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
// AddCenturies N世纪后
|
|
|
|
func (c Carbon) AddCenturies(centuries int) Carbon {
|
|
|
|
return c.AddYears(YearsPerCentury * centuries)
|
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// AddCenturiesNoOverflow N世纪后(月份不溢出)
|
|
|
|
func (c Carbon) AddCenturiesNoOverflow(centuries int) Carbon {
|
|
|
|
return c.AddYearsNoOverflow(centuries * YearsPerCentury)
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
// AddCentury 1世纪后
|
|
|
|
func (c Carbon) AddCentury() Carbon {
|
2021-02-01 21:29:21 +08:00
|
|
|
return c.AddCenturies(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCenturyNoOverflow 1世纪后(月份不溢出)
|
|
|
|
func (c Carbon) AddCenturyNoOverflow() Carbon {
|
|
|
|
return c.AddCenturiesNoOverflow(1)
|
2021-01-26 17:08:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubCenturies N世纪前
|
|
|
|
func (c Carbon) SubCenturies(centuries int) Carbon {
|
|
|
|
return c.SubYears(YearsPerCentury * centuries)
|
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// SubCenturiesNoOverflow N世纪前(月份不溢出)
|
|
|
|
func (c Carbon) SubCenturiesNoOverflow(centuries int) Carbon {
|
|
|
|
return c.SubYearsNoOverflow(centuries * YearsPerCentury)
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
// SubCentury 1世纪前
|
|
|
|
func (c Carbon) SubCentury() Carbon {
|
2021-02-01 21:29:21 +08:00
|
|
|
return c.SubCenturies(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubCenturyNoOverflow 1世纪前(月份不溢出)
|
|
|
|
func (c Carbon) SubCenturyNoOverflow() Carbon {
|
|
|
|
return c.SubCenturiesNoOverflow(1)
|
2021-01-26 17:08:27 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// AddYears N年后
|
|
|
|
func (c Carbon) AddYears(years int) Carbon {
|
|
|
|
c.Time = c.Time.AddDate(years, 0, 0)
|
|
|
|
return c
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// AddYearsNoOverflow N年后(月份不溢出)
|
|
|
|
func (c Carbon) AddYearsNoOverflow(years int) Carbon {
|
|
|
|
year := c.Time.Year() + years
|
|
|
|
month := c.Time.Month()
|
|
|
|
day := c.Time.Day()
|
|
|
|
|
|
|
|
// 获取N年后本月的最后一天
|
|
|
|
last := time.Date(year, month, 1, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc).AddDate(0, 1, -1)
|
|
|
|
|
|
|
|
if day > last.Day() {
|
|
|
|
day = last.Day()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Time = time.Date(last.Year(), last.Month(), day, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// AddYear 1年后
|
|
|
|
func (c Carbon) AddYear() Carbon {
|
2021-02-01 21:29:21 +08:00
|
|
|
return c.AddYears(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddYearNoOverflow 1年后(月份不溢出)
|
|
|
|
func (c Carbon) AddYearNoOverflow() Carbon {
|
|
|
|
return c.AddYearsNoOverflow(1)
|
2020-09-08 16:01:01 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// SubYears N年前
|
|
|
|
func (c Carbon) SubYears(years int) Carbon {
|
|
|
|
c.Time = c.Time.AddDate(-years, 0, 0)
|
|
|
|
return c
|
2020-09-10 09:24:02 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// SubYearsWithOverflow N年前(月份不溢出)
|
|
|
|
func (c Carbon) SubYearsNoOverflow(years int) Carbon {
|
|
|
|
return c.AddYearsNoOverflow(-years)
|
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// SubYear 1年前
|
|
|
|
func (c Carbon) SubYear() Carbon {
|
2021-02-01 21:29:21 +08:00
|
|
|
return c.SubYears(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubYearWithOverflow 1年前(月份不溢出)
|
|
|
|
func (c Carbon) SubYearNoOverflow() Carbon {
|
|
|
|
return c.SubYearsNoOverflow(1)
|
2020-09-10 09:24:02 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// AddQuarters N季度后
|
2020-11-02 10:36:04 +08:00
|
|
|
func (c Carbon) AddQuarters(quarters int) Carbon {
|
|
|
|
return c.AddMonths(quarters * MonthsPerQuarter)
|
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// AddQuartersNoOverflow N季度后(月份不溢出)
|
|
|
|
func (c Carbon) AddQuartersNoOverflow(quarters int) Carbon {
|
|
|
|
return c.AddMonthsNoOverflow(quarters * MonthsPerQuarter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddQuarter 1季度后
|
2020-11-02 10:36:04 +08:00
|
|
|
func (c Carbon) AddQuarter() Carbon {
|
|
|
|
return c.AddQuarters(1)
|
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// NextQuarters 1季度后(月份不溢出)
|
|
|
|
func (c Carbon) AddQuarterNoOverflow() Carbon {
|
|
|
|
return c.AddQuartersNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubQuarters N季度前
|
2020-11-02 10:36:04 +08:00
|
|
|
func (c Carbon) SubQuarters(quarters int) Carbon {
|
|
|
|
return c.SubMonths(quarters * MonthsPerQuarter)
|
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// SubQuartersNoOverflow N季度前(月份不溢出)
|
|
|
|
func (c Carbon) SubQuartersNoOverflow(quarters int) Carbon {
|
|
|
|
return c.AddMonthsNoOverflow(-quarters * MonthsPerQuarter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubQuarter 1季度前
|
2020-11-02 10:36:04 +08:00
|
|
|
func (c Carbon) SubQuarter() Carbon {
|
|
|
|
return c.SubQuarters(1)
|
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// SubQuarterNoOverflow 1季度前(月份不溢出)
|
|
|
|
func (c Carbon) SubQuarterNoOverflow() Carbon {
|
|
|
|
return c.SubQuartersNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// AddMonths N月后
|
|
|
|
func (c Carbon) AddMonths(months int) Carbon {
|
|
|
|
c.Time = c.Time.AddDate(0, months, 0)
|
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// AddMonthsNoOverflow N月后(月份不溢出)
|
|
|
|
func (c Carbon) AddMonthsNoOverflow(months int) Carbon {
|
|
|
|
year := c.Time.Year()
|
|
|
|
month := c.Time.Month() + time.Month(months)
|
|
|
|
day := c.Time.Day()
|
|
|
|
|
|
|
|
// 获取N月后的最后一天
|
|
|
|
last := time.Date(year, month, 1, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc).AddDate(0, 1, -1)
|
|
|
|
|
|
|
|
if day > last.Day() {
|
|
|
|
day = last.Day()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Time = time.Date(last.Year(), last.Month(), day, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// AddMonth 1月后
|
|
|
|
func (c Carbon) AddMonth() Carbon {
|
|
|
|
return c.AddMonths(1)
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// AddMonthNoOverflow 1月后(月份不溢出)
|
|
|
|
func (c Carbon) AddMonthNoOverflow() Carbon {
|
|
|
|
return c.AddMonthsNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// SubMonths N月前
|
|
|
|
func (c Carbon) SubMonths(months int) Carbon {
|
|
|
|
c.Time = c.Time.AddDate(0, -months, 0)
|
|
|
|
return c
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// SubMonthsNoOverflow N月前(月份不溢出)
|
|
|
|
func (c Carbon) SubMonthsNoOverflow(months int) Carbon {
|
|
|
|
return c.AddMonthsNoOverflow(-months)
|
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// SubMonth 1月前
|
|
|
|
func (c Carbon) SubMonth() Carbon {
|
|
|
|
return c.SubMonths(1)
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 21:29:21 +08:00
|
|
|
// SubMonthNoOverflow 1月前(月份不溢出)
|
|
|
|
func (c Carbon) SubMonthNoOverflow() Carbon {
|
|
|
|
return c.SubMonthsNoOverflow(1)
|
|
|
|
}
|
|
|
|
|
2020-11-02 10:36:04 +08:00
|
|
|
// AddWeeks N周后
|
|
|
|
func (c Carbon) AddWeeks(weeks int) Carbon {
|
|
|
|
return c.AddDays(weeks * DaysPerWeek)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddWeek 1天后
|
|
|
|
func (c Carbon) AddWeek() Carbon {
|
|
|
|
return c.AddWeeks(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubWeeks N周后
|
|
|
|
func (c Carbon) SubWeeks(weeks int) Carbon {
|
|
|
|
return c.SubDays(weeks * DaysPerWeek)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubWeek 1天后
|
|
|
|
func (c Carbon) SubWeek() Carbon {
|
|
|
|
return c.SubWeeks(1)
|
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// AddDays N天后
|
|
|
|
func (c Carbon) AddDays(days int) Carbon {
|
|
|
|
c.Time = c.Time.AddDate(0, 0, days)
|
|
|
|
return c
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// AddDay 1天后
|
|
|
|
func (c Carbon) AddDay() Carbon {
|
|
|
|
return c.AddDays(1)
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// SubDays N天前
|
|
|
|
func (c Carbon) SubDays(days int) Carbon {
|
|
|
|
c.Time = c.Time.AddDate(0, 0, -days)
|
|
|
|
return c
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// SubDay 1天前
|
|
|
|
func (c Carbon) SubDay() Carbon {
|
|
|
|
return c.SubDays(1)
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// AddHours N小时后
|
|
|
|
func (c Carbon) AddHours(hours int) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
td := time.Duration(hours) * time.Hour
|
|
|
|
c.Time = c.Time.Add(td)
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// AddHour 1小时后
|
|
|
|
func (c Carbon) AddHour() Carbon {
|
|
|
|
return c.AddHours(1)
|
2020-09-13 13:21:47 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// SubHours N小时前
|
|
|
|
func (c Carbon) SubHours(hours int) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
td := time.Duration(hours) * -time.Hour
|
|
|
|
c.Time = c.Time.Add(td)
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-13 13:21:47 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// SubHour 1小时前
|
|
|
|
func (c Carbon) SubHour() Carbon {
|
|
|
|
return c.SubHours(1)
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// AddMinutes N分钟后
|
|
|
|
func (c Carbon) AddMinutes(minutes int) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
td := time.Duration(minutes) * time.Minute
|
|
|
|
c.Time = c.Time.Add(td)
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-13 19:04:34 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// AddMinute 1分钟后
|
|
|
|
func (c Carbon) AddMinute() Carbon {
|
|
|
|
return c.AddMinutes(1)
|
2020-09-13 19:04:34 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// SubMinutes N分钟前
|
|
|
|
func (c Carbon) SubMinutes(minutes int) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
td := time.Duration(minutes) * -time.Minute
|
|
|
|
c.Time = c.Time.Add(td)
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// SubMinute 1分钟前
|
|
|
|
func (c Carbon) SubMinute() Carbon {
|
|
|
|
return c.SubMinutes(1)
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// AddSeconds N秒钟后
|
|
|
|
func (c Carbon) AddSeconds(seconds int) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
td := time.Duration(seconds) * time.Second
|
|
|
|
c.Time = c.Time.Add(td)
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-09 20:14:23 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// AddSecond 1秒钟后
|
|
|
|
func (c Carbon) AddSecond() Carbon {
|
|
|
|
return c.AddSeconds(1)
|
2020-09-09 20:14:23 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// SubSeconds N秒钟前
|
|
|
|
func (c Carbon) SubSeconds(seconds int) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
td := time.Duration(seconds) * -time.Second
|
|
|
|
c.Time = c.Time.Add(td)
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-09 20:14:23 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// SubSecond 1秒钟前
|
|
|
|
func (c Carbon) SubSecond() Carbon {
|
|
|
|
return c.SubSeconds(1)
|
2020-09-09 20:14:23 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 09:34:10 +08:00
|
|
|
// StartOfYear 本年开始时间
|
|
|
|
func (c Carbon) StartOfYear() Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), 1, 1, 0, 0, 0, 0, c.Loc)
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-09-08 16:01:01 +08:00
|
|
|
|
2020-10-10 16:11:22 +08:00
|
|
|
// EndOfYear 本年结束时间
|
|
|
|
func (c Carbon) EndOfYear() Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), 12, 31, 23, 59, 59, 0, c.Loc)
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-13 15:08:23 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 09:34:10 +08:00
|
|
|
// StartOfMonth 本月开始时间
|
|
|
|
func (c Carbon) StartOfMonth() Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), c.Time.Month(), 1, 0, 0, 0, 0, c.Loc)
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-13 15:08:23 +08:00
|
|
|
}
|
|
|
|
|
2020-10-10 16:11:22 +08:00
|
|
|
// EndOfMonth 本月结束时间
|
|
|
|
func (c Carbon) EndOfMonth() Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
t := time.Date(c.Time.Year(), c.Time.Month(), 1, 23, 59, 59, 0, c.Loc)
|
2020-10-10 16:11:22 +08:00
|
|
|
c.Time = t.AddDate(0, 1, -1)
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-13 15:08:23 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 09:34:10 +08:00
|
|
|
// StartOfWeek 本周开始时间
|
|
|
|
func (c Carbon) StartOfWeek() Carbon {
|
2020-10-07 17:15:46 +08:00
|
|
|
days := c.Time.Weekday()
|
|
|
|
if days == 0 {
|
|
|
|
days = DaysPerWeek
|
2020-09-10 09:24:02 +08:00
|
|
|
}
|
2021-01-25 17:26:38 +08:00
|
|
|
t := time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 0, 0, 0, 0, c.Loc)
|
2020-10-10 16:11:22 +08:00
|
|
|
c.Time = t.AddDate(0, 0, int(1-days))
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-10 09:24:02 +08:00
|
|
|
}
|
|
|
|
|
2020-10-10 16:11:22 +08:00
|
|
|
// EndOfWeek 本周结束时间
|
|
|
|
func (c Carbon) EndOfWeek() Carbon {
|
2020-10-07 17:15:46 +08:00
|
|
|
days := c.Time.Weekday()
|
|
|
|
if days == 0 {
|
|
|
|
days = DaysPerWeek
|
2020-09-10 09:24:02 +08:00
|
|
|
}
|
2021-01-25 17:26:38 +08:00
|
|
|
t := time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 23, 59, 59, 0, c.Loc)
|
2020-10-10 16:11:22 +08:00
|
|
|
c.Time = t.AddDate(0, 0, int(DaysPerWeek-days))
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-10-22 09:34:10 +08:00
|
|
|
// StartOfDay 本日开始时间
|
|
|
|
func (c Carbon) StartOfDay() Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 0, 0, 0, 0, c.Loc)
|
2020-10-10 16:11:22 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndOfDay 本日结束时间
|
|
|
|
func (c Carbon) EndOfDay() Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 23, 59, 59, 0, c.Loc)
|
2020-10-10 16:11:22 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-10-22 09:34:10 +08:00
|
|
|
// StartOfHour 小时开始时间
|
|
|
|
func (c Carbon) StartOfHour() Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), 0, 0, 0, c.Loc)
|
2020-10-10 16:11:22 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndOfHour 小时结束时间
|
|
|
|
func (c Carbon) EndOfHour() Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), 59, 59, 0, c.Loc)
|
2020-10-10 16:11:22 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-10-22 09:34:10 +08:00
|
|
|
// StartOfMinute 分钟开始时间
|
|
|
|
func (c Carbon) StartOfMinute() Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), 0, 0, c.Loc)
|
2020-10-10 16:11:22 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndOfMinute 分钟结束时间
|
|
|
|
func (c Carbon) EndOfMinute() Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), 59, 0, c.Loc)
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-13 15:08:23 +08:00
|
|
|
}
|
2020-11-02 10:36:04 +08:00
|
|
|
|
|
|
|
// Timezone 设置时区
|
|
|
|
func SetTimezone(name string) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
loc, err := getLocationByTimezone(name)
|
|
|
|
return Carbon{Loc: loc, Error: err}
|
2020-11-02 10:36:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Timezone 设置时区
|
|
|
|
func (c Carbon) SetTimezone(name string) Carbon {
|
2021-01-26 17:08:27 +08:00
|
|
|
loc, err := getLocationByTimezone(name)
|
|
|
|
c.Loc = loc
|
|
|
|
c.Error = err
|
|
|
|
return c
|
2020-11-02 10:36:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetYear 设置年
|
|
|
|
func (c Carbon) SetYear(year int) Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(year, c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
|
2020-11-02 10:36:04 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMonth 设置月
|
|
|
|
func (c Carbon) SetMonth(month int) Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), time.Month(month), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
|
2020-11-02 10:36:04 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDay 设置日
|
|
|
|
func (c Carbon) SetDay(day int) Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), c.Time.Month(), day, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
|
2020-11-02 10:36:04 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHour 设置时
|
|
|
|
func (c Carbon) SetHour(hour int) Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), hour, c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
|
2020-11-02 10:36:04 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMinute 设置分
|
|
|
|
func (c Carbon) SetMinute(minute int) Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), minute, c.Time.Second(), c.Time.Nanosecond(), c.Loc)
|
2020-11-02 10:36:04 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSecond 设置秒
|
|
|
|
func (c Carbon) SetSecond(second int) Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), second, c.Time.Nanosecond(), c.Loc)
|
2020-11-02 10:36:04 +08:00
|
|
|
return c
|
|
|
|
}
|