123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package assist
- import (
- "bytes"
- "fmt"
- "git.bvbej.com/bvbej/base-golang/pkg/time_parse"
- "math"
- "math/rand"
- "net"
- "regexp"
- "strconv"
- "strings"
- "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)
- }
- func IP4ToInt(ip net.IP) int {
- ipSplit := strings.Split(ip.To4().String(), ".")
- var ipInt = 0
- var pos uint = 24
- for _, ipSeg := range ipSplit {
- tempInt, _ := strconv.Atoi(ipSeg)
- tempInt = tempInt << pos
- ipInt = ipInt | tempInt
- pos -= 8
- }
- return ipInt
- }
- func IP4IntToString(ipInt int) string {
- ipSplit := make([]string, 4)
- var length = len(ipSplit)
- buffer := bytes.NewBufferString("")
- for i := 0; i < length; i++ {
- tempInt := ipInt & 0xFF
- ipSplit[length-i-1] = strconv.Itoa(tempInt)
- ipInt = ipInt >> 8
- }
- for i := 0; i < length; i++ {
- buffer.WriteString(ipSplit[i])
- if i < length-1 {
- buffer.WriteString(".")
- }
- }
- return buffer.String()
- }
|