forked from OneOfOne/otk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
other.go
79 lines (69 loc) · 1.29 KB
/
other.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
package otk
import (
"strconv"
"time"
)
// UniqueSlice returns all unique keys in `in` by modifying it in place
func UniqueSlice(in []string) (out []string) {
set := make(Set, len(in))
out = in[:0]
for _, s := range in {
if set.AddIfNotExists(s) {
out = append(out, s)
}
}
return
}
// TryParseTime will try to parse input in all provided go time formats.
// Accepts: U for unix ts, UN for unix nano.
func TryParseTime(ts string, layouts []string, tz *time.Location) time.Time {
const ms = 10000000000
var (
t time.Time
err error
)
if tz == nil {
tz = time.UTC
}
for _, l := range layouts {
if l == "U" || l == "UN" {
var n int64
if n, err = strconv.ParseInt(ts, 10, 64); err != nil {
continue
}
if l == "U" {
if n > ms { // javascript ts in ms
n = n / 1000
}
t = time.Unix(n, 0)
} else {
t = time.Unix(0, n)
}
return t.In(tz)
}
if t, err = time.ParseInLocation(l, ts, tz); err == nil {
return t
}
}
return time.Time{}
}
type M = map[string]interface{}
func MergeMap(dst, src M) M {
if dst == nil {
return src
}
for k, v := range src {
if v, ok := v.(M); ok {
if dv, ok := dst[k].(M); ok {
MergeMap(dv, v)
continue
}
}
if v == nil {
delete(dst, k)
} else {
dst[k] = v
}
}
return dst
}