package common import ( "time" "mongo.games.com/goserver/core/timer" ) type WGDayTimeChange struct { LastMin int LastHour int LastDay int LastWeek int LastMonth int } func InSameDay(tNow, tPre time.Time) bool { if tPre.IsZero() { return true } if tNow.Day() != tPre.Day() { return false } if tNow.Sub(tPre) < time.Hour*24 { return true } return false } func InSameDayNoZero(tNow, tPre time.Time) bool { if tNow.Day() != tPre.Day() { return false } if tNow.Sub(tPre) < time.Hour*24 { return true } return false } func TsInSameDay(tsNow, tsPre int64) bool { tNow := time.Unix(tsNow, 0) tPre := time.Unix(tsPre, 0) return InSameDay(tNow, tPre) } func IsContinueDay(tNow, tPre time.Time) bool { if tPre.IsZero() { return true } tNext := tPre.AddDate(0, 0, 1) if InSameDay(tNow, tNext) { return true } return false } func InSameMonth(tNow, tPre time.Time) bool { if tPre.IsZero() { return true } if tNow.Month() != tPre.Month() { return false } if tNow.Year() == tPre.Year() { return true } return false } func InSameWeek(tNow, tPre time.Time) bool { if tPre.IsZero() { return true } if GetWeekStartTs(tNow.Unix()) == GetWeekStartTs(tPre.Unix()) { return true } return false } func TsInSameWeek(tsNow, tsPre int64) bool { tNow := time.Unix(tsNow, 0).Local() tPre := time.Unix(tsPre, 0).Local() return InSameWeek(tNow, tPre) } func DiffDay(tNow, tPre time.Time) int { // AddDate(0, 0, -1) 会显示差0天 y, m, d := tPre.Date() tStart := time.Date(y, m, d, 0, 0, 0, 0, tPre.Location()) return int(tNow.Sub(tStart) / (time.Hour * 24)) } func DiffDaybyTs(tNowts, tPrets int64) int { tNow := time.Unix(tNowts, 0) tPre := time.Unix(tPrets, 0) y, m, d := tPre.Date() tStart := time.Date(y, m, d, 0, 0, 0, 0, tPre.Location()) return int(tNow.Sub(tStart) / (time.Hour * 24)) } func DiffMonth(tNow, tPre time.Time) int { y1, m1, _ := tNow.Date() y2, m2, _ := tPre.Date() diffMonth := (y1-y2)*12 + (int(m1) - int(m2)) return int(diffMonth) } func DelayInvake(method func(), ud interface{}, delay time.Duration, times int) (timer.TimerHandle, bool) { return timer.StartTimer(timer.TimerActionWrapper(func(h timer.TimerHandle, ud interface{}) bool { if method != nil { method() } return true }), ud, delay, times) } func StrTimeToTs(strTime string) int64 { rTime, err := time.ParseInLocation("2006-01-02 15:04:05", strTime, time.Local) if err != nil { return 0 } return rTime.Unix() } func TsToStrTime(tc int64) string { //return time.Now().Format("2018-07-02 19:14:00") return time.Unix(tc, 0).Format("2006-01-02 15:04:05") } func TsToStrDateTime(tc int64) string { //return time.Now().Format("2018-07-02 19:14:00") return time.Unix(tc, 0).Format("2006-01-02") } func InTimeRange(beginHour, beginMinute, endHour, endMinute, checkHour, checkMinute int32) bool { beginTime := beginHour*100 + beginMinute endTime := endHour*100 + endMinute checkTime := checkHour*100 + checkMinute return beginTime <= checkTime && checkTime <= endTime } // GetWeekStartTs 获取本周开始的时间戳,周一为周的开始 func GetWeekStartTs(ts int64) int64 { now := time.Unix(ts, 0).Local() year, month, day := now.Date() today := time.Date(year, month, day, 0, 0, 0, 0, time.Local) n := today.Weekday() if today.Weekday() == 0 { n = 7 } st := today.AddDate(0, 0, -int(n-1)).Unix() return st } func GetDayStartTs(ts int64) int64 { now := time.Unix(ts, 0).Local() year, month, day := now.Date() today := time.Date(year, month, day, 0, 0, 0, 0, time.Local) return today.Unix() } func GetDayNextStartTs(ts int64) int64 { return GetDayStartTs(ts) + 86400 } func GetMonthTimestamp() []int64 { now := time.Now().Local() year, month, _ := now.Date() // 本月起始日期 st := time.Date(year, month, 1, 0, 0, 0, 0, time.Local).Unix() // 计算下个月的年和月 if month == time.December { year = year + 1 month = time.January } else { month = month + 1 } // 构建下个月的第一天的时间 et := time.Date(year, month, 1, 0, 0, 0, 0, now.Location()).Unix() return []int64{st, et} }