精简 Season 方法

This commit is contained in:
Peleus 2024-02-01 11:25:07 +08:00
parent 00cb194cd8
commit f286051e26

View File

@ -4,6 +4,23 @@ import (
"strings" "strings"
) )
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
}
// Season gets season name according to the meteorological division method like "Spring", i18n is supported. // Season gets season name according to the meteorological division method like "Spring", i18n is supported.
// 获取当前季节(以气象划分)支持i18n // 获取当前季节(以气象划分)支持i18n
func (c Carbon) Season() string { func (c Carbon) Season() string {
@ -15,20 +32,16 @@ func (c Carbon) Season() string {
} }
index := -1 index := -1
month := c.Month() month := c.Month()
switch { for i := 0; i < len(seasons); i++ {
case month == 3 || month == 4 || month == 5: season := seasons[i]
index = 0 if month == season.month {
case month == 6 || month == 7 || month == 8: index = season.index
index = 1 }
case month == 9 || month == 10 || month == 11:
index = 2
case month == 12 || month == 1 || month == 2:
index = 3
} }
c.lang.rw.Lock() c.lang.rw.Lock()
defer c.lang.rw.Unlock() defer c.lang.rw.Unlock()
if seasons, ok := c.lang.resources["seasons"]; ok { if resources, ok := c.lang.resources["seasons"]; ok {
slice := strings.Split(seasons, "|") slice := strings.Split(resources, "|")
if len(slice) == QuartersPerYear { if len(slice) == QuartersPerYear {
return slice[index] return slice[index]
} }