tool.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package assist
  2. import (
  3. "bytes"
  4. "fmt"
  5. "git.bvbej.com/bvbej/base-golang/pkg/time_parse"
  6. "math"
  7. "math/rand"
  8. "net"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "unicode"
  14. )
  15. func GetOrderNumber() string {
  16. parse, _ := time.Parse(time_parse.CSTLayout, "2021-04-27 00:00:00")
  17. hours := time.Now().Sub(parse).Hours()
  18. day := math.Floor(hours / 24)
  19. prefix := fmt.Sprintf("%05d", int64(day))
  20. format := time.Now().Format("") + "%0" + strconv.Itoa(10) + "d"
  21. n := math.Pow10(10)
  22. return prefix + fmt.Sprintf(format, rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(int64(n)))
  23. }
  24. func ByteFmt(size int64) string {
  25. var unitArr = []string{"B", "KB", "MB", "GB", "TB", "EB"}
  26. if size == 0 {
  27. return "unknown"
  28. }
  29. fs := float64(size)
  30. p := int(math.Log(fs) / math.Log(1024))
  31. val := fs / math.Pow(1024, float64(p))
  32. _, frac := math.Modf(val)
  33. if frac > 0 {
  34. return fmt.Sprintf("%.1f%s", math.Floor(val*10)/10, unitArr[p])
  35. } else {
  36. return fmt.Sprintf("%d%s", int(val), unitArr[p])
  37. }
  38. }
  39. func UniqueArray[T comparable](array []T) []T {
  40. result := make([]T, 0, len(array))
  41. temp := map[T]struct{}{}
  42. for _, item := range array {
  43. if _, ok := temp[item]; !ok {
  44. temp[item] = struct{}{}
  45. result = append(result, item)
  46. }
  47. }
  48. return result
  49. }
  50. func InArray[T comparable](item T, array []T) bool {
  51. for _, s := range array {
  52. if item == s {
  53. return true
  54. }
  55. }
  56. return false
  57. }
  58. func CurrencyDecimal(value float64) float64 {
  59. value, _ = strconv.ParseFloat(fmt.Sprintf("%.8f", value), 64)
  60. return value
  61. }
  62. func IsChinese(str string) bool {
  63. var count int
  64. for _, v := range str {
  65. if unicode.Is(unicode.Han, v) {
  66. count++
  67. break
  68. }
  69. }
  70. return count > 0
  71. }
  72. func RandInt(max int) int {
  73. if max <= 0 {
  74. return 0
  75. }
  76. rand.Seed(time.Now().UnixNano())
  77. <-time.After(time.Nanosecond)
  78. return rand.Intn(max)
  79. }
  80. func FindIPv4(input string) string {
  81. partIp := "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"
  82. must := partIp + "\\." + partIp + "\\." + partIp + "\\." + partIp
  83. matchMe := regexp.MustCompile(must)
  84. return matchMe.FindString(input)
  85. }
  86. func RandString(n int) string {
  87. var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  88. b := make([]rune, n)
  89. for i := range b {
  90. b[i] = letterRunes[RandInt(len(letterRunes))]
  91. }
  92. return string(b)
  93. }
  94. func IP4ToInt(ip net.IP) int {
  95. ipSplit := strings.Split(ip.To4().String(), ".")
  96. var ipInt = 0
  97. var pos uint = 24
  98. for _, ipSeg := range ipSplit {
  99. tempInt, _ := strconv.Atoi(ipSeg)
  100. tempInt = tempInt << pos
  101. ipInt = ipInt | tempInt
  102. pos -= 8
  103. }
  104. return ipInt
  105. }
  106. func IP4IntToString(ipInt int) string {
  107. ipSplit := make([]string, 4)
  108. var length = len(ipSplit)
  109. buffer := bytes.NewBufferString("")
  110. for i := 0; i < length; i++ {
  111. tempInt := ipInt & 0xFF
  112. ipSplit[length-i-1] = strconv.Itoa(tempInt)
  113. ipInt = ipInt >> 8
  114. }
  115. for i := 0; i < length; i++ {
  116. buffer.WriteString(ipSplit[i])
  117. if i < length-1 {
  118. buffer.WriteString(".")
  119. }
  120. }
  121. return buffer.String()
  122. }