package lib import ( "fmt" "math" "math/rand" "regexp" "strings" "time" ) // #取得随机字符串:通过打乱slice来操作 func GetRandstring(length int, char string, rand_x int64) string { if length < 1 { return "" } if len(char) <= 6 || len(char) <= length { char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" } charArr := strings.Split(char, "") ran := rand.New(rand.NewSource(time.Now().UnixMilli() + rand_x)) l := len(charArr) for i := l - 1; i > 0; i-- { r := ran.Intn(i) charArr[r], charArr[i] = charArr[i], charArr[r] } rchar := charArr[:length] return strings.Join(rchar, "") } // 检查日期格式 func IsDateStr(date string) bool { if _, err := time.Parse("2006-01-02", date); err != nil { return false } return true } // 转化 func TimeStrToTime(T_time string) (time.Time, bool) { stamp, err := time.ParseInLocation("2006-01-02 15:04:05", T_time, time.Local) // +8 if err != nil { return time.Time{}, false } return stamp, true } func TimeStrToTime2(T_time string) (time.Time, bool) { stamp, err := time.ParseInLocation("2006-01-02 15:04:05", T_time, time.Local) // +8 if err != nil { return time.Time{}, false } return stamp, true } // 判断字符串是否为纯数字 func IsNumeric(str string) bool { // 定义一个正则表达式来匹配纯数字字符串 re := regexp.MustCompile(`^\d+$`) return re.MatchString(str) } // 计算两个时间字符串之间的分钟差 func MinutesDifference(startTime, endTime string) (float64, error) { layout := "2006-01-02 15:04:05" st, err := time.Parse(layout, startTime) if err != nil { return 0, fmt.Errorf("error parsing timeStr1: %w", err) } et, err := time.Parse(layout, endTime) if err != nil { return 0, fmt.Errorf("error parsing timeStr2: %w", err) } duration := et.Sub(st) minutes := math.Ceil(duration.Minutes()) return minutes, nil }