1 Star 0 Fork 64

ajunlonglive/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
season.go 2.58 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2021-09-16 09:52 . update comment
package carbon
import (
"strings"
"time"
)
// Season gets season name according to the meteorological division method, i18n is supported.
// 获取当前季节(以气象划分),支持i18n
func (c Carbon) Season() string {
if c.IsInvalid() {
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
index := -1
switch {
case c.Month() == 3 || c.Month() == 4 || c.Month() == 5:
index = 0
case c.Month() == 6 || c.Month() == 7 || c.Month() == 8:
index = 1
case c.Month() == 9 || c.Month() == 10 || c.Month() == 11:
index = 2
case c.Month() == 12 || c.Month() == 1 || c.Month() == 2:
index = 3
}
if seasons, ok := c.lang.resources["seasons"]; ok {
slice := strings.Split(seasons, "|")
if len(slice) == 4 {
return slice[index]
}
}
return ""
}
// StartOfSeason returns a Carbon instance for start of the season.
// 本季节开始时间
func (c Carbon) StartOfSeason() Carbon {
if c.IsInvalid() {
return c
}
if c.Month() == 1 || c.Month() == 2 {
c.time = time.Date(c.Year()-1, time.Month(12), 1, 0, 0, 0, 0, c.loc)
return c
}
c.time = time.Date(c.Year(), time.Month(c.Month()/3*3), 1, 0, 0, 0, 0, c.loc)
return c
}
// EndOfSeason returns a Carbon instance for end of the season.
// 本季节结束时间
func (c Carbon) EndOfSeason() Carbon {
if c.IsInvalid() {
return c
}
if c.Month() == 1 || c.Month() == 2 {
c.time = time.Date(c.Year(), time.Month(2), 1, 23, 59, 59, 999999999, c.loc).AddDate(0, 1, -1)
return c
}
if c.Month() == 12 {
c.time = time.Date(c.Year()+1, time.Month(2), 1, 23, 59, 59, 999999999, c.loc).AddDate(0, 1, -1)
return c
}
c.time = time.Date(c.Year(), time.Month(c.Month()/3*3+2), 1, 23, 59, 59, 999999999, c.loc).AddDate(0, 1, -1)
return c
}
// IsSpring reports whether is spring.
// 是否是春季
func (c Carbon) IsSpring() bool {
if c.IsInvalid() {
return false
}
if c.Month() == 3 || c.Month() == 4 || c.Month() == 5 {
return true
}
return false
}
// IsSummer reports whether is summer.
// 是否是夏季
func (c Carbon) IsSummer() bool {
if c.IsInvalid() {
return false
}
if c.Month() == 6 || c.Month() == 7 || c.Month() == 8 {
return true
}
return false
}
// IsAutumn reports whether is autumn.
// 是否是秋季
func (c Carbon) IsAutumn() bool {
if c.IsInvalid() {
return false
}
if c.Month() == 9 || c.Month() == 10 || c.Month() == 11 {
return true
}
return false
}
// IsWinter reports whether is winter.
// 是否是冬季
func (c Carbon) IsWinter() bool {
if c.IsInvalid() {
return false
}
if c.Month() == 12 || c.Month() == 1 || c.Month() == 2 {
return true
}
return false
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ajunlonglive/carbon.git
git@gitee.com:ajunlonglive/carbon.git
ajunlonglive
carbon
carbon
v2.0.1

搜索帮助

0d507c66 1850385 C8b1a773 1850385