carbon/season.go

119 lines
2.4 KiB
Go
Raw Normal View History

2021-07-12 16:22:36 +08:00
package carbon
import (
"strings"
)
2022-04-14 11:32:01 +08:00
// Season gets season name according to the meteorological division method like "Spring", 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
2022-04-12 17:38:36 +08:00
month := c.Month()
2021-07-12 16:22:36 +08:00
switch {
2022-04-12 17:38:36 +08:00
case month == 3 || month == 4 || month == 5:
2021-07-12 16:22:36 +08:00
index = 0
2022-04-12 17:38:36 +08:00
case month == 6 || month == 7 || month == 8:
2021-07-12 16:22:36 +08:00
index = 1
2022-04-12 17:38:36 +08:00
case month == 9 || month == 10 || month == 11:
2021-07-12 16:22:36 +08:00
index = 2
2022-04-12 17:38:36 +08:00
case month == 12 || month == 1 || month == 2:
2021-07-12 16:22:36 +08:00
index = 3
}
2024-01-22 17:08:16 +08:00
c.lang.rw.Lock()
defer c.lang.rw.Unlock()
if seasons, ok := c.lang.resources["seasons"]; ok {
2021-07-12 16:22:36 +08:00
slice := strings.Split(seasons, "|")
2023-12-26 22:00:18 +08:00
if len(slice) == QuartersPerYear {
2021-08-02 10:22:23 +08:00
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
}
2022-04-12 17:38:36 +08:00
year, month, _ := c.Date()
if month == 1 || month == 2 {
2022-04-12 23:24:49 +08:00
return c.create(year-1, 12, 1, 0, 0, 0, 0)
2021-07-12 16:22:36 +08:00
}
2022-04-12 23:24:49 +08:00
return c.create(year, month/3*3, 1, 0, 0, 0, 0)
2021-07-12 16:22:36 +08:00
}
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
}
2022-04-12 17:38:36 +08:00
year, month, _ := c.Date()
if month == 1 || month == 2 {
2022-04-12 23:24:49 +08:00
return c.create(year, 3, 0, 23, 59, 59, 999999999)
2021-07-12 16:22:36 +08:00
}
2022-04-12 17:38:36 +08:00
if month == 12 {
2022-04-12 23:24:49 +08:00
return c.create(year+1, 3, 0, 23, 59, 59, 999999999)
2021-07-12 16:22:36 +08:00
}
2022-04-12 23:24:49 +08:00
return c.create(year, month/3*3+3, 0, 23, 59, 59, 999999999)
2021-07-12 16:22:36 +08:00
}
2021-09-16 09:52:35 +08:00
// IsSpring reports 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
}
2022-04-12 17:38:36 +08:00
month := c.Month()
if month == 3 || month == 4 || month == 5 {
2021-07-12 16:22:36 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsSummer reports 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
}
2022-04-12 17:38:36 +08:00
month := c.Month()
if month == 6 || month == 7 || month == 8 {
2021-07-12 16:22:36 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsAutumn reports 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
}
2022-04-12 17:38:36 +08:00
month := c.Month()
if month == 9 || month == 10 || month == 11 {
2021-07-12 16:22:36 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsWinter reports 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
}
2022-04-12 17:38:36 +08:00
month := c.Month()
if month == 12 || month == 1 || month == 2 {
2021-07-12 16:22:36 +08:00
return true
}
return false
}