utils.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package utils
  2. import (
  3. "bytes"
  4. "sort"
  5. "strconv"
  6. "time"
  7. "go-common/app/admin/ep/saga/conf"
  8. )
  9. // CalAverageTime 计算时间的平均/*分位 时间
  10. func CalAverageTime(timeType int, timeArray []float64) (result float64) {
  11. sort.Float64s(timeArray)
  12. if timeType == 0 {
  13. // 判断数量为零
  14. if len(timeArray) == 0 {
  15. result = 0
  16. } else {
  17. var sum float64
  18. for _, t := range timeArray {
  19. sum += t
  20. }
  21. result = sum / float64(len(timeArray))
  22. }
  23. } else if timeType > 0 && timeType < 11 {
  24. if len(timeArray) == 0 {
  25. result = 0
  26. } else {
  27. index := len(timeArray) * timeType / 10
  28. result = timeArray[index]
  29. }
  30. }
  31. return
  32. }
  33. // CalSizeTime ...
  34. func CalSizeTime(time, max, min float64) (float64, float64) {
  35. if max == 0 {
  36. max = time
  37. return max, min
  38. }
  39. if min == 0 {
  40. min = time
  41. return max, min
  42. }
  43. if time > max {
  44. max = time
  45. return max, min
  46. }
  47. if time != 0 && time < min {
  48. min = time
  49. return max, min
  50. }
  51. return max, min
  52. }
  53. // CalSyncTime ...
  54. func CalSyncTime() (since, until *time.Time) {
  55. syncDays := conf.Conf.Property.SyncData.DefaultSyncDays
  56. year, month, day := time.Now().Date()
  57. untilTime := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
  58. sinceTime := time.Date(year, month, day-syncDays, 0, 0, 0, 0, time.Local)
  59. since = &sinceTime
  60. until = &untilTime
  61. return
  62. }
  63. // CombineSlice ...
  64. func CombineSlice(s1, s2 []float64) []float64 {
  65. slice := make([]float64, len(s1)+len(s2))
  66. copy(slice, s1)
  67. copy(slice[len(s1):], s2)
  68. return slice
  69. }
  70. // InSlice ...
  71. func InSlice(key interface{}, list []string) bool {
  72. for _, item := range list {
  73. if key == item {
  74. return true
  75. }
  76. }
  77. return false
  78. }
  79. // Unicode2Chinese ...
  80. func Unicode2Chinese(str string) string {
  81. buf := bytes.NewBuffer(nil)
  82. i, j := 0, len(str)
  83. for i < j {
  84. x := i + 6
  85. if x > j {
  86. buf.WriteString(str[i:])
  87. break
  88. }
  89. if str[i] == '\\' && str[i+1] == 'u' {
  90. hex := str[i+2 : x]
  91. r, err := strconv.ParseUint(hex, 16, 64)
  92. if err == nil {
  93. buf.WriteRune(rune(r))
  94. } else {
  95. buf.WriteString(str[i:x])
  96. }
  97. i = x
  98. } else {
  99. buf.WriteByte(str[i])
  100. i++
  101. }
  102. }
  103. return buf.String()
  104. }