-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
93 lines (85 loc) · 1.9 KB
/
utils.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package goapi
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
func toPtr[T any](v T) *T {
return &v
}
func isFixedType(fType reflect.Type) bool {
if fType == typeFile || fType == typeFiles || fType == typeCookie {
return true
}
return false
}
func inArray[T comparable](val T, list []T) bool {
for _, v := range list {
if val == v {
return true
}
}
return false
}
func timeFormat(date time.Time, format ...string) string {
if date.IsZero() {
return ""
}
str := "Y-m-d H:i:s"
if len(format) > 0 {
str = format[0]
}
year := strconv.Itoa(date.Year())
month := fmt.Sprintf("%d", date.Month())
day := strconv.Itoa(date.Day())
hour := strconv.Itoa(date.Hour())
minute := strconv.Itoa(date.Minute())
second := strconv.Itoa(date.Second())
week := date.Weekday().String()
weekMap := map[string]string{
"Monday": "1",
"Tuesday": "2",
"Wednesday": "3",
"Thursday": "4",
"Friday": "5",
"Saturday": "6",
"Sunday": "7",
}
str = strings.ReplaceAll(str, "Y", year)
str = strings.ReplaceAll(str, "m", zeroFill(month, 2))
str = strings.ReplaceAll(str, "d", zeroFill(day, 2))
str = strings.ReplaceAll(str, "H", zeroFill(hour, 2))
str = strings.ReplaceAll(str, "i", zeroFill(minute, 2))
str = strings.ReplaceAll(str, "s", zeroFill(second, 2))
str = strings.ReplaceAll(str, "w", weekMap[week])
str = strings.ReplaceAll(str, "W", week)
return str
}
func zeroFill(input string, num int) string {
zeroNum := num - len(input)
rs := ""
for i := 0; i < zeroNum; i++ {
rs += "0"
}
return rs + input
}
func spanFill(input string, inputLen, num int) string {
zeroNum := num - inputLen
for i := 0; i < zeroNum; i++ {
input += " "
}
return input
}
func isDefaultLogger(log Logger) (ok bool) {
var levelLog *levelHandleLogger
if levelLog, ok = log.(*levelHandleLogger); !ok {
return
}
if levelLog.log == nil {
return
}
_, ok = levelLog.log.(*defaultLogger)
return
}