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
|
|
|
}
|
|
|
|
|
|
|
|
// Now 当前
|
2020-10-07 17:15:46 +08:00
|
|
|
func Now() Carbon {
|
|
|
|
return newCarbon(time.Now())
|
2020-09-12 08:53:59 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
|
|
|
// Now 当前(指定时区)
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) Now() Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
return newCarbon(Now().Time.In(c.Loc))
|
2020-09-12 08:53:59 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// Tomorrow 明天
|
|
|
|
func Tomorrow() Carbon {
|
|
|
|
return newCarbon(time.Now().AddDate(0, 0, 1))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
|
|
|
// Tomorrow 明天(指定时区)
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) Tomorrow() Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
return newCarbon(Tomorrow().Time.In(c.Loc))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// Yesterday 昨天
|
|
|
|
func Yesterday() Carbon {
|
|
|
|
return newCarbon(time.Now().AddDate(0, 0, -1))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
|
|
|
// Yesterday 昨天(指定时区)
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) Yesterday() Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
return newCarbon(Yesterday().Time.In(c.Loc))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// CreateFromTimestamp 从时间戳创建Carbon实例
|
|
|
|
func 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
|
|
|
|
}
|
|
|
|
return newCarbon(time.Unix(ts, 0))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
|
|
|
// CreateFromTimestamp 从时间戳创建Carbon实例(指定时区)
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) CreateFromTimestamp(timestamp int64) Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
return newCarbon(CreateFromTimestamp(timestamp).Time.In(c.Loc))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// CreateFromDateTime 从年月日时分秒创建Carbon实例
|
|
|
|
func CreateFromDateTime(year int, month int, day int, hour int, minute int, second int) Carbon {
|
|
|
|
return newCarbon(time.Date(year, time.Month(month), day, hour, minute, second, 0, getLocalByTimezone(Local)))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +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-25 17:26:38 +08:00
|
|
|
return newCarbon(CreateFromDateTime(year, month, day, hour, minute, second).Time.In(c.Loc))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// CreateFromDate 从年月日创建Carbon实例
|
|
|
|
func CreateFromDate(year int, month int, day int) Carbon {
|
|
|
|
hour, minute, second := time.Now().Clock()
|
|
|
|
return newCarbon(time.Date(year, time.Month(month), day, hour, minute, second, 0, getLocalByTimezone(Local)))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +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-25 17:26:38 +08:00
|
|
|
return newCarbon(CreateFromDate(year, month, day).Time.In(c.Loc))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// CreateFromTime 从时分秒创建Carbon实例
|
|
|
|
func CreateFromTime(hour int, minute int, second int) Carbon {
|
|
|
|
year, month, day := time.Now().Date()
|
|
|
|
return newCarbon(time.Date(year, month, day, hour, minute, second, 0, getLocalByTimezone(Local)))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +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-25 17:26:38 +08:00
|
|
|
return newCarbon(CreateFromTime(hour, minute, second).Time.In(c.Loc))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-10 16:11:22 +08:00
|
|
|
// CreateFromGoTime 从原生time.Time创建Carbon实例
|
|
|
|
func CreateFromGoTime(t time.Time) Carbon {
|
|
|
|
return newCarbon(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFromGoTime 从原生time.Time创建Carbon实例(指定时区)
|
|
|
|
func (c Carbon) CreateFromGoTime(t time.Time) Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
return newCarbon(CreateFromGoTime(t).Time.In(c.Loc))
|
2020-10-10 16:11:22 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// Parse 解析标准格式时间字符串
|
|
|
|
func Parse(value string) Carbon {
|
2020-10-10 16:11:22 +08:00
|
|
|
layout := DateTimeFormat
|
2020-09-07 17:10:33 +08:00
|
|
|
|
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-25 17:26:38 +08:00
|
|
|
return Carbon{Loc: getLocalByTimezone(Local)}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if len(value) == 14 {
|
2020-10-10 16:11:22 +08:00
|
|
|
layout = ShortDateTimeFormat
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(value) == 8 {
|
2020-10-10 16:11:22 +08:00
|
|
|
layout = ShortDateFormat
|
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
|
|
|
|
|
|
|
return newCarbon(parseByLayout(value, layout))
|
2020-10-07 17:15:46 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
|
|
|
// Parse 解析标准格式时间字符串(指定时区)
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) Parse(value string) Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
return newCarbon(Parse(value).Time.In(c.Loc))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// ParseByFormat 解析指定格式时间字符串
|
|
|
|
func ParseByFormat(value string, format string) Carbon {
|
2020-09-07 17:10:33 +08:00
|
|
|
value = strings.Trim(value, " ")
|
|
|
|
layout := format2layout(format)
|
2020-10-10 16:11:22 +08:00
|
|
|
return newCarbon(parseByLayout(value, layout))
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
|
|
|
// ParseByFormat 解析指定格式时间字符串(指定时区)
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) ParseByFormat(value string, format string) Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
return newCarbon(ParseByFormat(value, format).Time.In(c.Loc))
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// ParseByDuration 解析持续时间字符串(基于现在时间)
|
|
|
|
// 支持正负整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
|
|
|
|
func ParseByDuration(duration string) Carbon {
|
2020-10-10 16:11:22 +08:00
|
|
|
return newCarbon(time.Now().Add(parseByDuration(duration)))
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
|
|
|
// ParseByDuration 解析持续时间字符串(指定时区)
|
2020-10-07 17:15:46 +08:00
|
|
|
func (c Carbon) ParseByDuration(duration string) Carbon {
|
2021-01-25 17:26:38 +08:00
|
|
|
return newCarbon(ParseByDuration(duration).Time.In(c.Loc))
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 07:41:50 +08:00
|
|
|
// AddDurations 按照持续时间字符串增加时间
|
|
|
|
// 支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
|
|
|
|
func (c Carbon) AddDuration(duration string) Carbon {
|
2020-10-10 16:11:22 +08:00
|
|
|
c.Time = c.Time.Add(parseByDuration(duration))
|
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 {
|
|
|
|
c.Time = c.Time.Add(parseByDuration("-" + duration))
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
duration := time.Duration(hours) * time.Hour
|
|
|
|
c.Time = c.Time.Add(duration)
|
|
|
|
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 {
|
|
|
|
duration := time.Duration(hours) * -time.Hour
|
|
|
|
c.Time = c.Time.Add(duration)
|
|
|
|
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 {
|
|
|
|
duration := time.Duration(minutes) * time.Minute
|
|
|
|
c.Time = c.Time.Add(duration)
|
|
|
|
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 {
|
|
|
|
duration := time.Duration(minutes) * -time.Minute
|
|
|
|
c.Time = c.Time.Add(duration)
|
|
|
|
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 {
|
|
|
|
duration := time.Duration(seconds) * time.Second
|
|
|
|
c.Time = c.Time.Add(duration)
|
|
|
|
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 {
|
|
|
|
duration := time.Duration(seconds) * -time.Second
|
|
|
|
c.Time = c.Time.Add(duration)
|
|
|
|
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-07 17:15:46 +08:00
|
|
|
// NextYears N年后
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// NextYear 1年后
|
|
|
|
func (c Carbon) NextYear() Carbon {
|
|
|
|
return c.NextYears(1)
|
2020-09-09 20:14:23 +08:00
|
|
|
}
|
|
|
|
|
2020-11-02 10:36:04 +08:00
|
|
|
// PreYears N年前
|
|
|
|
func (c Carbon) PreYears(years int) Carbon {
|
|
|
|
return c.NextYears(-years)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PreYear 1年前
|
|
|
|
func (c Carbon) PreYear() Carbon {
|
|
|
|
return c.NextYears(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextQuarters N季度后
|
|
|
|
func (c Carbon) NextQuarters(quarters int) Carbon {
|
|
|
|
return c.NextMonths(quarters * MonthsPerQuarter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextQuarters 1季度后
|
|
|
|
func (c Carbon) NextQuarter() Carbon {
|
|
|
|
return c.NextQuarters(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PreQuarters N季度前
|
|
|
|
func (c Carbon) PreQuarters(quarters int) Carbon {
|
|
|
|
return c.NextMonths(-quarters * MonthsPerQuarter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PreQuarter 1季度前
|
|
|
|
func (c Carbon) PreQuarter() Carbon {
|
|
|
|
return c.PreQuarters(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-10-07 17:15:46 +08:00
|
|
|
// NextMonth 1月后
|
|
|
|
func (c Carbon) NextMonth() Carbon {
|
|
|
|
return c.NextMonths(1)
|
2020-09-07 17:10:33 +08:00
|
|
|
}
|
|
|
|
|
2020-11-02 10:36:04 +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
|
|
|
}
|
|
|
|
|
2020-11-02 10:36:04 +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-25 17:26:38 +08:00
|
|
|
return Carbon{Loc: getLocalByTimezone(name)}
|
2020-11-02 10:36:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Timezone 设置时区
|
|
|
|
func (c Carbon) SetTimezone(name string) Carbon {
|
|
|
|
loc := getLocalByTimezone(name)
|
2021-01-25 17:26:38 +08:00
|
|
|
return Carbon{Time: c.Time.In(c.Loc), Loc: loc}
|
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
|
|
|
|
}
|