-
Notifications
You must be signed in to change notification settings - Fork 9
/
cast.go
299 lines (251 loc) · 6.76 KB
/
cast.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
package toolkit
import (
"fmt"
"math"
"reflect"
"sort"
"strconv"
"strings"
"time"
)
const (
RoundingAuto = "RoundAuto"
RoundingUp = "RoundUp"
RoundingDown = "RoundDown"
)
func Value(o interface{}) reflect.Value {
return reflect.Indirect(reflect.ValueOf(o))
}
func Kind(o interface{}) reflect.Kind {
return Value(o).Kind()
}
func ToString(o interface{}) string {
if IsPointer(o) {
return ""
}
v := Value(o)
k := v.Kind()
if k == reflect.Interface && v.IsNil() {
return ""
} else if k == reflect.String {
if val, ok := o.(string); ok {
return val
}
return Sprintf("%s", o)
} else if k == reflect.Int || k == reflect.Int8 || k == reflect.Int16 || k == reflect.Int32 || k == reflect.Int64 {
return fmt.Sprintf("%d", o)
} else if k == reflect.Uint || k == reflect.Uint8 || k == reflect.Uint16 || k == reflect.Uint32 || k == reflect.Uint64 {
return fmt.Sprintf("%d", o)
} else if k == reflect.Float32 || k == reflect.Float64 {
return fmt.Sprintf("%f", o)
} else if k == reflect.Bool {
return fmt.Sprintf("%t", o)
} else if k == reflect.Array || k == reflect.Slice || k == reflect.Map || k == reflect.Struct {
return fmt.Sprintf("%v", o)
} else {
return ""
}
}
/*===============================
== LEGEND ========================
=================================
d = date
dd = date 2 digit
M = month
MM = month 2 digit
MMM = month in name, 3 chars
MMMM = month in name, full
YY = Year 2 digit
YYYY = Year 4 digit
h = hour
hh = hour 2 digit
H = hour in 24 cycle
HH = hour in 24 cycle 2 digit
m = minute
mm = minute 2 digits
s = Second
ss = second 2 digit
A = AMPM
T = Timezone
=================================*/
func getFormatDate(o interface{}, dateFormat string) string {
var dateMap = map[string]string{"dd": "02", "d": "2", "MMMM": "January", "MMM": "Jan", "MM": "01", "M": "1",
"YYYY": "2006", "YY": "06", "hh": "03", "h": "3", "HH": "15", "mm": "04", "m": "4", "ss": "05", "s": "5",
"A": "PM", "T": "MST",
}
var dateOrder = map[int]string{1: "dd", 2: "d", 3: "MMMM", 4: "MMM", 5: "MM", 6: "M", 7: "YYYY", 8: "YY",
9: "hh", 10: "h", 11: "HH", 12: "mm", 13: "m", 14: "ss", 15: "s", 16: "A", 17: "T",
}
var keys []int
for k := range dateOrder {
keys = append(keys, k)
}
sort.Ints(keys)
dateFormat = strings.Replace(dateFormat, "y", "Y", -1)
for _, k := range keys {
dateFormat = strings.Replace(dateFormat, dateOrder[k], dateMap[dateOrder[k]], -1)
}
if strings.Contains(dateFormat, "H") {
if Value(o).Kind() == reflect.String {
dateFormat = strings.Replace(dateFormat, "H", "15", -1)
} else {
if o.(time.Time).Hour() < 10 {
dateFormat = strings.Replace(dateFormat, "H", "3", -1)
} else {
dateFormat = strings.Replace(dateFormat, "H", "15", -1)
}
}
}
return dateFormat
}
var _defaultDateFormat string
func SetDefaultDateFormat(f string) {
_defaultDateFormat = f
}
func DefaultDateFormat() string {
if _defaultDateFormat == "" {
_defaultDateFormat = "dd-MMM-yyyy"
}
return _defaultDateFormat
}
func Date2String(t time.Time, dateFormat string) string {
if dateFormat == "" {
dateFormat = DefaultDateFormat()
}
dateFormat = getFormatDate(t, dateFormat)
return t.Format(dateFormat)
}
func String2Date(dateString string, dateFormat string) time.Time {
if dateFormat == "" {
dateFormat = DefaultDateFormat()
}
dateFormat = getFormatDate(dateString, dateFormat)
t, _ := time.Parse(dateFormat, dateString)
return t
}
func ToInt(o interface{}, rounding string) int {
if IsPointer(o) {
return 0
}
var ret int
k := Kind(o)
v := Value(o)
if k == reflect.String {
i := strings.Index(v.String(), ".")
if i >= 0 {
f, _ := strconv.ParseFloat(v.String(), 64)
ret = ToInt(f, rounding)
} else {
if i, e := strconv.Atoi(v.String()); e == nil {
return i
} else {
return 0
}
}
} else if k == reflect.Int || k == reflect.Int8 || k == reflect.Int16 || k == reflect.Int32 || k == reflect.Int64 {
return int(v.Int())
} else if k == reflect.Uint || k == reflect.Uint8 || k == reflect.Uint16 || k == reflect.Uint32 || k == reflect.Uint64 {
return int(v.Uint())
} else if k == reflect.Float32 || k == reflect.Float64 {
f := ToFloat64(v.Float(), 0, rounding)
return int(int64(f))
}
return ret
}
func ToFloat32(o interface{}, decimalPoint int, rounding string) float32 {
if IsPointer(o) {
return float32(0)
}
var f float64
t := strings.ToLower(TypeName(o))
v := Value(o)
if t != "interface{}" && strings.HasPrefix(t, "int") {
f = ToFloat64(v.Int(), decimalPoint, rounding)
} else if strings.HasPrefix(t, "uint") {
f = ToFloat64(v.Uint(), decimalPoint, rounding)
} else if strings.HasPrefix(t, "float") {
f = ToFloat64(v.Float(), decimalPoint, rounding)
} else {
f = ToFloat64(v.String(), decimalPoint, rounding)
}
if math.IsNaN(f) || math.IsInf(f, 0) {
f = 0
}
return float32(f)
}
func ToFloat64(o interface{}, decimalPoint int, rounding string) float64 {
//fmt.Printf("\ndec: %v\n", decimalPoint)
if IsPointer(o) {
return float64(0)
}
var f float64
var e error
t := strings.ToLower(TypeName(o))
v := Value(o)
if t != "interface{}" && strings.HasPrefix(t, "int") {
f = float64(v.Int())
} else if strings.HasPrefix(t, "uint") {
f = float64(v.Uint())
} else if strings.HasPrefix(t, "float") {
f = float64(v.Float())
} else {
f, e = strconv.ParseFloat(v.String(), 64)
if e != nil {
return 0
}
}
//fmt.Printf("\ndec: %v\n", decimalPoint)
switch rounding {
case RoundingAuto:
return RoundingAuto64(f, decimalPoint)
case RoundingDown:
return RoundingDown64(f, decimalPoint)
case RoundingUp:
return RoundingUp64(f, decimalPoint)
}
if math.IsNaN(f) || math.IsInf(f, 0) {
f = 0
}
return f
}
func RoundingAuto64(f float64, decimalPoint int) (retValue float64) {
tempPow := math.Pow(10, float64(decimalPoint))
f = f * tempPow
if f < 0 {
f = math.Ceil(f - 0.5)
} else {
f = math.Floor(f + 0.5)
}
retValue = f / tempPow
return
}
func RoundingDown64(f float64, decimalPoint int) (retValue float64) {
tempPow := math.Pow(10, float64(decimalPoint))
f = f * tempPow
f = math.Floor(f)
retValue = f / tempPow
return
}
func RoundingUp64(f float64, decimalPoint int) (retValue float64) {
tempPow := math.Pow(10, float64(decimalPoint))
f = f * tempPow
f = math.Ceil(f)
retValue = f / tempPow
return
}
func ToDate(o interface{}, formatDate string) time.Time {
v := reflect.Indirect(reflect.ValueOf(o))
t := strings.ToLower(v.Type().String())
if strings.Contains(t, "int") {
intDate := v.Int()
return time.Unix(intDate, 0)
} else if strings.Contains(t, "string") {
return String2Date(o.(string), formatDate)
} else if strings.HasSuffix(t, "time.time") {
return o.(time.Time)
}
return time.Now()
}
func ToDuration(o interface{}) time.Duration {
return time.Duration(ToInt(o, RoundingAuto)) * time.Second
}