carbon/season.go

132 lines
2.6 KiB
Go
Raw Normal View History

2021-07-12 16:22:36 +08:00
package carbon
import (
"strings"
)
2024-02-01 11:25:07 +08:00
var seasons = []struct {
month, index int
}{
{3, 0}, // spring
{4, 0}, // spring
{5, 0}, // spring
{6, 1}, // summer
{7, 1}, // summer
{8, 1}, // summer
{9, 2}, // autumn
{10, 2}, // autumn
{11, 2}, // autumn
{12, 3}, // winter
{1, 3}, // winter
{2, 3}, // winter
}
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 {
2024-04-09 20:14:16 +08:00
if c.Error != nil {
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()
2024-02-01 11:25:07 +08:00
for i := 0; i < len(seasons); i++ {
season := seasons[i]
if month == season.month {
index = season.index
}
2021-07-12 16:22:36 +08:00
}
2024-11-25 16:15:33 +08:00
c.lang.rw.RLock()
defer c.lang.rw.RUnlock()
2024-02-01 11:25:07 +08:00
if resources, ok := c.lang.resources["seasons"]; ok {
slice := strings.Split(resources, "|")
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 {
2024-04-09 20:14:16 +08:00
if c.Error != nil {
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 {
2024-04-09 20:14:16 +08:00
if c.Error != nil {
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 {
2024-04-09 20:14:16 +08:00
if c.Error != nil {
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 {
2024-04-09 20:14:16 +08:00
if c.Error != nil {
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 {
2024-04-09 20:14:16 +08:00
if c.Error != nil {
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 {
2024-04-09 20:14:16 +08:00
if c.Error != nil {
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
}