You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.6 KiB

package comFunc
import (
"strconv"
"time"
)
var FirstSeason, _ = time.Parse("01-02", "01-01")
var SecondSeason, _ = time.Parse("01-02", "04-01")
var ThirdSeason, _ = time.Parse("01-02", "07-01")
var ForthSeason, _ = time.Parse("01-02", "10-01")
var seasons = []time.Time{ForthSeason, FirstSeason, SecondSeason, ThirdSeason, ForthSeason, FirstSeason}
// 获取对应季度
type Season struct {
Now time.Time // 当前时间
ThisSeason int8 // 当前季度
}
func NewSeason(now time.Time) Season {
s := Season{Now: now}
s.ThisSeason = s.GetThisSeason()
return s
}
func (ss *Season) GetThisSeason() int8 {
if ss.ThisSeason > 0 {
return ss.ThisSeason
}
if ss.Now.After(FirstSeason) {
ss.ThisSeason = 1
}
if ss.Now.After(SecondSeason) {
ss.ThisSeason = 2
}
if ss.Now.After(ThirdSeason) {
ss.ThisSeason = 3
}
if ss.Now.After(ForthSeason) {
ss.ThisSeason = 4
}
return ss.ThisSeason
}
// 获取下个季度的开始时间
func (ss *Season) GetNextSeasonStartTime() (date time.Time) {
s := ss.GetThisSeason()
date, _ = time.Parse("2006-01-02", strconv.Itoa(ss.Now.AddDate(0, 3, 0).Year())+"-"+seasons[s+1].Format("01-02"))
return
}
// 获取本季度的开始时间
func (ss *Season) GetThisSeasonStartTime() (date time.Time) {
s := ss.GetThisSeason()
date, _ = time.Parse("2006-01-02", strconv.Itoa(ss.Now.Year())+"-"+seasons[s].Format("01-02"))
return
}
// 获取上个季度的开始时间
func (ss *Season) GetPrevSeasonStartTime() (date time.Time) {
s := ss.GetThisSeason()
date, _ = time.Parse("2006-01-02", strconv.Itoa(ss.Now.AddDate(0, -3, 0).Year())+"-"+seasons[s+1].Format("01-02"))
return
}