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