package otherutils import "time" // GetYearDay returns a formatted integer representing year and day number (format: YYYYDDD) func GetYearDay(tm *time.Time) int { year := tm.Year() day := tm.YearDay() return year*1000 + day } func GetYearDayByTs(ts int64) int { tm := time.Unix(ts, 0) return GetYearDay(&tm) } func GetYearWeek(tm *time.Time) int { y, w := tm.ISOWeek() return y*100 + w } func GetYearWeekByTs(ts int64) int { tm := time.Unix(ts, 0) return GetYearWeek(&tm) } func GetYearMonth(tm *time.Time) int { y, m, _ := tm.Date() return y*100 + int(m) } func GetYearMonthByTs(ts int64) int { tm := time.Unix(ts, 0) return GetYearMonth(&tm) } // 20141016 func GetYearMonthDay(tm *time.Time) int { y, m, d := tm.Date() return y*10000 + int(m)*100 + d } func IsSameDate(a *time.Time, b *time.Time) bool { a1 := GetYearMonthDay(a) b1 := GetYearMonthDay(b) return a1 == b1 } // IsSameDateByTs checks if two timestamps belong to the same date func IsSameDateByTs(a int64, b int64) bool { if a == b { return true } a1 := time.Unix(a, 0) b1 := time.Unix(b, 0) return IsSameDate(&a1, &b1) } // IsSameWeekByTs checks if two timestamps belong to the same ISO week func IsSameWeekByTs(a int64, b int64) bool { if a == b { return true } a1 := time.Unix(a, 0) b1 := time.Unix(b, 0) return GetYearWeek(&a1) == GetYearWeek(&b1) } // GetZeroTime 获取指定时间的0时 func GetZeroTime(t time.Time) int64 { t2 := t.Unix() _, offsetSeconds := t.Zone() seconds := (t2 + int64(offsetSeconds)) % 86400 return t2 - seconds } // DiffNatureDays 判断两个时间戳相差几天 func DiffNatureDays(t1, t2 int64) int { var secondsOfDay int64 = 86400 if t1 == t2 { return 0 } if t1 > t2 { t1, t2 = t2, t1 } diffDays := 0 secDiff := t2 - t1 if secDiff > secondsOfDay { tmpDays := int(secDiff / secondsOfDay) t1 += int64(tmpDays) * secondsOfDay diffDays += tmpDays } st := time.Unix(t1, 0) et := time.Unix(t2, 0) dateFormatTpl := "20060102" if st.Format(dateFormatTpl) != et.Format(dateFormatTpl) { diffDays += 1 } return diffDays } // GetWeekDay 时间是周几?范围1~7 func GetWeekDay(t time.Time) int { dayOfWeek := t.Weekday() // 将周日转换为7,其余依次加1 dayNumber := int(dayOfWeek) if dayNumber == 0 { // 周日 dayNumber = 7 } return dayNumber } // GetWhichDayOfPeriod 活动开始后第几天,包括结束日 func CalcWhichDayOfPeriod(startTime int64, endTime int64) int64 { n := CalcDaysBetweenTs(startTime, endTime) return n + 1 } // 计算从start日0点到endTime日0点,中间有几天,只计算日差值 func CalcDaysBetweenTime(startTime time.Time, endTime time.Time) int64 { endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, time.Local) startTime = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 0, 0, 0, time.Local) return int64(endTime.Sub(startTime).Hours() / 24) } // 计算从start日0点到endTime日0点,中间有几天,只计算日差值 func CalcDaysBetweenTs(startTime int64, endTime int64) int64 { t1 := time.Unix(startTime, 0) t2 := time.Unix(endTime, 0) return CalcDaysBetweenTime(t1, t2) } // 计算从start日0点到endTime日0点,中间有几天,只计算日差值 func CalcDaysByYmd(ymd1 int32, ymd2 int32) int32 { t1 := GetTimeByDateInt(ymd1) t2 := GetTimeByDateInt(ymd2) return int32(CalcDaysBetweenTime(t1, t2)) } func GetTimeByDateInt(date int32) time.Time { year := int(date / 10000) month := int(date / 100 % 100) day := int(date % 100) t := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local) return t } // func GetDateIntTimeStamp(timeStamp int64) int32 { // _time := time.Unix(timeStamp, 0) // return int32(_time.Year()*10000 + int(_time.Month())*100 + _time.Day()) // } // func SubDays(endTime, startTime time.Time) int64 { // endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, time.Local) // startTime = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 0, 0, 0, time.Local) // return int64(endTime.Sub(startTime).Hours() / 24) // } // 获取这一天0点时的时间戳 func GetNowDayTimestamp() int64 { now := time.Now() year, month, day := now.Date() midnight := time.Date(year, month, day, 0, 0, 0, 0, now.Location()) timestamp := midnight.Unix() return timestamp } // 获取指定周0点时间 func GetWeekZeroTime(timestamp int64) int64 { now := time.Unix(timestamp, 0) zeroTime := GetNowDayTimestamp() week := int64(now.Weekday()) if week == 0 { week = 7 } return zeroTime - ((week - 1) * 86400) }