| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 | 
							- package util
 
- import (
 
- 	"fmt"
 
- 	"strconv"
 
- 	"strings"
 
- 	"time"
 
- )
 
- /*
 
- 时间常量
 
- */
 
- const (
 
- 	//定义每分钟的秒数
 
- 	SecondsPerMinute int64 = 60
 
- 	//定义每小时的秒数
 
- 	SecondsPerHour int64 = SecondsPerMinute * 60
 
- 	//定义每天的秒数
 
- 	SecondsPerDay int64 = SecondsPerHour * 24
 
- 	LayoutDateFormat = "2006-01-02"
 
- )
 
- /*
 
- 时间转换函数
 
- */
 
- func resolveTime(seconds int64) (day int64, hour int64, minute int64) {
 
- 	//每分钟秒数
 
- 	minute = seconds / SecondsPerMinute
 
- 	//每小时秒数
 
- 	hour = seconds / SecondsPerHour
 
- 	//每天秒数
 
- 	day = seconds / SecondsPerDay
 
- 	return
 
- }
 
- func TimeTransformation(unix int64) string {
 
- 	_, hour, _ := resolveTime(unix)
 
- 	now := ""
 
- 	if hour < 10 {
 
- 		now = fmt.Sprintf("0%d", hour)
 
- 	} else {
 
- 		now = fmt.Sprintf("%d", hour)
 
- 	}
 
- 	minute := (unix - (hour * SecondsPerHour)) / 60
 
- 	fmt.Println(minute, (hour * SecondsPerHour))
 
- 	if minute < 10 {
 
- 		now = fmt.Sprintf("%s:0%d", now, minute)
 
- 	} else {
 
- 		now = fmt.Sprintf("%s:%d", now, minute)
 
- 	}
 
- 	seconds := unix - (hour*SecondsPerHour + SecondsPerMinute*minute)
 
- 	if seconds < 10 {
 
- 		now = fmt.Sprintf("%s:0%d", now, seconds)
 
- 	} else {
 
- 		now = fmt.Sprintf("%s:%d", now, seconds)
 
- 	}
 
- 	return now
 
- }
 
- // TimeBetween 获取指定时间范围内天数
 
- // eg startTime => "2023-06-01" endTime => "2023-06-03"
 
- // return ["2023-06-01","2023-06-02","2023-06-03"]
 
- func TimeBetween(startTime, endTime string) []string {
 
- 	startDate, _ := time.ParseInLocation(LayoutDateFormat, startTime, time.Local)
 
- 	endDate, _ := time.ParseInLocation(LayoutDateFormat, endTime, time.Local)
 
- 	timeList := make([]string, 0)
 
- 	for endDate.After(startDate) {
 
- 		timeList = append(timeList, startDate.Format(LayoutDateFormat))
 
- 		startDate = startDate.AddDate(0, 0, 1)
 
- 	}
 
- 	timeList = append(timeList, endTime)
 
- 	return timeList
 
- }
 
- // TimeParseToMinutes 时间转换成分钟
 
- // durationStr := "00:02:30" => 150
 
- func TimeParseToMinutes(timeStr string) int {
 
- 	durationSlice := strings.Split(timeStr, ":")
 
- 	if len(durationSlice) < 3 {
 
- 		return 0
 
- 	}
 
- 	s1, _ := strconv.Atoi(durationSlice[0])
 
- 	s2, _ := strconv.Atoi(durationSlice[1])
 
- 	s3, _ := strconv.Atoi(durationSlice[2])
 
- 	newDurationStr := fmt.Sprintf("%dh%dm%ds", s1, s2, s3)
 
- 	duration, err := time.ParseDuration(newDurationStr)
 
- 	if err != nil {
 
- 		fmt.Println("无法解析时间间隔:", err)
 
- 		return 0
 
- 	}
 
- 	return int(duration.Seconds())
 
- }
 
- // Median 获取切片的中位值
 
- func Median(nums []int) float64 {
 
- 	n := len(nums)
 
- 	if n%2 == 0 {
 
- 		return float64(nums[n/2-1]+nums[n/2]) / float64(2)
 
- 	} else {
 
- 		return float64(nums[n/2])
 
- 	}
 
- }
 
 
  |