tkcashgame_v4/pkg/utils/utils.go

110 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"crypto/md5"
"fmt"
"math/rand"
"strconv"
"time"
"github.com/google/uuid"
)
func BoolToInt32(value bool) int32 {
if value {
return 1
}
return 0
}
// Int32ToBool convert int32 to bool
func Int32ToBool(value int32) bool {
return value == 1
}
// ChangColumnIndexToAxis 将列索引转换为Excel列坐标
func ChangColumnIndexToAxis(intIndexX, intIndexY int) string {
var arr = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN"}
intIndexY = intIndexY + 1
resultY := ""
coloumnTotal := len(arr)
for {
if intIndexY <= coloumnTotal {
resultY = resultY + arr[intIndexY-1]
break
}
mo := intIndexY % coloumnTotal
resultY = arr[mo-1] + resultY
shang := intIndexY / coloumnTotal
if shang <= coloumnTotal {
resultY = arr[shang-1] + resultY
break
}
intIndexY = shang
}
return resultY + strconv.Itoa(intIndexX)
}
func IsDiffDay(last, now int64) bool {
lastYear, lastMonth, lastDay := time.Unix(last, 0).Date()
nowYear, nowMonth, nowDay := time.Unix(now, 0).Date()
if lastDay != nowDay || lastMonth != nowMonth || lastYear != nowYear {
return true
}
return false
}
func RandInterval(b1, b2 int32) int32 {
if b1 == b2 {
return b1
}
min, max := int64(b1), int64(b2)
if min > max {
min, max = max, min
}
return int32(rand.Int63n(max-min+1) + min)
}
// 获取utc时间t的days前负数或者后正数个的0点前时间
func ZeroDays(t int64, days int) int64 {
// t += int64(days-1) * 86400
t += int64(days) * 86400
year, month, day := time.Unix(t, 0).Date()
date := time.Date(year, month, day, 23, 59, 59, 0, time.Local)
return date.Unix()
}
func GetDateNum() uint32 {
date, _ := strconv.Atoi(time.Now().Format("20060102"))
return uint32(date)
}
func GetDateNum2(t int64) uint32 {
date, _ := strconv.Atoi(time.Unix(t, 0).Format("20060102"))
return uint32(date)
}
// 根据固定的字符串生成uuid
func GenerateUUID(specificString string) string {
hash := md5.Sum([]byte(specificString))
uniqueUUID := fmt.Sprintf("%x", hash)
parsedUUID, err := uuid.Parse(uniqueUUID)
if err != nil {
return "invalid UUID"
}
return parsedUUID.String()
}
// InSlice 判断字符串是否在切片中
func InSlice(str string, slice []string) bool {
for _, v := range slice {
if v == str {
return true
}
}
return false
}