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-01-29 09:42:51 +08:00
|
|
|
// CreateFromGoTime 从原生time.Time创建Carbon实例
|
2021-01-26 17:08:27 +08:00
|
|
|
func (c Carbon) CreateFromGoTime(tt time.Time) Carbon {
|
|
|
|
c.Time = tt
|
|
|
|
return c
|
2020-10-10 16:11:22 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
// CreateFromGoTime 从原生time.Time创建Carbon实例(默认时区)
|
|
|
|
func CreateFromGoTime(tt time.Time) Carbon {
|
|
|
|
return SetTimezone(Local).CreateFromGoTime(tt)
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// Parse 解析标准格式时间字符串
|
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-01-26 17:08:27 +08:00
|
|
|
// Parse 解析标准格式时间字符串(默认时区)
|
|
|
|
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-01-29 09:42:51 +08:00
|
|
|
// ParseByFormat 解析指定格式时间字符串
|
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)
|
|
|
|
tt, err := parseByLayout(value, layout)
|
|
|
|
c.Time = tt
|
|
|
|
c.Error = err
|
|
|
|
return c
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 17:08:27 +08:00
|
|
|
// ParseByFormat 解析指定格式时间字符串(默认时区)
|
|
|
|
func ParseByFormat(value string, format string) Carbon {
|
|
|
|
return SetTimezone(Local).ParseByFormat(value, format)
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCentury 1世纪后
|
|
|
|
func (c Carbon) AddCentury() Carbon {
|
|
|
|
return c.AddYears(YearsPerCentury)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubCenturies N世纪前
|
|
|
|
func (c Carbon) SubCenturies(centuries int) Carbon {
|
|
|
|
return c.SubYears(YearsPerCentury * centuries)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubCentury 1世纪前
|
|
|
|
func (c Carbon) SubCentury() Carbon {
|
|
|
|
return c.SubYears(YearsPerCentury)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// AddYear 1年后
|
|
|
|
func (c Carbon) AddYear() Carbon {
|
|
|
|
c.Time = c.Time.AddDate(1, 0, 0)
|
|
|
|
return c
|
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
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// SubYear 1年前
|
|
|
|
func (c Carbon) SubYear() Carbon {
|
|
|
|
c.Time = c.Time.AddDate(-1, 0, 0)
|
|
|
|
return c
|
2020-09-10 09:24:02 +08:00
|
|
|
}
|
|
|
|
|
2020-11-02 10:36:04 +08:00
|
|
|
// AddMonths N季度后
|
|
|
|
func (c Carbon) AddQuarters(quarters int) Carbon {
|
|
|
|
return c.AddMonths(quarters * MonthsPerQuarter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddMonth 1季度后
|
|
|
|
func (c Carbon) AddQuarter() Carbon {
|
|
|
|
return c.AddQuarters(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubMonths N季度前
|
|
|
|
func (c Carbon) SubQuarters(quarters int) Carbon {
|
|
|
|
return c.SubMonths(quarters * MonthsPerQuarter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubMonth 1季度前
|
|
|
|
func (c Carbon) SubQuarter() Carbon {
|
|
|
|
return c.SubQuarters(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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// NextCenturies N世纪后(不会出现月份溢出)
|
2021-01-26 17:08:27 +08:00
|
|
|
func (c Carbon) NextCenturies(centuries int) Carbon {
|
|
|
|
return c.NextYears(centuries * YearsPerCentury)
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// NextCentury 1世纪后(不会出现月份溢出)
|
2021-01-26 17:08:27 +08:00
|
|
|
func (c Carbon) NextCentury() Carbon {
|
|
|
|
return c.NextYears(YearsPerCentury)
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// PreCenturies N世纪前(不会出现月份溢出)
|
2021-01-26 17:08:27 +08:00
|
|
|
func (c Carbon) PreCenturies(centuries int) Carbon {
|
|
|
|
return c.PreYears(centuries * YearsPerCentury)
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// PreYears 1世纪前(不会出现月份溢出)
|
2021-01-26 17:08:27 +08:00
|
|
|
func (c Carbon) PreCentury() Carbon {
|
|
|
|
return c.PreYears(YearsPerCentury)
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// NextYears N年后(不会出现月份溢出)
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) NextYears(years int) Carbon {
|
|
|
|
year := c.Time.Year() + years
|
|
|
|
month := c.Time.Month()
|
|
|
|
day := c.Time.Day()
|
2020-09-09 20:14:23 +08:00
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// 获取N年后本月的最后一天
|
2021-01-25 17:26:38 +08:00
|
|
|
last := time.Date(year, month, 1, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc).AddDate(0, 1, -1)
|
2020-09-09 20:14:23 +08:00
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
if day > last.Day() {
|
|
|
|
day = last.Day()
|
|
|
|
}
|
2020-09-09 20:14:23 +08:00
|
|
|
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(last.Year(), last.Month(), day, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-09 20:14:23 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// NextYear 1年后(不会出现月份溢出)
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) NextYear() Carbon {
|
|
|
|
return c.NextYears(1)
|
2020-09-09 20:14:23 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// PreYears N年前(不会出现月份溢出)
|
2020-11-02 10:36:04 +08:00
|
|
|
func (c Carbon) PreYears(years int) Carbon {
|
|
|
|
return c.NextYears(-years)
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// PreYear 1年前(不会出现月份溢出)
|
2020-11-02 10:36:04 +08:00
|
|
|
func (c Carbon) PreYear() Carbon {
|
|
|
|
return c.NextYears(-1)
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// NextQuarters N季度后(不会出现月份溢出)
|
2020-11-02 10:36:04 +08:00
|
|
|
func (c Carbon) NextQuarters(quarters int) Carbon {
|
|
|
|
return c.NextMonths(quarters * MonthsPerQuarter)
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// NextQuarters 1季度后(不会出现月份溢出)
|
2020-11-02 10:36:04 +08:00
|
|
|
func (c Carbon) NextQuarter() Carbon {
|
|
|
|
return c.NextQuarters(1)
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// PreQuarters N季度前(不会出现月份溢出)
|
2020-11-02 10:36:04 +08:00
|
|
|
func (c Carbon) PreQuarters(quarters int) Carbon {
|
|
|
|
return c.NextMonths(-quarters * MonthsPerQuarter)
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// PreQuarter 1季度前(不会出现月份溢出)
|
2020-11-02 10:36:04 +08:00
|
|
|
func (c Carbon) PreQuarter() Carbon {
|
|
|
|
return c.PreQuarters(1)
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// NextMonths N月后(不会出现月份溢出)
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) NextMonths(months int) Carbon {
|
|
|
|
year := c.Time.Year()
|
|
|
|
month := c.Time.Month() + time.Month(months)
|
|
|
|
day := c.Time.Day()
|
2020-09-09 20:14:23 +08:00
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// 获取N月后的最后一天
|
2021-01-25 17:26:38 +08:00
|
|
|
last := time.Date(year, month, 1, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc).AddDate(0, 1, -1)
|
2020-09-09 20:14:23 +08:00
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
if day > last.Day() {
|
|
|
|
day = last.Day()
|
|
|
|
}
|
2020-09-09 20:14:23 +08:00
|
|
|
|
2021-01-25 17:26:38 +08:00
|
|
|
c.Time = time.Date(last.Year(), last.Month(), day, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
|
2020-10-07 17:15:46 +08:00
|
|
|
return c
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// NextMonth 1月后(不会出现月份溢出)
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) NextMonth() Carbon {
|
|
|
|
return c.NextMonths(1)
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// PreMonths N月前(不会出现月份溢出)
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) PreMonths(months int) Carbon {
|
|
|
|
return c.NextMonths(-months)
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:42:51 +08:00
|
|
|
// PreMonth 1月前(不会出现月份溢出)
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) PreMonth() Carbon {
|
2020-11-02 10:36:04 +08:00
|
|
|
return c.PreMonths(1)
|
2020-09-07 17:10:33 +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
|
|
|
|
}
|