carbon/boundary.go

202 lines
5.2 KiB
Go
Raw Normal View History

2021-02-18 14:32:31 +08:00
package carbon
2021-08-10 10:54:04 +08:00
// StartOfCentury returns a Carbon instance for start of the century.
2021-07-28 15:18:05 +08:00
// 本世纪开始时间
func (c Carbon) StartOfCentury() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 23:17:49 +08:00
return c.create(c.Year()/YearsPerCentury*YearsPerCentury, 1, 1, 0, 0, 0, 0)
}
2021-08-10 10:54:04 +08:00
// EndOfCentury returns a Carbon instance for end of the century.
2021-07-28 15:18:05 +08:00
// 本世纪结束时间
func (c Carbon) EndOfCentury() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 23:17:49 +08:00
return c.create(c.Year()/YearsPerCentury*YearsPerCentury+99, 12, 31, 23, 59, 59, 999999999)
}
2021-08-10 10:54:04 +08:00
// StartOfDecade returns a Carbon instance for start of the decade.
2021-07-28 15:18:05 +08:00
// 本年代开始时间
func (c Carbon) StartOfDecade() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 23:17:49 +08:00
return c.create(c.Year()/YearsPerDecade*YearsPerDecade, 1, 1, 0, 0, 0, 0)
}
2021-08-10 10:54:04 +08:00
// EndOfDecade returns a Carbon instance for end of the decade.
2021-07-28 15:18:05 +08:00
// 本年代结束时间
func (c Carbon) EndOfDecade() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 23:17:49 +08:00
return c.create(c.Year()/YearsPerDecade*YearsPerDecade+9, 12, 31, 23, 59, 59, 999999999)
}
2021-08-10 10:54:04 +08:00
// StartOfYear returns a Carbon instance for start of the year.
2021-07-28 15:18:05 +08:00
// 本年开始时间
2021-02-18 14:32:31 +08:00
func (c Carbon) StartOfYear() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 23:17:49 +08:00
return c.create(c.Year(), 1, 1, 0, 0, 0, 0)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:54:04 +08:00
// EndOfYear returns a Carbon instance for end of the year.
2021-07-28 15:18:05 +08:00
// 本年结束时间
2021-02-18 14:32:31 +08:00
func (c Carbon) EndOfYear() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 23:17:49 +08:00
return c.create(c.Year(), 12, 31, 23, 59, 59, 999999999)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:54:04 +08:00
// StartOfQuarter returns a Carbon instance for start of the quarter.
2021-07-28 15:18:05 +08:00
// 本季度开始时间
func (c Carbon) StartOfQuarter() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 17:15:39 +08:00
year, quarter, day := c.Year(), c.Quarter(), 1
2022-04-12 23:17:49 +08:00
return c.create(year, 3*quarter-2, day, 0, 0, 0, 0)
}
2021-08-10 10:54:04 +08:00
// EndOfQuarter returns a Carbon instance for end of the quarter.
2021-07-28 15:18:05 +08:00
// 本季度结束时间
func (c Carbon) EndOfQuarter() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 17:15:39 +08:00
year, quarter, day := c.Year(), c.Quarter(), 30
switch quarter {
case 1, 4:
day = 31
case 2, 3:
day = 30
}
2022-04-12 23:17:49 +08:00
return c.create(year, 3*quarter, day, 23, 59, 59, 999999999)
}
2021-08-10 10:54:04 +08:00
// StartOfMonth returns a Carbon instance for start of the month.
2021-07-28 15:18:05 +08:00
// 本月开始时间
2021-02-18 14:32:31 +08:00
func (c Carbon) StartOfMonth() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 17:15:39 +08:00
year, month, _ := c.Date()
2022-04-12 23:17:49 +08:00
return c.create(year, month, 1, 0, 0, 0, 0)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:54:04 +08:00
// EndOfMonth returns a Carbon instance for end of the month.
2021-07-28 15:18:05 +08:00
// 本月结束时间
2021-02-18 14:32:31 +08:00
func (c Carbon) EndOfMonth() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 17:15:39 +08:00
year, month, _ := c.Date()
2022-04-12 23:17:49 +08:00
return c.create(year, month+1, 0, 23, 59, 59, 999999999)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:54:04 +08:00
// StartOfWeek returns a Carbon instance for start of the week.
2021-07-28 15:18:05 +08:00
// 本周开始时间
func (c Carbon) StartOfWeek() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
dayOfWeek, weekStartsAt := c.DayOfWeek(), int(c.weekStartsAt)
return c.SubDays((DaysPerWeek + dayOfWeek - weekStartsAt) % DaysPerWeek).StartOfDay()
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:54:04 +08:00
// EndOfWeek returns a Carbon instance for end of the week.
2021-07-28 15:18:05 +08:00
// 本周结束时间
func (c Carbon) EndOfWeek() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
dayOfWeek, weekEndsAt := c.DayOfWeek(), int(c.weekStartsAt)+DaysPerWeek-1
return c.AddDays((DaysPerWeek - dayOfWeek + weekEndsAt) % DaysPerWeek).EndOfDay()
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:54:04 +08:00
// StartOfDay returns a Carbon instance for start of the day.
2021-07-28 15:18:05 +08:00
// 本日开始时间
2021-02-18 14:32:31 +08:00
func (c Carbon) StartOfDay() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 17:15:39 +08:00
year, month, day := c.Date()
2022-04-12 23:17:49 +08:00
return c.create(year, month, day, 0, 0, 0, 0)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:54:04 +08:00
// EndOfDay returns a Carbon instance for end of the day.
2021-07-28 15:18:05 +08:00
// 本日结束时间
2021-02-18 14:32:31 +08:00
func (c Carbon) EndOfDay() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 17:15:39 +08:00
year, month, day := c.Date()
2022-04-12 23:17:49 +08:00
return c.create(year, month, day, 23, 59, 59, 999999999)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:54:04 +08:00
// StartOfHour returns a Carbon instance for start of the hour.
2021-07-28 15:18:05 +08:00
// 小时开始时间
2021-02-18 14:32:31 +08:00
func (c Carbon) StartOfHour() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 17:15:39 +08:00
year, month, day := c.Date()
2022-04-12 23:17:49 +08:00
return c.create(year, month, day, c.Hour(), 0, 0, 0)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:54:04 +08:00
// EndOfHour returns a Carbon instance for end of the hour.
2021-07-28 15:18:05 +08:00
// 小时结束时间
2021-02-18 14:32:31 +08:00
func (c Carbon) EndOfHour() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 17:15:39 +08:00
year, month, day := c.Date()
2022-04-12 23:17:49 +08:00
return c.create(year, month, day, c.Hour(), 59, 59, 999999999)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:54:04 +08:00
// StartOfMinute returns a Carbon instance for start of the minute.
2021-07-28 15:18:05 +08:00
// 分钟开始时间
2021-02-18 14:32:31 +08:00
func (c Carbon) StartOfMinute() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 17:15:39 +08:00
year, month, day, hour, minute, _ := c.DateTime()
2022-04-12 23:17:49 +08:00
return c.create(year, month, day, hour, minute, 0, 0)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:54:04 +08:00
// EndOfMinute returns a Carbon instance for end of the minute.
2021-07-28 15:18:05 +08:00
// 分钟结束时间
2021-02-18 14:32:31 +08:00
func (c Carbon) EndOfMinute() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 17:15:39 +08:00
year, month, day, hour, minute, _ := c.DateTime()
2022-04-12 23:17:49 +08:00
return c.create(year, month, day, hour, minute, 59, 999999999)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:54:04 +08:00
// StartOfSecond returns a Carbon instance for start of the second.
2021-07-28 15:18:05 +08:00
// 秒开始时间
2021-02-18 14:32:31 +08:00
func (c Carbon) StartOfSecond() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 17:15:39 +08:00
year, month, day, hour, minute, second := c.DateTime()
2022-04-12 23:17:49 +08:00
return c.create(year, month, day, hour, minute, second, 0)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:54:04 +08:00
// EndOfSecond returns a Carbon instance for end of the second.
2021-07-28 15:18:05 +08:00
// 秒结束时间
2021-02-18 14:32:31 +08:00
func (c Carbon) EndOfSecond() Carbon {
2024-04-09 20:26:57 +08:00
if c.Error != nil {
return c
}
2022-04-12 17:15:39 +08:00
year, month, day, hour, minute, second := c.DateTime()
2022-04-12 23:17:49 +08:00
return c.create(year, month, day, hour, minute, second, 999999999)
2021-02-18 14:32:31 +08:00
}