-
Notifications
You must be signed in to change notification settings - Fork 0
/
gutil.go
72 lines (63 loc) · 1.56 KB
/
gutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package gutils
import (
"math/rand"
"time"
"crypto/md5"
"encoding/hex"
"strings"
"fmt"
)
func GetTimeNow() string{
var timeLayout = "2006-01-02 15:04:05.0000"
a:=time.Now().Format(timeLayout)
return a
}
func GetNowDateTime(pat string)string{
timer := time.Now()
var timeLayout = "2006-01-02 15:04:05"
switch(pat){
case "YYYY-MM-DD HH:mm:ss":return timer.Format(timeLayout);
case "YYYY-MM-DD": timeLayout = "2006-01-02";return timer.Format(timeLayout);
case "YYYY.MM.DD": timeLayout = "2006.01.02";return timer.Format(timeLayout);
case "HH:mm:ss": timeLayout = "15:04:05";return timer.Format(timeLayout);
default: return timer.Format(timeLayout);
}
}
func S2md5(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
func S2md5V2(str string) string {
data := []byte(str)
has := md5.Sum(data)
md5str := fmt.Sprintf("%x", has)
return md5str
}
func GetRandom(MIN int, MAX int) int {
rand1 := rand.New(rand.NewSource(time.Now().UnixNano()))
return MIN + rand1.Intn(MAX-MIN)
}
func GenCode(format string, cha int) string { //9,15,35
alphabet := "1234567890abcdefghijklmnopqrstuvwxyz"
FMT := "XXXXX-XXXXX-XXXXX"
CHA := 15
if format != "" {
FMT = format
}
if cha != 0 {
CHA = cha
}
c := ""
section := strings.Split(FMT, "-")
for _, v := range section {
for i := 0; i < len(v); i++ {
d := (GetRandom(0, CHA+1))
b := alphabet[d : d+1]
//fmt.Println(b)
c += b
}
c = c + "-"
}
return c[0 : len(c)-1]
}