libString.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package lib
  2. import (
  3. "math/rand"
  4. "strings"
  5. "time"
  6. )
  7. // #取得随机字符串:通过打乱slice来操作
  8. func GetRandstring(length int, char string, rand_x int64) string {
  9. if length < 1 {
  10. return ""
  11. }
  12. if len(char) <= 6 || len(char) <= length {
  13. char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  14. }
  15. charArr := strings.Split(char, "")
  16. ran := rand.New(rand.NewSource(time.Now().UnixMilli() + rand_x))
  17. l := len(charArr)
  18. for i := l - 1; i > 0; i-- {
  19. r := ran.Intn(i)
  20. charArr[r], charArr[i] = charArr[i], charArr[r]
  21. }
  22. rchar := charArr[:length]
  23. return strings.Join(rchar, "")
  24. }
  25. // 检查日期格式
  26. func IsDateStr(date string) bool {
  27. if _, err := time.Parse("2006-01-02", date); err != nil {
  28. return false
  29. }
  30. return true
  31. }
  32. // 转化
  33. func TimeStrToTime(T_time string) (time.Time, bool) {
  34. stamp, err := time.ParseInLocation("2006-01-02 15:04:05", T_time, time.Local) // +8
  35. if err != nil {
  36. return time.Time{}, false
  37. }
  38. return stamp, true
  39. }
  40. func TimeStrToTime2(T_time string) (time.Time, bool) {
  41. stamp, err := time.ParseInLocation("2006-01-02 15:04:05", T_time, time.Local) // +8
  42. if err != nil {
  43. return time.Time{}, false
  44. }
  45. return stamp, true
  46. }