GO 操作时间

package timer

import (
"fmt"
"time"
)

var (
BackOffset = -1 * time.Millisecond
)

type TimeRange struct {
Start time.Time
End time.Time
}

func (s TimeRange) String() string {
out := fmt.Sprintf("[%s, %s]", s.Start, s.End)
return out
}

func (s *TimeRange) ToUnix() *TimeRangeUnix {
out := &TimeRangeUnix{}
out.Start = s.Start.Unix()
out.End = s.End.Unix()
return out
}

type TimeRangeUnix struct {
Start int64
End int64
}

// GetDayRange 1945-10-10 12:12:12 --> [1945-10-10 00:00:00, 1945-10-10 23:59:59]
func GetDayRange(t time.Time) *TimeRange {
tiStart := GetDayStart(t)
tiEnd := tiStart.AddDate(0, 0, 1).Add(BackOffset)
tr := &TimeRange{
tiStart, tiEnd,
}
return tr
}

// GetDayStart 1945-10-10 12:12:12 --> 1945-10-10 00:00:00
func GetDayStart(t time.Time) time.Time {
tiStart := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
return tiStart
}

// GetWeekRange 1945-10-10 12:12:12 --> [1945-10-08 00:00:00, 1945-10-14 23:59:59]
func GetWeekRange(in time.Time) *TimeRange {
weekDay := int(in.Weekday())
dayFix := 1 // monday is the first day of week
weekDay -= dayFix
dayStart := GetDayStart(in)
tiStart := dayStart.AddDate(0, 0, -1*weekDay)
tiEnd := tiStart.AddDate(0, 0, 7).Add(BackOffset)
tr := &TimeRange{
tiStart, tiEnd,
}
return tr
}

// GetMonthRange 获取上个月时间
func GetMonthRange(ti time.Time) *TimeRange {
today := GetDayStart(ti)
dayOffset := ti.Day()*-1 + 1
start := today.AddDate(0, -1, dayOffset)
end := start.AddDate(0, 1, 0).Add(BackOffset)
tr := TimeRange{
start, end,
}
return &tr
}

// GetMonthRangeByCondit 获取前几个月时间
func GetMonthRangeByCondit(ti time.Time, n int) *TimeRange {
today := GetDayStart(ti)
dayOffset := ti.Day()*-1 + 1
start := today.AddDate(0, -n, dayOffset)
end := start.AddDate(0, n, 0).Add(BackOffset)
tr := TimeRange{
start, end,
}
return &tr
}

// GetThisMonthRangeByCondit 获取前几个月时间
func GetThisMonthRangeByCondit(ti time.Time, n1 int, n2 int) *TimeRange {
today := GetDayStart(ti)
dayOffset := ti.Day()*-1 + 1
start := today.AddDate(0, -n1, dayOffset)
end := start.AddDate(0, n2, 0).Add(BackOffset)
tr := TimeRange{
start, end,
}
return &tr
}

// GetLastMonthRange 获取上一个月
func GetLastMonthRange() *TimeRange {
ti := time.Now().AddDate(0, -1, 0)
return GetMonthRange(ti)
}

// GetYearRange 1945-10-10 12:12:12 --> [1945-01-01 00:00:00, 1945-12-31 23:59:59]
func GetYearRange(ti time.Time) *TimeRange {
st := time.Date(ti.Year(), time.January, 1, 0, 0, 0, 0, ti.Location())
end := st.AddDate(1, 0, 0).Add(BackOffset)
tr := TimeRange{
st, end,
}
return &tr
}

// WeekIntervalTime 获取本周、上周、下一周的时间
func WeekIntervalTime(week int) (int64, int64) {
now := time.Now()
offset := int(time.Monday - now.Weekday())
if offset > 0 {
offset = -6
}
year, month, day := now.Date()
thisWeek := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
startTime := thisWeek.AddDate(0, 0, offset+7*week).Format("2006-01-02") + " 00:00:00"
endTime := thisWeek.AddDate(0, 0, offset+6+7*week).Format("2006-01-02") + " 23:59:59"
return Str2Stamp(startTime), Str2Stamp(endTime)
}

func Str2Time(formatStr string) time.Time {
timeLayout := "2006-01-02 15:04:05"
loc, _ := time.LoadLocation("Local")
theTime, _ := time.ParseInLocation(timeLayout, formatStr, loc)
return theTime
}
func Str2Stamp(formatTimeStr string) int64 {
timeStruct := Str2Time(formatTimeStr)
millisecond := timeStruct.UnixNano() / 1e6
return millisecond
}
func Stamp2Str(stamp int64) string {
timeLayout := "2006-01-02"
str := time.Unix(stamp/1000, 0).Format(timeLayout)
return str
}

// TimeAreaTrans .
func TimeAreaTrans(timeType int, inputTime int64) string {
var timeLoation *time.Location
//区域切换
if timeType == 1 {
timeLoation = time.FixedZone("UTC/GMT", -3*3600)
} else if timeType == 2 {
timeLoation = time.FixedZone("CST", 8*3600)
} else {
timeLoation = time.FixedZone("CST", 8*3600)
}
//时间戳转日期,加时区
now := inputTime / 1000
transDate := time.Unix(now, 0).In(timeLoation).Format("2006-01-02 15:04:05")
return transDate
}
func StampStrOther(stamp int64) string {
timeLayout := "20060102"
str := time.Unix(stamp/1000, 0).Format(timeLayout)
return str
}
posted @ 2023-01-29 16:24  lisus2000  阅读(23)  评论(0编辑  收藏  举报