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()
}