2 Star 0 Fork 70

golang-package/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
season.go 2.37 KB
一键复制 编辑 原始数据 按行查看 历史
package carbon
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.
func (c *Carbon) Season() string {
if c.IsInvalid() {
return ""
}
index := -1
month := c.Month()
for i := 0; i < len(seasons); i++ {
season := seasons[i]
if month == season.month {
index = season.index
}
}
c.lang.rw.RLock()
defer c.lang.rw.RUnlock()
if resources, ok := c.lang.resources["seasons"]; ok {
slice := strings.Split(resources, "|")
if len(slice) == QuartersPerYear {
return slice[index]
}
}
return ""
}
// StartOfSeason returns a Carbon instance for start of the season.
func (c *Carbon) StartOfSeason() *Carbon {
if c.IsInvalid() {
return c
}
year, month, _ := c.Date()
if month == 1 || month == 2 {
return c.create(year-1, 12, 1, 0, 0, 0, 0)
}
return c.create(year, month/3*3, 1, 0, 0, 0, 0)
}
// EndOfSeason returns a Carbon instance for end of the season.
func (c *Carbon) EndOfSeason() *Carbon {
if c.IsInvalid() {
return c
}
year, month, _ := c.Date()
if month == 1 || month == 2 {
return c.create(year, 3, 0, 23, 59, 59, 999999999)
}
if month == 12 {
return c.create(year+1, 3, 0, 23, 59, 59, 999999999)
}
return c.create(year, month/3*3+3, 0, 23, 59, 59, 999999999)
}
// IsSpring reports whether is spring.
func (c *Carbon) IsSpring() bool {
if c.IsInvalid() {
return false
}
month := c.Month()
if month == 3 || month == 4 || month == 5 {
return true
}
return false
}
// IsSummer reports whether is summer.
func (c *Carbon) IsSummer() bool {
if c.IsInvalid() {
return false
}
month := c.Month()
if month == 6 || month == 7 || month == 8 {
return true
}
return false
}
// IsAutumn reports whether is autumn.
func (c *Carbon) IsAutumn() bool {
if c.IsInvalid() {
return false
}
month := c.Month()
if month == 9 || month == 10 || month == 11 {
return true
}
return false
}
// IsWinter reports whether is winter.
func (c *Carbon) IsWinter() bool {
if c.IsInvalid() {
return false
}
month := c.Month()
if month == 12 || month == 1 || month == 2 {
return true
}
return false
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/golang-package/carbon.git
git@gitee.com:golang-package/carbon.git
golang-package
carbon
carbon
master

搜索帮助