libString.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package lib
  2. import (
  3. "math/rand"
  4. "regexp"
  5. "strings"
  6. "time"
  7. )
  8. // #取得随机字符串:通过打乱slice来操作
  9. func GetRandstring(length int, char string, rand_x int64) string {
  10. if length < 1 {
  11. return ""
  12. }
  13. if len(char) <= 6 || len(char) <= length {
  14. char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  15. }
  16. charArr := strings.Split(char, "")
  17. ran := rand.New(rand.NewSource(time.Now().UnixMilli() + rand_x))
  18. l := len(charArr)
  19. for i := l - 1; i > 0; i-- {
  20. r := ran.Intn(i)
  21. charArr[r], charArr[i] = charArr[i], charArr[r]
  22. }
  23. rchar := charArr[:length]
  24. return strings.Join(rchar, "")
  25. }
  26. // 检查日期格式
  27. func IsDateStr(date string) bool {
  28. if _, err := time.Parse("2006-01-02", date); err != nil {
  29. return false
  30. }
  31. return true
  32. }
  33. // 转化
  34. func TimeStrToTime(T_time string) (time.Time, bool) {
  35. stamp, err := time.ParseInLocation("2006-01-02 15:04:05", T_time, time.Local) // +8
  36. if err != nil {
  37. return time.Time{}, false
  38. }
  39. return stamp, true
  40. }
  41. func TimeStrToTime2(T_time string) (time.Time, bool) {
  42. stamp, err := time.ParseInLocation("2006-01-02 15:04:05", T_time, time.Local) // +8
  43. if err != nil {
  44. return time.Time{}, false
  45. }
  46. return stamp, true
  47. }
  48. // 判断字符串是否为纯数字
  49. func IsNumeric(str string) bool {
  50. // 定义一个正则表达式来匹配纯数字字符串
  51. re := regexp.MustCompile(`^\d+$`)
  52. return re.MatchString(str)
  53. }