tool.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package tool
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "fmt"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. const (
  12. Layout = "2006-01-02 15:04:05"
  13. DateTime = "2006-01-02"
  14. DefaultExecTime = "0_0_0"
  15. )
  16. // StringToTimeUnix 时间字符串转换成时间戳
  17. // a.g 6_2_3 ===> 转换成 6天2小时3分钟后的时间戳
  18. // TODO 需要优化代码写得太死,需要优化后兼容秒的场景
  19. func StringToTimeUnix(execTimeStr string) int64 {
  20. var processAt = time.Now().Unix()
  21. if execTimeStr == DefaultExecTime {
  22. return processAt
  23. }
  24. execTime := strings.Split(execTimeStr, "_")
  25. if len(execTime) < 3 {
  26. return processAt
  27. }
  28. // 天数
  29. days, _ := strconv.Atoi(execTime[0])
  30. processAt += int64(days) * 24 * 60 * 60
  31. // 小时
  32. hours, _ := strconv.Atoi(execTime[1])
  33. processAt += int64(hours) * 60 * 60
  34. // 分钟
  35. minutes, _ := strconv.Atoi(execTime[2])
  36. processAt += int64(minutes) * 60
  37. return processAt
  38. }
  39. func GetLocalTime(timeStr string) time.Time {
  40. execTime, _ := time.ParseInLocation(Layout, timeStr, time.Local)
  41. return execTime
  42. }
  43. // TimeParseLocalUnix 获取当天零点的时间戳
  44. // eg 2023-02-22 => 1676995200
  45. func TimeParseLocalUnix(DayTime string) int64 {
  46. value := DayTime
  47. if len(DayTime) <= 11 {
  48. value = fmt.Sprintf("%s 00:00:00", DayTime)
  49. }
  50. loc, _ := time.LoadLocation("Local")
  51. theTime, _ := time.ParseInLocation(Layout, value, loc)
  52. return theTime.Unix()
  53. }
  54. func Md5String(input string) string {
  55. s := md5.New()
  56. digest := strings.ReplaceAll(input, "\n", "")
  57. s.Write([]byte(digest))
  58. return hex.EncodeToString(s.Sum(nil))
  59. }
  60. // TimeBetween 获取指定时间范围内天数
  61. // eg startTime => "2023-06-01" endTime => "2023-06-03"
  62. // return ["2023-06-01","2023-06-02","2023-06-03"]
  63. func TimeBetween(startTime, endTime string) []string {
  64. startDate, _ := time.ParseInLocation("2006-01-02", startTime, time.Local)
  65. endDate, _ := time.ParseInLocation("2006-01-02", endTime, time.Local)
  66. timeList := make([]string, 0)
  67. for endDate.After(startDate) {
  68. timeList = append(timeList, startDate.Format("2006-01-02"))
  69. startDate = startDate.AddDate(0, 0, 1)
  70. }
  71. timeList = append(timeList, endTime)
  72. return timeList
  73. }
  74. // Median 获取切片的中位值
  75. func Median(nums []float64) float64 {
  76. sort.Float64s(nums)
  77. n := len(nums)
  78. if n%2 == 0 {
  79. return (nums[n/2-1] + nums[n/2]) / float64(2)
  80. } else {
  81. return nums[n/2]
  82. }
  83. }
  84. // MedianInt64 获取切片的中位值
  85. func MedianInt64(nums []int64) int64 {
  86. sort.Slice(nums, func(i, j int) bool {
  87. return nums[i] < nums[j]
  88. })
  89. n := len(nums)
  90. if n%2 == 0 {
  91. return (nums[n/2-1] + nums[n/2]) / 2
  92. } else {
  93. return nums[n/2]
  94. }
  95. }
  96. // TimeSub 计算两个时间的差值
  97. // eg startTime => "2022-05-01 09:07:08"
  98. // eg endTime => "00:06:35"
  99. // eg return => 2022-05-01 09:00:33
  100. func TimeSub(startTime, endTime string) string {
  101. str := strings.Split(endTime, ":")
  102. if len(str) < 3 {
  103. return ""
  104. }
  105. durationStr := fmt.Sprintf("%sh%sm%ss", str[0], str[1], str[2])
  106. timeValue, err := time.Parse(time.RFC3339, startTime)
  107. if err != nil {
  108. fmt.Println("解析时间出错:", err)
  109. return ""
  110. }
  111. // 解析持续时间字符串为 time.Duration 类型
  112. durationValue, err := time.ParseDuration(durationStr)
  113. if err != nil {
  114. fmt.Println("解析持续时间出错:", err)
  115. return ""
  116. }
  117. // 计算时间之间的差异
  118. return timeValue.Add(-durationValue).Format(Layout)
  119. }