-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathutil.go
71 lines (61 loc) · 1.35 KB
/
util.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
package util
import (
"strconv"
"strings"
"time"
"gopkg.in/errgo.v2/fmt/errors"
)
var dateFormats = []string{
"2006-01-02T15:04:05.00000Z",
"2006-01-02T15:04:05.000Z",
"2006-01-02T15:04:05.000",
"2006-01-02T15:04:05Z",
"2006-01-02T15:04:05",
"2006-01-02T15:04",
"2006-01-02",
}
func ParseTimestamp(timestamp string) (time.Time, error) {
if timeMs, err := strconv.ParseInt(timestamp, 10, 64); err == nil {
return time.UnixMilli(timeMs), nil
}
loc, _ := time.LoadLocation("Local")
for _, format := range dateFormats {
if val, e := time.ParseInLocation(format, timestamp, loc); e == nil {
return val, nil
}
}
return time.Time{}, errors.Newf("unable to parse timestamp: %s", timestamp)
}
func ConvertControlChars(value string) string {
value = strings.Replace(value, "\\n", "\n", -1)
value = strings.Replace(value, "\\r", "\r", -1)
value = strings.Replace(value, "\\t", "\t", -1)
return value
}
func ContainsString(list []string, element string) bool {
for _, it := range list {
if it == element {
return true
}
}
return false
}
func ContainsInt32(list []int32, element int32) bool {
for _, it := range list {
if it == element {
return true
}
}
return false
}
func StringArraysEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}