-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_kzutils.go
313 lines (220 loc) · 4.91 KB
/
class_kzutils.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
312
313
package KzPack4Go
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"time"
)
type KzUnicode string
func (s KzUnicode) MarshalJSON() ([]byte, error) {
return []byte(strconv.QuoteToASCII(string(s))), nil
}
func ToDate() int64 {
result, _ := strconv.ParseInt(time.Now().Format("20060102"), 10, 64)
return result
}
func DatePlus(aStartDay int) int64 {
result, _ := strconv.ParseInt(time.Now().AddDate(0, 0, aStartDay).Format("20060102"), 10, 64)
return result
}
func ToTime() float64 {
result, _ := strconv.ParseFloat(time.Now().Format("20060102150405"), 64)
return result
}
func SubStr(str string, start int, end int) string {
rs := []rune(str)
length := len(rs)
if start < 0 || start > length {
return ""
}
if end < 0 || end > length {
return ""
}
return string(rs[start:end])
}
func TimeToStr(Value float64) string {
//#20200618105004 -> 2020-06-18 10:50:04
result := fmt.Sprintf("%.0f", Value)
if len(result) != 14 {
return "-1"
}
a := result[0:4]
b := result[4:6]
c := result[6:8]
d := result[8:10]
e := result[10:12]
f := result[12:14]
return fmt.Sprintf("%s-%s-%s %s:%s:%s", a, b, c, d, e, f)
}
func DateToStr(Value int64) string {
//#20200618 -> 2020-06-18
result := fmt.Sprintf("%d", Value)
if len(result) != 8 {
return "-1"
}
a := result[0:4]
b := result[4:6]
c := result[6:8]
return fmt.Sprintf("%s-%s-%s", a, b, c)
}
func TimePlus(aStartDay int) float64 {
result, _ := strconv.ParseFloat(time.Now().AddDate(0, 0, aStartDay).Format("20060102150405"), 64)
return result
}
func ToKJQJ() int64 {
result, _ := strconv.ParseInt(time.Now().Format("200601"), 10, 64)
return result
}
func Min(x, y int64) int64 {
if x < y {
return x
}
return y
}
func Max(x, y int64) int64 {
if x > y {
return x
}
return y
}
func StrToInt(Value string) int64 {
result, _ := strconv.ParseInt(Value, 10, 64)
return result
}
func IntToStr(Value int64) string {
result := strconv.Itoa(int(Value))
return result
}
func FormatFloat(Value float64) float64 {
result, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", Value), 64)
return result
}
func IfThenI(Value bool, Result0, Result1 int64) int64 {
Result := Result1
if Value {
Result = Result0
}
return Result
}
func IfFailI(Value bool, Result0 int64) int64 {
var Result int64 = 0
if Value {
Result = Result0
}
return Result
}
func IfThenS(Value bool, Result0, Result1 string) string {
Result := Result1
if Value {
Result = Result0
}
return Result
}
func IfFailS(Value bool, Result0 string) string {
Result := ""
if Value {
Result = Result0
}
return Result
}
func IfThenF(Value bool, Result0, Result1 float64) float64 {
Result := Result1
if Value {
Result = Result0
}
return Result
}
func IfFailF(Value bool, Result0 float64) float64 {
var Result float64 = 0
if Value {
Result = Result0
}
return Result
}
func Contain(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func FileExist(aFilePath string) (bool, error) {
_, eror := os.Stat(aFilePath)
if eror != nil {
return false, eror
}
if os.IsNotExist(eror) {
return false, nil
}
return true, nil
}
func IsFile(aFilePath string) bool {
fi, e := os.Stat(aFilePath)
if e != nil {
return false
}
return !fi.IsDir()
}
func ToFile(aFilePath string, v interface{}) error {
var eror error
var source []byte
/*
if !IsFile(aFilePath) {
return errors.New("Error: The FilePath is not a FileName;")
}
*/
source, eror = json.Marshal(v)
if eror != nil {
return eror
}
eror = ioutil.WriteFile(aFilePath, source, 0666)
if eror != nil {
return eror
}
return nil
}
func InFile(aFilePath string, v interface{}) error {
var eror error
var didExist bool
/*
if !IsFile(aFilePath) {
return errors.New("Error: The FilePath is not a FileName;")
}
*/
didExist, _ = FileExist(aFilePath)
if !didExist {
eror = ToFile(aFilePath, v)
if eror != nil {
return eror
}
return nil
}
source, eror := ioutil.ReadFile(aFilePath)
if eror != nil {
return eror
}
eror = json.Unmarshal(source, &v)
if eror != nil {
return eror
}
return nil
}
func FormatFileSize(fileSize int64) (size string) {
if fileSize < 1024 {
//return strconv.FormatInt(fileSize, 10) + "B"
return fmt.Sprintf("%.2fB", float64(fileSize)/float64(1))
} else if fileSize < (1024 * 1024) {
return fmt.Sprintf("%.2fKB", float64(fileSize)/float64(1024))
} else if fileSize < (1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fMB", float64(fileSize)/float64(1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fGB", float64(fileSize)/float64(1024*1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fTB", float64(fileSize)/float64(1024*1024*1024*1024))
} else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
return fmt.Sprintf("%.2fEB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
}
}