-
Notifications
You must be signed in to change notification settings - Fork 54
/
util.go
138 lines (102 loc) · 2.57 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package forest
import (
"encoding/json"
"fmt"
"github.com/labstack/gommon/log"
"math/rand"
"net"
"time"
)
func GenerateSerialNo() string {
now := time.Now()
format := now.Format("20060102150405")
suffer := fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
return fmt.Sprintf("%s%s", format, suffer)
}
func ToDateString(date time.Time) string {
return date.Format("2006-01-02 15:04:05")
}
func ParkJobConf(jobConf *JobConf) (value []byte, err error) {
value, err = json.Marshal(jobConf)
return
}
func UParkJobConf(value []byte) (jobConf *JobConf, err error) {
jobConf = new(JobConf)
err = json.Unmarshal(value, jobConf)
return
}
func ParkGroupConf(groupConf *GroupConf) (value []byte, err error) {
value, err = json.Marshal(groupConf)
return
}
func UParkGroupConf(value []byte) (groupConf *GroupConf, err error) {
groupConf = new(GroupConf)
err = json.Unmarshal(value, groupConf)
return
}
func ParkJobSnapshot(snapshot *JobSnapshot) (value []byte, err error) {
value, err = json.Marshal(snapshot)
return
}
func UParkJobSnapshot(value []byte) (snapshot *JobSnapshot, err error) {
snapshot = new(JobSnapshot)
err = json.Unmarshal(value, snapshot)
return
}
func ParkJobExecuteSnapshot(snapshot *JobExecuteSnapshot) (value []byte, err error) {
value, err = json.Marshal(snapshot)
return
}
func UParkJobExecuteSnapshot(value []byte) (snapshot *JobExecuteSnapshot, err error) {
snapshot = new(JobExecuteSnapshot)
err = json.Unmarshal(value, snapshot)
return
}
func GetLocalIpAddress() (ip string) {
addrs, err := net.InterfaceAddrs()
if err != nil {
log.Warnf("err:%#v", err)
return
}
for _, value := range addrs {
if ipnet, ok := value.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ip = ipnet.IP.String()
return
}
}
}
ip = "127.0.0.1"
return
}
func TimeSubDays(t1, t2 time.Time) int {
if t1.Location().String() != t2.Location().String() {
return -1
}
hours := t1.Sub(t2).Hours()
if hours <= 0 {
return -1
}
// sub hours less than 24
if hours < 24 {
// may same day
t1y, t1m, t1d := t1.Date()
t2y, t2m, t2d := t2.Date()
isSameDay := (t1y == t2y && t1m == t2m && t1d == t2d)
if isSameDay {
return 0
} else {
return 1
}
} else { // equal or more than 24
if (hours/24)-float64(int(hours/24)) == 0 { // just 24's times
return int(hours / 24)
} else { // more than 24 hours
return int(hours/24) + 1
}
}
}
func ParseInLocation(value string) (dateTime time.Time, err error) {
dateTime, err = time.Parse("2006-01-02 15:04:05", value)
return
}