package assist import ( "fmt" "git.bvbej.com/bvbej/base-golang/pkg/time_parse" "math" "math/rand" "regexp" "strconv" "time" "unicode" ) func GetOrderNumber() string { parse, _ := time.Parse(time_parse.CSTLayout, "2021-04-27 00:00:00") hours := time.Now().Sub(parse).Hours() day := math.Floor(hours / 24) prefix := fmt.Sprintf("%05d", int64(day)) format := time.Now().Format("") + "%0" + strconv.Itoa(10) + "d" n := math.Pow10(10) return prefix + fmt.Sprintf(format, rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(int64(n))) } func ByteFmt(size int64) string { var unitArr = []string{"B", "KB", "MB", "GB", "TB", "EB"} if size == 0 { return "unknown" } fs := float64(size) p := int(math.Log(fs) / math.Log(1024)) val := fs / math.Pow(1024, float64(p)) _, frac := math.Modf(val) if frac > 0 { return fmt.Sprintf("%.1f%s", math.Floor(val*10)/10, unitArr[p]) } else { return fmt.Sprintf("%d%s", int(val), unitArr[p]) } } func UniqueArray[T comparable](array []T) []T { result := make([]T, 0, len(array)) temp := map[T]struct{}{} for _, item := range array { if _, ok := temp[item]; !ok { temp[item] = struct{}{} result = append(result, item) } } return result } func InArray[T comparable](item T, array []T) bool { for _, s := range array { if item == s { return true } } return false } func CurrencyDecimal(value float64) float64 { value, _ = strconv.ParseFloat(fmt.Sprintf("%.8f", value), 64) return value } func IsChinese(str string) bool { var count int for _, v := range str { if unicode.Is(unicode.Han, v) { count++ break } } return count > 0 } func RandInt(max int) int { if max <= 0 { return 0 } rand.Seed(time.Now().UnixNano()) <-time.After(time.Nanosecond) return rand.Intn(max) } func FindIPv4(input string) string { partIp := "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])" must := partIp + "\\." + partIp + "\\." + partIp + "\\." + partIp matchMe := regexp.MustCompile(must) return matchMe.FindString(input) } func RandString(n int) string { var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") b := make([]rune, n) for i := range b { b[i] = letterRunes[RandInt(len(letterRunes))] } return string(b) }