time.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package util
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. /*
  9. 时间常量
  10. */
  11. const (
  12. //定义每分钟的秒数
  13. SecondsPerMinute int64 = 60
  14. //定义每小时的秒数
  15. SecondsPerHour int64 = SecondsPerMinute * 60
  16. //定义每天的秒数
  17. SecondsPerDay int64 = SecondsPerHour * 24
  18. LayoutDateFormat = "2006-01-02"
  19. )
  20. /*
  21. 时间转换函数
  22. */
  23. func resolveTime(seconds int64) (day int64, hour int64, minute int64) {
  24. //每分钟秒数
  25. minute = seconds / SecondsPerMinute
  26. //每小时秒数
  27. hour = seconds / SecondsPerHour
  28. //每天秒数
  29. day = seconds / SecondsPerDay
  30. return
  31. }
  32. func TimeTransformation(unix int64) string {
  33. _, hour, _ := resolveTime(unix)
  34. now := ""
  35. if hour < 10 {
  36. now = fmt.Sprintf("0%d", hour)
  37. } else {
  38. now = fmt.Sprintf("%d", hour)
  39. }
  40. minute := (unix - (hour * SecondsPerHour)) / 60
  41. fmt.Println(minute, (hour * SecondsPerHour))
  42. if minute < 10 {
  43. now = fmt.Sprintf("%s:0%d", now, minute)
  44. } else {
  45. now = fmt.Sprintf("%s:%d", now, minute)
  46. }
  47. seconds := unix - (hour*SecondsPerHour + SecondsPerMinute*minute)
  48. if seconds < 10 {
  49. now = fmt.Sprintf("%s:0%d", now, seconds)
  50. } else {
  51. now = fmt.Sprintf("%s:%d", now, seconds)
  52. }
  53. return now
  54. }
  55. // TimeBetween 获取指定时间范围内天数
  56. // eg startTime => "2023-06-01" endTime => "2023-06-03"
  57. // return ["2023-06-01","2023-06-02","2023-06-03"]
  58. func TimeBetween(startTime, endTime string) []string {
  59. startDate, _ := time.ParseInLocation(LayoutDateFormat, startTime, time.Local)
  60. endDate, _ := time.ParseInLocation(LayoutDateFormat, endTime, time.Local)
  61. timeList := make([]string, 0)
  62. for endDate.After(startDate) {
  63. timeList = append(timeList, startDate.Format(LayoutDateFormat))
  64. startDate = startDate.AddDate(0, 0, 1)
  65. }
  66. timeList = append(timeList, endTime)
  67. return timeList
  68. }
  69. // TimeParseToMinutes 时间转换成分钟
  70. // durationStr := "00:02:30" => 150
  71. func TimeParseToMinutes(timeStr string) int {
  72. durationSlice := strings.Split(timeStr, ":")
  73. if len(durationSlice) < 3 {
  74. return 0
  75. }
  76. s1, _ := strconv.Atoi(durationSlice[0])
  77. s2, _ := strconv.Atoi(durationSlice[1])
  78. s3, _ := strconv.Atoi(durationSlice[2])
  79. newDurationStr := fmt.Sprintf("%dh%dm%ds", s1, s2, s3)
  80. duration, err := time.ParseDuration(newDurationStr)
  81. if err != nil {
  82. fmt.Println("无法解析时间间隔:", err)
  83. return 0
  84. }
  85. return int(duration.Seconds())
  86. }
  87. // Median 获取切片的中位值
  88. func Median(nums []int) float64 {
  89. n := len(nums)
  90. if n%2 == 0 {
  91. return float64(nums[n/2-1]+nums[n/2]) / float64(2)
  92. } else {
  93. return float64(nums[n/2])
  94. }
  95. }