1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package lib
- import (
- "errors"
- "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 ReplaceSeconds(timeStr string) (string, error) {
- length := len(timeStr)
- if length < 2 {
- return "", errors.New("时间格式不正确")
- }
- lastTwoDigits := timeStr[length-2:]
- if lastTwoDigits == "00" {
- return timeStr, nil
- }
- t, err := time.Parse("2006-01-02 15:04:05", timeStr)
- if err != nil {
- return "", err
- }
- roundedTime := t.Truncate(time.Minute) // 将时间截断到分钟级别
- roundedTimeString := roundedTime.Format("2006-01-02 15:04:00")
- return roundedTimeString, nil
- }
- // 判断字符串是否为纯数字
- func IsNumeric(str string) bool {
- // 定义一个正则表达式来匹配纯数字字符串
- re := regexp.MustCompile(`^\d+$`)
- return re.MatchString(str)
- }
- func SubtractStringLists(list1, list2 []string) []string {
- // Create a map to store elements of list2 for fast lookup
- excludeMap := make(map[string]bool)
- for _, item := range list2 {
- excludeMap[item] = true
- }
- // Initialize the result slice with elements not present in list2
- var result []string
- for _, item := range list1 {
- if !excludeMap[item] {
- result = append(result, item)
- }
- }
- return result
- }
|