package timez import ( "joybuywork/internal/consts" "joybuywork/internal/utils" "time" "github.com/golang-module/carbon/v2" ) var utc = map[int32]string{ -12: "UTC-12", -11: "UTC-11", -10: "UTC-10", -9: "UTC-9", -8: "UTC-8", -7: "UTC-7", -6: "UTC-6", -5: "UTC-5", -4: "UTC-4", -3: "UTC-3", -2: "UTC-2", -1: "UTC-1", 0: "UTC", 1: "UTC+1", 2: "UTC+2", 3: "UTC+3", 4: "UTC+4", 5: "UTC+5", 6: "UTC+6", 7: "UTC+7", 8: "UTC+8", 9: "UTC+9", 10: "UTC+10", 11: "UTC+11", 12: "UTC+12", } type Time struct { } const ( ShortDateOnly = "20060102" ListDate = "2006/01/02 15:04:05" // 列表使用的时间格式 PointYmdHi = "2006.01.02 15:04" // 点样式时间格式 ) // GMT 北京时间的时间戳变成GMT时间时间戳 func GMT(tt int64, tOffset int32) int64 { if tt == 0 { return 0 } return tt + int64((consts.CampaignTzOffset-tOffset)*3600) } // GMTInsideEndless 转换成GMT时间做区间比较,如果为结束为0代表正无穷 func GMTInsideEndless(tOffset int32, start, end int64, vals ...int64) bool { var val = time.Now().Unix() if len(vals) > 0 { val = vals[0] } start, end = GMT(start, tOffset), GMT(end, tOffset) if end == 0 { return val >= start } return val >= start && val <= end } // STZ GMT时间的时间戳变成北京时间时间戳 func STZ(tt int64, tOffset int32) int64 { if tt == 0 { return 0 } return tt + int64((tOffset-consts.CampaignTzOffset)*3600) } // GMTCarbon GMT的carbon组件 func GMTCarbon(tOffset int32) carbon.Carbon { return carbon.SetLocation(Location(tOffset)) } // GMTTime GMT的time组件 func GMTTime(tOffset int32, sec ...int64) time.Time { if len(sec) > 0 && sec[0] > 0 { return time.Unix(sec[0], 0).In(Location(tOffset)) } return time.Now().In(Location(tOffset)) } // GMTTimeStart GMT的time组件变成那一天的开始 func GMTTimeStart(tOffset int32, sec ...int64) (time.Time, error) { t := GMTTime(tOffset, sec...) // 当地是哪一天的 day := t.Format(time.DateOnly) // 变成当天的开始 return time.ParseInLocation(time.DateOnly, day, Location(tOffset)) } // GMTStartAndEndTs 基于时区的开始和结束 如:2024-10-19 00:00:00 - 2024-10-19 23:59:59 func GMTStartAndEndTs(tOffset int32, sec ...int64) (day string, start, end int64) { st, _ := GMTTimeStart(tOffset, sec...) return st.Format(time.DateOnly), st.Unix(), st.Unix() + 86399 } // GetWeekEnd 获取某个节点一周的开始和结束 如:2024-10-19 00:00:00 - 2024-10-26 23:59:59 func GetWeekEnd(tOffset, weekNum int32) (start, end int64) { t, _ := GMTTimeStart(tOffset) w := utils.Ternary[int32](t.Weekday() == 0, 7, int32(t.Weekday())) var ws int64 = 7 * 86400 if w < weekNum { end = t.Unix() + int64((weekNum-w)*86400) } else { end = t.Unix() + int64((7-(w-weekNum))*86400) } return end - ws, end - 1 } // Location 活动时区 func Location(tOffset int32) *time.Location { return time.FixedZone(utc[tOffset], int(tOffset)*3600) }