-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
311 lines (261 loc) · 5.49 KB
/
base.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package util
import (
"crypto/rand"
"encoding/hex"
uuid "github.com/nu7hatch/gouuid"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
func ParseInt(s string, defaultValue int64) int64 {
value, err := strconv.ParseInt(s, 10, 0)
if err != nil {
return defaultValue
}
return value
}
func SplitToIntSlice(s string) []int64 {
ids := strings.Split(s, ",")
if len(ids) == 0 {
return []int64{}
}
ints := make([]int64, 0, len(ids))
for _, v := range ids {
value := ParseInt(v, 0)
if value > 0 {
ints = append(ints, value)
}
}
return ints
}
func Getwd() string {
pwd, err := os.Getwd()
if err != nil {
return strconv.Itoa(filepath.Separator)
}
return pwd
}
func NewUUID() *uuid.UUID {
uuid, err := uuid.NewV4()
if err != nil {
return nil
}
return uuid
}
func IsDirExists(dir string) bool {
_, err := os.Stat(dir)
return err != nil && os.IsExist(err)
}
func MergeValues(src, dst url.Values) {
for k, v := range src {
dst[k] = v
}
}
func Int64SliceToString(s []int64) []string {
retval := make([]string, 0, len(s))
for _, v := range s {
retval = append(retval, strconv.FormatInt(v, 10))
}
return retval
}
func IntSliceToString(s []int) []string {
retval := make([]string, 0, len(s))
for _, v := range s {
retval = append(retval, strconv.Itoa(v))
}
return retval
}
func StringSliceToInt(s []string) []int {
retval := make([]int, 0, len(s))
for _, v := range s {
i, err := strconv.Atoi(v)
if err != nil {
//logger.Warnln(err)
}
retval = append(retval, i)
}
return retval
}
func StringSliceToInt64(s []string) []int64 {
retval := make([]int64, 0, len(s))
for _, v := range s {
i, err := strconv.ParseInt(v, 10, 0)
if err != nil {
}
retval = append(retval, i)
}
return retval
}
func Today() time.Time {
now := time.Now()
y, m, d := now.Date()
return time.Date(y, m, d, 0, 0, 0, 0, now.Location())
}
func Yesterday() time.Time {
return Today().Add(time.Hour * -24)
}
var Location *time.Location = time.Local
func GetCurrYearMonth() (int, time.Time) {
now := time.Now()
year := now.Year()
month := now.Month()
thetime := time.Date(year, month, 1, 0, 0, 0, 0, Location)
return year*100 + int(month), thetime
}
func GetYearMonth(t time.Time) int {
year := t.Year()
month := t.Month()
return year*100 + int(month)
}
// 201401, 201402 以此类推
func GetPrevYearMonth() (int, time.Time) {
now := time.Now()
prevMonth := now.AddDate(0, -1, 0)
year := prevMonth.Year()
month := prevMonth.Month()
// time.LoadLocation("Asia/ShangHai")
prevMonth = time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
return year*100 + int(month), prevMonth
}
func MonthToRange(month int) (begin, end time.Time, err error) {
month_string := strconv.Itoa(month)
begin, err = time.Parse("200601", month_string)
if err != nil {
return
}
end = begin.AddDate(0, 1, 0)
return
}
var digits = "0123456789abcdefghijklmnopqrstuvwxyz"
func RandNumberString(length int) string {
b := make([]byte, length)
rand.Read(b)
for i, num := range b {
b[i] = digits[num%10]
}
return string(b)
}
// 生成一个随机位数的字符串
func RandString(bits int) string {
b := make([]byte, bits)
rand.Read(b)
for i, num := range b {
b[i] = digits[num&31]
}
return string(b)
}
// 随机字节,并转换成十六进制小写
func RandHex(n int) string {
b := make([]byte, n)
rand.Read(b)
return hex.EncodeToString(b)
}
//生成唯一UUID
func UniqueId() string {
uuid := NewUUID()
return hex.EncodeToString(uuid[:])
}
func ParseAddress(remoteAddr string) string {
ip_port_sep_index := strings.LastIndex(remoteAddr, ":")
if ip_port_sep_index > 0 {
remoteAddr = remoteAddr[0:ip_port_sep_index]
}
return remoteAddr
}
func GetUserAgent(req *http.Request) string {
return req.Header.Get("User-Agent")
}
func ToTimestamp(t *time.Time) int64 {
return t.Unix() * 1000
}
func HasFormValue(req *http.Request, key string) bool {
if req.Form == nil {
req.ParseMultipartForm(1024 * 1024)
}
_, ok := req.Form[key]
return ok
}
func String2DateTime(s string, loc *time.Location) *time.Time {
if loc == nil {
loc = time.Local
}
t, err := time.ParseInLocation("2006-1-2 15:4:5", s, loc)
if err != nil {
return nil
} else {
return &t
}
}
func NowDate() time.Time {
t := time.Now()
y, m, d := t.Date()
return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
}
func NowDateString() string {
t := time.Now()
return t.Format("2006-1-2")
}
func NowDateTimeString() string {
t := time.Now()
return t.Format("2006-1-2 15:04:05")
}
func String2Date(s string, loc *time.Location) *time.Time {
if loc == nil {
loc = time.Local
}
t, err := time.ParseInLocation("2006-1-2", s, loc)
if err != nil {
return nil
} else {
return &t
}
}
//
//file, _ := exec.LookPath(os.Args[0])
//path, _ := filepath.Abs(file)
//fmt.Println(path)
//
func GetPartOfInt64Slice(theSlice []int64, fromKey int, size int) []int64 {
var dest []int64
if size <= 0 {
return dest
}
if len(theSlice) <= size{
return theSlice
}
if fromKey >= len(theSlice) {
return dest
}
for key, theInt64 := range theSlice {
if key >= fromKey {
if len(dest) <= size{
dest = append(dest, theInt64)
}
}
}
return dest
}
func GetPartOfStringSlice(theSlice []string, fromKey int, size int) []string {
var dest []string
if size <= 0 {
return dest
}
if len(theSlice) <= size{
return theSlice
}
if fromKey >= len(theSlice) {
return dest
}
for key, theString := range theSlice {
if key >= fromKey {
if len(dest) <= size{
dest = append(dest, theString)
}
}
}
return dest
}