package utils
import (
"strings"
"time"
)
type TimeUtil struct {
}
//获取 nowTime(时间戳)月开始时间戳
func (t *TimeUtil) GetMonthBeginTimeStamp(nowTime int64) int64 {
thisTime := time.Unix(nowTime, 0).In(time.FixedZone("CST", 8*3600))
return time.Date(thisTime.Year(), thisTime.Month(), 1, 0, 0, 0, 0, thisTime.Location()).Unix()
}
//获取 nowTime(时间戳)月结束时间戳
func (t *TimeUtil) GetMonthEndTimeStamp(nowTime int64) int64 {
thisTime := time.Unix(nowTime, 0).In(time.FixedZone("CST", 8*3600))
return time.Date(thisTime.Year(), thisTime.Month()+1, 0, 23, 59, 59, 59, thisTime.Location()).Unix()
}
//获取 nowTime(时间戳)日开始时间戳
func (t *TimeUtil) GetDayBeginTimeStamp(nowTime int64) int64 {
thisTime := time.Unix(nowTime, 0).In(time.FixedZone("CST", 8*3600))
return time.Date(thisTime.Year(), thisTime.Month(), thisTime.Day(), 0, 0, 0, 0, thisTime.Location()).Unix()
}
//获取 nowTime(时间戳)日结束时间戳
func (t *TimeUtil) GetDayEndTimeStamp(nowTime int64) int64 {
thisTime := time.Unix(nowTime, 0).In(time.FixedZone("CST", 8*3600))
return time.Date(thisTime.Year(), thisTime.Month(), thisTime.Day(), 23, 59, 59, 59, thisTime.Location()).Unix()
}
//根据输入模板 "Y-m-d H:i:s" or "Y-m-d" 来进行时间转换为时间戳
func (t *TimeUtil) TransformationTimeStamp(toBeCharge string, temp string) int64 {
temp = strings.Replace(temp, " ", "", -1)
timeLayout := "2006-01-02 15:04:05"
if temp == "" || temp == "Y-m-d H:i:s" {
timeLayout = "2006-01-02 15:04:05"
} else if temp == "Y-m-d" {
timeLayout = "2006-01-02"
}
theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, time.FixedZone("CST", 8*3600)) //使用模板在对应时区转化为time.time类型
return theTime.Unix() //转化为时间戳 类型是int64
}
//根据输入模板 "Y-m-d H:i:s" or "Y-m-d" 来进行时间戳转换为时间
func (t *TimeUtil) TransformationTime(timeStamp int64, temp string) string {
temp = strings.Replace(temp, " ", "", -1)
timeLayout := "2006-01-02 15:04:05"
if temp == "" || temp == "Y-m-d H:i:s" {
timeLayout = "2006-01-02 15:04:05"
} else if temp == "Y-m-d" {
timeLayout = "2006-01-02"
}
thisTime := time.Unix(timeStamp, 0).In(time.FixedZone("CST", 8*3600))
return thisTime.Format(timeLayout)
}
//获取当前时间戳
func (t *TimeUtil) GetNowTimeStamp() int64 {
return time.Now().In(time.FixedZone("CST", 8*3600)).Unix()
}
//获取当前时间 (根据模板 "Y-m-d H:i:s" or "Y-m-d")
func (t *TimeUtil) GetNowTime(temp string) string {
timeStamp := time.Now().In(time.FixedZone("CST", 8*3600)).Unix()
temp = strings.Replace(temp, " ", "", -1)
timeLayout := "2006-01-02 15:04:05"
if temp == "" || temp == "Y-m-d H:i:s" {
timeLayout = "2006-01-02 15:04:05"
} else if temp == "Y-m-d" {
timeLayout = "2006-01-02"
}
thisTime := time.Unix(timeStamp, 0).In(time.FixedZone("CST", 8*3600))
return thisTime.Format(timeLayout)
}
//获取时间戳所在年
func (t *TimeUtil) GetYear(timeStamp int64) int {
thisTime := time.Unix(timeStamp, 0).In(time.FixedZone("CST", 8*3600))
return thisTime.Year()
}
//获取时间戳所在月
func (t *TimeUtil) GetMonth(timeStamp int64) int {
thisTime := time.Unix(timeStamp, 0).In(time.FixedZone("CST", 8*3600))
return int(thisTime.Month())
}
//获取时间戳所在日
func (t *TimeUtil) GetDay(timeStamp int64) int {
thisTime := time.Unix(timeStamp, 0).In(time.FixedZone("CST", 8*3600))
return thisTime.Day()
}