carbon/season.go

116 lines
2.5 KiB
Go
Raw Normal View History

2021-07-12 16:22:36 +08:00
package carbon
import (
"strings"
"time"
)
2021-08-10 10:58:43 +08:00
// Season gets season name according to the meteorological division method, i18n is supported.
2021-07-28 15:18:05 +08:00
// 获取当前季节(以气象划分)支持i18n
2021-07-12 16:22:36 +08:00
func (c Carbon) Season() string {
2021-07-23 11:06:32 +08:00
if c.IsInvalid() {
2021-07-12 16:22:36 +08:00
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
2021-07-12 16:22:36 +08:00
}
index := -1
switch {
case c.Month() == 3 || c.Month() == 4 || c.Month() == 5:
index = 0
case c.Month() == 6 || c.Month() == 7 || c.Month() == 8:
index = 1
case c.Month() == 9 || c.Month() == 10 || c.Month() == 11:
index = 2
case c.Month() == 12 || c.Month() == 1 || c.Month() == 2:
index = 3
}
if seasons, ok := c.lang.resources["seasons"]; ok {
2021-07-12 16:22:36 +08:00
slice := strings.Split(seasons, "|")
2021-08-02 10:22:23 +08:00
if len(slice) == 4 {
return slice[index]
}
2021-07-12 16:22:36 +08:00
}
return ""
}
2021-08-10 10:58:43 +08:00
// StartOfSeason returns a Carbon instance for start of the season.
2021-07-28 15:18:05 +08:00
// 本季节开始时间
2021-07-12 16:22:36 +08:00
func (c Carbon) StartOfSeason() Carbon {
2021-07-23 11:06:32 +08:00
if c.IsInvalid() {
2021-07-12 16:22:36 +08:00
return c
}
if c.Month() == 1 || c.Month() == 2 {
c.Time = time.Date(c.Year()-1, time.Month(12), 1, 0, 0, 0, 0, c.loc)
2021-07-12 16:22:36 +08:00
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()/3*3), 1, 0, 0, 0, 0, c.loc)
2021-07-12 16:22:36 +08:00
return c
}
2021-08-10 10:58:43 +08:00
// EndOfSeason returns a Carbon instance for end of the season.
2021-07-28 15:18:05 +08:00
// 本季节结束时间
2021-07-12 16:22:36 +08:00
func (c Carbon) EndOfSeason() Carbon {
2021-07-23 11:06:32 +08:00
if c.IsInvalid() {
2021-07-12 16:22:36 +08:00
return c
}
if c.Month() == 1 || c.Month() == 2 {
c.Time = time.Date(c.Year(), time.Month(2), 1, 23, 59, 59, 999999999, c.loc).AddDate(0, 1, -1)
2021-07-12 16:22:36 +08:00
return c
}
if c.Month() == 12 {
c.Time = time.Date(c.Year()+1, time.Month(2), 1, 23, 59, 59, 999999999, c.loc).AddDate(0, 1, -1)
2021-07-12 16:22:36 +08:00
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()/3*3+2), 1, 23, 59, 59, 999999999, c.loc).AddDate(0, 1, -1)
2021-07-12 16:22:36 +08:00
return c
}
2021-08-10 10:58:43 +08:00
// IsSpring whether is spring.
2021-07-28 15:18:05 +08:00
// 是否是春季
2021-07-12 16:22:36 +08:00
func (c Carbon) IsSpring() bool {
2021-07-23 11:06:32 +08:00
if c.IsInvalid() {
2021-07-12 16:22:36 +08:00
return false
}
if c.Month() == 3 || c.Month() == 4 || c.Month() == 5 {
return true
}
return false
}
2021-08-10 10:58:43 +08:00
// IsSummer whether is summer.
2021-07-28 15:18:05 +08:00
// 是否是夏季
2021-07-12 16:22:36 +08:00
func (c Carbon) IsSummer() bool {
2021-07-23 11:06:32 +08:00
if c.IsInvalid() {
2021-07-12 16:22:36 +08:00
return false
}
if c.Month() == 6 || c.Month() == 7 || c.Month() == 8 {
return true
}
return false
}
2021-08-10 10:58:43 +08:00
// IsAutumn whether is autumn.
2021-07-28 15:18:05 +08:00
// 是否是秋季
2021-07-12 16:22:36 +08:00
func (c Carbon) IsAutumn() bool {
2021-07-23 11:06:32 +08:00
if c.IsInvalid() {
2021-07-12 16:22:36 +08:00
return false
}
if c.Month() == 9 || c.Month() == 10 || c.Month() == 11 {
return true
}
return false
}
2021-08-10 10:58:43 +08:00
// IsWinter whether is winter.
2021-07-28 15:18:05 +08:00
// 是否是冬季
2021-07-12 16:22:36 +08:00
func (c Carbon) IsWinter() bool {
2021-07-23 11:06:32 +08:00
if c.IsInvalid() {
2021-07-12 16:22:36 +08:00
return false
}
if c.Month() == 12 || c.Month() == 1 || c.Month() == 2 {
return true
}
return false
}