1
// Copyright © 2014 Steve Francia <spf@spf13.com>.
3
// Use of this source code is governed by an MIT-style
4
// license that can be found in the LICENSE file.
19
var errNegativeNotAllowed = errors.New("unable to cast negative value")
21
// ToTimeE casts an interface to a time.Time type.
22
func ToTimeE(i interface{}) (tim time.Time, err error) {
23
return ToTimeInDefaultLocationE(i, time.UTC)
26
// ToTimeInDefaultLocationE casts an empty interface to time.Time,
27
// interpreting inputs without a timezone to be in the given location,
28
// or the local timezone if nil.
29
func ToTimeInDefaultLocationE(i interface{}, location *time.Location) (tim time.Time, err error) {
32
switch v := i.(type) {
36
return StringToDateInDefaultLocation(v, location)
38
s, err1 := ToInt64E(v)
40
return time.Time{}, fmt.Errorf("unable to cast %#v of type %T to Time", i, i)
42
return time.Unix(s, 0), nil
44
return time.Unix(int64(v), 0), nil
46
return time.Unix(v, 0), nil
48
return time.Unix(int64(v), 0), nil
50
return time.Unix(int64(v), 0), nil
52
return time.Unix(int64(v), 0), nil
54
return time.Unix(int64(v), 0), nil
56
return time.Time{}, fmt.Errorf("unable to cast %#v of type %T to Time", i, i)
60
// ToDurationE casts an interface to a time.Duration type.
61
func ToDurationE(i interface{}) (d time.Duration, err error) {
64
switch s := i.(type) {
67
case int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8:
68
d = time.Duration(ToInt64(s))
70
case float32, float64:
71
d = time.Duration(ToFloat64(s))
74
if strings.ContainsAny(s, "nsuµmh") {
75
d, err = time.ParseDuration(s)
77
d, err = time.ParseDuration(s + "ns")
86
err = fmt.Errorf("unable to cast %#v of type %T to Duration", i, i)
91
// ToBoolE casts an interface to a bool type.
92
func ToBoolE(i interface{}) (bool, error) {
95
switch b := i.(type) {
127
return strconv.ParseBool(i.(string))
129
v, err := ToInt64E(b)
133
return false, fmt.Errorf("unable to cast %#v of type %T to bool", i, i)
135
return false, fmt.Errorf("unable to cast %#v of type %T to bool", i, i)
139
// ToFloat64E casts an interface to a float64 type.
140
func ToFloat64E(i interface{}) (float64, error) {
145
return float64(intv), nil
148
switch s := i.(type) {
152
return float64(s), nil
154
return float64(s), nil
156
return float64(s), nil
158
return float64(s), nil
160
return float64(s), nil
162
return float64(s), nil
164
return float64(s), nil
166
return float64(s), nil
168
return float64(s), nil
170
return float64(s), nil
172
v, err := strconv.ParseFloat(s, 64)
176
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
178
v, err := s.Float64()
182
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
191
return 0, fmt.Errorf("unable to cast %#v of type %T to float64", i, i)
195
// ToFloat32E casts an interface to a float32 type.
196
func ToFloat32E(i interface{}) (float32, error) {
201
return float32(intv), nil
204
switch s := i.(type) {
206
return float32(s), nil
210
return float32(s), nil
212
return float32(s), nil
214
return float32(s), nil
216
return float32(s), nil
218
return float32(s), nil
220
return float32(s), nil
222
return float32(s), nil
224
return float32(s), nil
226
return float32(s), nil
228
v, err := strconv.ParseFloat(s, 32)
230
return float32(v), nil
232
return 0, fmt.Errorf("unable to cast %#v of type %T to float32", i, i)
234
v, err := s.Float64()
236
return float32(v), nil
238
return 0, fmt.Errorf("unable to cast %#v of type %T to float32", i, i)
247
return 0, fmt.Errorf("unable to cast %#v of type %T to float32", i, i)
251
// ToInt64E casts an interface to an int64 type.
252
func ToInt64E(i interface{}) (int64, error) {
257
return int64(intv), nil
260
switch s := i.(type) {
284
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
288
return 0, fmt.Errorf("unable to cast %#v of type %T to int64", i, i)
290
return ToInt64E(string(s))
299
return 0, fmt.Errorf("unable to cast %#v of type %T to int64", i, i)
303
// ToInt32E casts an interface to an int32 type.
304
func ToInt32E(i interface{}) (int32, error) {
309
return int32(intv), nil
312
switch s := i.(type) {
336
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
340
return 0, fmt.Errorf("unable to cast %#v of type %T to int32", i, i)
342
return ToInt32E(string(s))
351
return 0, fmt.Errorf("unable to cast %#v of type %T to int32", i, i)
355
// ToInt16E casts an interface to an int16 type.
356
func ToInt16E(i interface{}) (int16, error) {
361
return int16(intv), nil
364
switch s := i.(type) {
388
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
392
return 0, fmt.Errorf("unable to cast %#v of type %T to int16", i, i)
394
return ToInt16E(string(s))
403
return 0, fmt.Errorf("unable to cast %#v of type %T to int16", i, i)
407
// ToInt8E casts an interface to an int8 type.
408
func ToInt8E(i interface{}) (int8, error) {
413
return int8(intv), nil
416
switch s := i.(type) {
440
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
444
return 0, fmt.Errorf("unable to cast %#v of type %T to int8", i, i)
446
return ToInt8E(string(s))
455
return 0, fmt.Errorf("unable to cast %#v of type %T to int8", i, i)
459
// ToIntE casts an interface to an int type.
460
func ToIntE(i interface{}) (int, error) {
468
switch s := i.(type) {
492
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
496
return 0, fmt.Errorf("unable to cast %#v of type %T to int64", i, i)
498
return ToIntE(string(s))
507
return 0, fmt.Errorf("unable to cast %#v of type %T to int", i, i)
511
// ToUintE casts an interface to a uint type.
512
func ToUintE(i interface{}) (uint, error) {
518
return 0, errNegativeNotAllowed
520
return uint(intv), nil
523
switch s := i.(type) {
525
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
528
return 0, errNegativeNotAllowed
532
return 0, fmt.Errorf("unable to cast %#v of type %T to uint", i, i)
534
return ToUintE(string(s))
537
return 0, errNegativeNotAllowed
542
return 0, errNegativeNotAllowed
547
return 0, errNegativeNotAllowed
552
return 0, errNegativeNotAllowed
567
return 0, errNegativeNotAllowed
572
return 0, errNegativeNotAllowed
583
return 0, fmt.Errorf("unable to cast %#v of type %T to uint", i, i)
587
// ToUint64E casts an interface to a uint64 type.
588
func ToUint64E(i interface{}) (uint64, error) {
594
return 0, errNegativeNotAllowed
596
return uint64(intv), nil
599
switch s := i.(type) {
601
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
604
return 0, errNegativeNotAllowed
606
return uint64(v), nil
608
return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i)
610
return ToUint64E(string(s))
613
return 0, errNegativeNotAllowed
615
return uint64(s), nil
618
return 0, errNegativeNotAllowed
620
return uint64(s), nil
623
return 0, errNegativeNotAllowed
625
return uint64(s), nil
628
return 0, errNegativeNotAllowed
630
return uint64(s), nil
632
return uint64(s), nil
636
return uint64(s), nil
638
return uint64(s), nil
640
return uint64(s), nil
643
return 0, errNegativeNotAllowed
645
return uint64(s), nil
648
return 0, errNegativeNotAllowed
650
return uint64(s), nil
659
return 0, fmt.Errorf("unable to cast %#v of type %T to uint64", i, i)
663
// ToUint32E casts an interface to a uint32 type.
664
func ToUint32E(i interface{}) (uint32, error) {
670
return 0, errNegativeNotAllowed
672
return uint32(intv), nil
675
switch s := i.(type) {
677
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
680
return 0, errNegativeNotAllowed
682
return uint32(v), nil
684
return 0, fmt.Errorf("unable to cast %#v of type %T to uint32", i, i)
686
return ToUint32E(string(s))
689
return 0, errNegativeNotAllowed
691
return uint32(s), nil
694
return 0, errNegativeNotAllowed
696
return uint32(s), nil
699
return 0, errNegativeNotAllowed
701
return uint32(s), nil
704
return 0, errNegativeNotAllowed
706
return uint32(s), nil
708
return uint32(s), nil
710
return uint32(s), nil
714
return uint32(s), nil
716
return uint32(s), nil
719
return 0, errNegativeNotAllowed
721
return uint32(s), nil
724
return 0, errNegativeNotAllowed
726
return uint32(s), nil
735
return 0, fmt.Errorf("unable to cast %#v of type %T to uint32", i, i)
739
// ToUint16E casts an interface to a uint16 type.
740
func ToUint16E(i interface{}) (uint16, error) {
746
return 0, errNegativeNotAllowed
748
return uint16(intv), nil
751
switch s := i.(type) {
753
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
756
return 0, errNegativeNotAllowed
758
return uint16(v), nil
760
return 0, fmt.Errorf("unable to cast %#v of type %T to uint16", i, i)
762
return ToUint16E(string(s))
765
return 0, errNegativeNotAllowed
767
return uint16(s), nil
770
return 0, errNegativeNotAllowed
772
return uint16(s), nil
775
return 0, errNegativeNotAllowed
777
return uint16(s), nil
780
return 0, errNegativeNotAllowed
782
return uint16(s), nil
784
return uint16(s), nil
786
return uint16(s), nil
788
return uint16(s), nil
792
return uint16(s), nil
795
return 0, errNegativeNotAllowed
797
return uint16(s), nil
800
return 0, errNegativeNotAllowed
802
return uint16(s), nil
811
return 0, fmt.Errorf("unable to cast %#v of type %T to uint16", i, i)
815
// ToUint8E casts an interface to a uint type.
816
func ToUint8E(i interface{}) (uint8, error) {
822
return 0, errNegativeNotAllowed
824
return uint8(intv), nil
827
switch s := i.(type) {
829
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
832
return 0, errNegativeNotAllowed
836
return 0, fmt.Errorf("unable to cast %#v of type %T to uint8", i, i)
838
return ToUint8E(string(s))
841
return 0, errNegativeNotAllowed
846
return 0, errNegativeNotAllowed
851
return 0, errNegativeNotAllowed
856
return 0, errNegativeNotAllowed
871
return 0, errNegativeNotAllowed
876
return 0, errNegativeNotAllowed
887
return 0, fmt.Errorf("unable to cast %#v of type %T to uint8", i, i)
891
// From html/template/content.go
892
// Copyright 2011 The Go Authors. All rights reserved.
893
// indirect returns the value, after dereferencing as many times
894
// as necessary to reach the base type (or nil).
895
func indirect(a interface{}) interface{} {
899
if t := reflect.TypeOf(a); t.Kind() != reflect.Ptr {
900
// Avoid creating a reflect.Value if it's not a pointer.
903
v := reflect.ValueOf(a)
904
for v.Kind() == reflect.Ptr && !v.IsNil() {
910
// From html/template/content.go
911
// Copyright 2011 The Go Authors. All rights reserved.
912
// indirectToStringerOrError returns the value, after dereferencing as many times
913
// as necessary to reach the base type (or nil) or an implementation of fmt.Stringer
915
func indirectToStringerOrError(a interface{}) interface{} {
920
var errorType = reflect.TypeOf((*error)(nil)).Elem()
921
var fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
923
v := reflect.ValueOf(a)
924
for !v.Type().Implements(fmtStringerType) && !v.Type().Implements(errorType) && v.Kind() == reflect.Ptr && !v.IsNil() {
930
// ToStringE casts an interface to a string type.
931
func ToStringE(i interface{}) (string, error) {
932
i = indirectToStringerOrError(i)
934
switch s := i.(type) {
938
return strconv.FormatBool(s), nil
940
return strconv.FormatFloat(s, 'f', -1, 64), nil
942
return strconv.FormatFloat(float64(s), 'f', -1, 32), nil
944
return strconv.Itoa(s), nil
946
return strconv.FormatInt(s, 10), nil
948
return strconv.Itoa(int(s)), nil
950
return strconv.FormatInt(int64(s), 10), nil
952
return strconv.FormatInt(int64(s), 10), nil
954
return strconv.FormatUint(uint64(s), 10), nil
956
return strconv.FormatUint(uint64(s), 10), nil
958
return strconv.FormatUint(uint64(s), 10), nil
960
return strconv.FormatUint(uint64(s), 10), nil
962
return strconv.FormatUint(uint64(s), 10), nil
964
return s.String(), nil
966
return string(s), nil
968
return string(s), nil
970
return string(s), nil
972
return string(s), nil
974
return string(s), nil
975
case template.HTMLAttr:
976
return string(s), nil
980
return s.String(), nil
982
return s.Error(), nil
984
return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
988
// ToStringMapStringE casts an interface to a map[string]string type.
989
func ToStringMapStringE(i interface{}) (map[string]string, error) {
990
var m = map[string]string{}
992
switch v := i.(type) {
993
case map[string]string:
995
case map[string]interface{}:
996
for k, val := range v {
997
m[ToString(k)] = ToString(val)
1000
case map[interface{}]string:
1001
for k, val := range v {
1002
m[ToString(k)] = ToString(val)
1005
case map[interface{}]interface{}:
1006
for k, val := range v {
1007
m[ToString(k)] = ToString(val)
1011
err := jsonStringToObject(v, &m)
1014
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]string", i, i)
1018
// ToStringMapStringSliceE casts an interface to a map[string][]string type.
1019
func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
1020
var m = map[string][]string{}
1022
switch v := i.(type) {
1023
case map[string][]string:
1025
case map[string][]interface{}:
1026
for k, val := range v {
1027
m[ToString(k)] = ToStringSlice(val)
1030
case map[string]string:
1031
for k, val := range v {
1032
m[ToString(k)] = []string{val}
1034
case map[string]interface{}:
1035
for k, val := range v {
1036
switch vt := val.(type) {
1038
m[ToString(k)] = ToStringSlice(vt)
1042
m[ToString(k)] = []string{ToString(val)}
1046
case map[interface{}][]string:
1047
for k, val := range v {
1048
m[ToString(k)] = ToStringSlice(val)
1051
case map[interface{}]string:
1052
for k, val := range v {
1053
m[ToString(k)] = ToStringSlice(val)
1056
case map[interface{}][]interface{}:
1057
for k, val := range v {
1058
m[ToString(k)] = ToStringSlice(val)
1061
case map[interface{}]interface{}:
1062
for k, val := range v {
1063
key, err := ToStringE(k)
1065
return m, fmt.Errorf("unable to cast %#v of type %T to map[string][]string", i, i)
1067
value, err := ToStringSliceE(val)
1069
return m, fmt.Errorf("unable to cast %#v of type %T to map[string][]string", i, i)
1074
err := jsonStringToObject(v, &m)
1077
return m, fmt.Errorf("unable to cast %#v of type %T to map[string][]string", i, i)
1082
// ToStringMapBoolE casts an interface to a map[string]bool type.
1083
func ToStringMapBoolE(i interface{}) (map[string]bool, error) {
1084
var m = map[string]bool{}
1086
switch v := i.(type) {
1087
case map[interface{}]interface{}:
1088
for k, val := range v {
1089
m[ToString(k)] = ToBool(val)
1092
case map[string]interface{}:
1093
for k, val := range v {
1094
m[ToString(k)] = ToBool(val)
1097
case map[string]bool:
1100
err := jsonStringToObject(v, &m)
1103
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]bool", i, i)
1107
// ToStringMapE casts an interface to a map[string]interface{} type.
1108
func ToStringMapE(i interface{}) (map[string]interface{}, error) {
1109
var m = map[string]interface{}{}
1111
switch v := i.(type) {
1112
case map[interface{}]interface{}:
1113
for k, val := range v {
1114
m[ToString(k)] = val
1117
case map[string]interface{}:
1120
err := jsonStringToObject(v, &m)
1123
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]interface{}", i, i)
1127
// ToStringMapIntE casts an interface to a map[string]int{} type.
1128
func ToStringMapIntE(i interface{}) (map[string]int, error) {
1129
var m = map[string]int{}
1131
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i)
1134
switch v := i.(type) {
1135
case map[interface{}]interface{}:
1136
for k, val := range v {
1137
m[ToString(k)] = ToInt(val)
1140
case map[string]interface{}:
1141
for k, val := range v {
1145
case map[string]int:
1148
err := jsonStringToObject(v, &m)
1152
if reflect.TypeOf(i).Kind() != reflect.Map {
1153
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i)
1156
mVal := reflect.ValueOf(m)
1157
v := reflect.ValueOf(i)
1158
for _, keyVal := range v.MapKeys() {
1159
val, err := ToIntE(v.MapIndex(keyVal).Interface())
1161
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int", i, i)
1163
mVal.SetMapIndex(keyVal, reflect.ValueOf(val))
1168
// ToStringMapInt64E casts an interface to a map[string]int64{} type.
1169
func ToStringMapInt64E(i interface{}) (map[string]int64, error) {
1170
var m = map[string]int64{}
1172
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i)
1175
switch v := i.(type) {
1176
case map[interface{}]interface{}:
1177
for k, val := range v {
1178
m[ToString(k)] = ToInt64(val)
1181
case map[string]interface{}:
1182
for k, val := range v {
1186
case map[string]int64:
1189
err := jsonStringToObject(v, &m)
1193
if reflect.TypeOf(i).Kind() != reflect.Map {
1194
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i)
1196
mVal := reflect.ValueOf(m)
1197
v := reflect.ValueOf(i)
1198
for _, keyVal := range v.MapKeys() {
1199
val, err := ToInt64E(v.MapIndex(keyVal).Interface())
1201
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]int64", i, i)
1203
mVal.SetMapIndex(keyVal, reflect.ValueOf(val))
1208
// ToSliceE casts an interface to a []interface{} type.
1209
func ToSliceE(i interface{}) ([]interface{}, error) {
1212
switch v := i.(type) {
1214
return append(s, v...), nil
1215
case []map[string]interface{}:
1216
for _, u := range v {
1221
return s, fmt.Errorf("unable to cast %#v of type %T to []interface{}", i, i)
1225
// ToBoolSliceE casts an interface to a []bool type.
1226
func ToBoolSliceE(i interface{}) ([]bool, error) {
1228
return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
1231
switch v := i.(type) {
1236
kind := reflect.TypeOf(i).Kind()
1238
case reflect.Slice, reflect.Array:
1239
s := reflect.ValueOf(i)
1240
a := make([]bool, s.Len())
1241
for j := 0; j < s.Len(); j++ {
1242
val, err := ToBoolE(s.Index(j).Interface())
1244
return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
1250
return []bool{}, fmt.Errorf("unable to cast %#v of type %T to []bool", i, i)
1254
// ToStringSliceE casts an interface to a []string type.
1255
func ToStringSliceE(i interface{}) ([]string, error) {
1258
switch v := i.(type) {
1260
for _, u := range v {
1261
a = append(a, ToString(u))
1267
for _, u := range v {
1268
a = append(a, ToString(u))
1272
for _, u := range v {
1273
a = append(a, ToString(u))
1277
for _, u := range v {
1278
a = append(a, ToString(u))
1282
for _, u := range v {
1283
a = append(a, ToString(u))
1287
for _, u := range v {
1288
a = append(a, ToString(u))
1292
for _, u := range v {
1293
a = append(a, ToString(u))
1297
return strings.Fields(v), nil
1299
for _, err := range i.([]error) {
1300
a = append(a, err.Error())
1304
str, err := ToStringE(v)
1306
return a, fmt.Errorf("unable to cast %#v of type %T to []string", i, i)
1308
return []string{str}, nil
1310
return a, fmt.Errorf("unable to cast %#v of type %T to []string", i, i)
1314
// ToIntSliceE casts an interface to a []int type.
1315
func ToIntSliceE(i interface{}) ([]int, error) {
1317
return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
1320
switch v := i.(type) {
1325
kind := reflect.TypeOf(i).Kind()
1327
case reflect.Slice, reflect.Array:
1328
s := reflect.ValueOf(i)
1329
a := make([]int, s.Len())
1330
for j := 0; j < s.Len(); j++ {
1331
val, err := ToIntE(s.Index(j).Interface())
1333
return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
1339
return []int{}, fmt.Errorf("unable to cast %#v of type %T to []int", i, i)
1343
// ToDurationSliceE casts an interface to a []time.Duration type.
1344
func ToDurationSliceE(i interface{}) ([]time.Duration, error) {
1346
return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i)
1349
switch v := i.(type) {
1350
case []time.Duration:
1354
kind := reflect.TypeOf(i).Kind()
1356
case reflect.Slice, reflect.Array:
1357
s := reflect.ValueOf(i)
1358
a := make([]time.Duration, s.Len())
1359
for j := 0; j < s.Len(); j++ {
1360
val, err := ToDurationE(s.Index(j).Interface())
1362
return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i)
1368
return []time.Duration{}, fmt.Errorf("unable to cast %#v of type %T to []time.Duration", i, i)
1372
// StringToDate attempts to parse a string into a time.Time type using a
1373
// predefined list of formats. If no suitable format is found, an error is
1375
func StringToDate(s string) (time.Time, error) {
1376
return parseDateWith(s, time.UTC, timeFormats)
1379
// StringToDateInDefaultLocation casts an empty interface to a time.Time,
1380
// interpreting inputs without a timezone to be in the given location,
1381
// or the local timezone if nil.
1382
func StringToDateInDefaultLocation(s string, location *time.Location) (time.Time, error) {
1383
return parseDateWith(s, location, timeFormats)
1386
type timeFormatType int
1389
timeFormatNoTimezone timeFormatType = iota
1390
timeFormatNamedTimezone
1391
timeFormatNumericTimezone
1392
timeFormatNumericAndNamedTimezone
1396
type timeFormat struct {
1401
func (f timeFormat) hasTimezone() bool {
1402
// We don't include the formats with only named timezones, see
1403
// https://github.com/golang/go/issues/19694#issuecomment-289103522
1404
return f.typ >= timeFormatNumericTimezone && f.typ <= timeFormatNumericAndNamedTimezone
1408
timeFormats = []timeFormat{
1409
// Keep common formats at the top.
1410
{"2006-01-02", timeFormatNoTimezone},
1411
{time.RFC3339, timeFormatNumericTimezone},
1412
{"2006-01-02T15:04:05", timeFormatNoTimezone}, // iso8601 without timezone
1413
{time.RFC1123Z, timeFormatNumericTimezone},
1414
{time.RFC1123, timeFormatNamedTimezone},
1415
{time.RFC822Z, timeFormatNumericTimezone},
1416
{time.RFC822, timeFormatNamedTimezone},
1417
{time.RFC850, timeFormatNamedTimezone},
1418
{"2006-01-02 15:04:05.999999999 -0700 MST", timeFormatNumericAndNamedTimezone}, // Time.String()
1419
{"2006-01-02T15:04:05-0700", timeFormatNumericTimezone}, // RFC3339 without timezone hh:mm colon
1420
{"2006-01-02 15:04:05Z0700", timeFormatNumericTimezone}, // RFC3339 without T or timezone hh:mm colon
1421
{"2006-01-02 15:04:05", timeFormatNoTimezone},
1422
{time.ANSIC, timeFormatNoTimezone},
1423
{time.UnixDate, timeFormatNamedTimezone},
1424
{time.RubyDate, timeFormatNumericTimezone},
1425
{"2006-01-02 15:04:05Z07:00", timeFormatNumericTimezone},
1426
{"02 Jan 2006", timeFormatNoTimezone},
1427
{"2006-01-02 15:04:05 -07:00", timeFormatNumericTimezone},
1428
{"2006-01-02 15:04:05 -0700", timeFormatNumericTimezone},
1429
{time.Kitchen, timeFormatTimeOnly},
1430
{time.Stamp, timeFormatTimeOnly},
1431
{time.StampMilli, timeFormatTimeOnly},
1432
{time.StampMicro, timeFormatTimeOnly},
1433
{time.StampNano, timeFormatTimeOnly},
1437
func parseDateWith(s string, location *time.Location, formats []timeFormat) (d time.Time, e error) {
1439
for _, format := range formats {
1440
if d, e = time.Parse(format.format, s); e == nil {
1442
// Some time formats have a zone name, but no offset, so it gets
1443
// put in that zone name (not the default one passed in to us), but
1444
// without that zone's offset. So set the location manually.
1445
if format.typ <= timeFormatNamedTimezone {
1446
if location == nil {
1447
location = time.Local
1449
year, month, day := d.Date()
1450
hour, min, sec := d.Clock()
1451
d = time.Date(year, month, day, hour, min, sec, d.Nanosecond(), location)
1457
return d, fmt.Errorf("unable to parse date: %s", s)
1460
// jsonStringToObject attempts to unmarshall a string as JSON into
1461
// the object passed as pointer.
1462
func jsonStringToObject(s string, v interface{}) error {
1464
return json.Unmarshal(data, v)
1467
// toInt returns the int value of v if v or v's underlying type
1469
// Note that this will return false for int64 etc. types.
1470
func toInt(v interface{}) (int, bool) {
1471
switch v := v.(type) {
1483
func trimZeroDecimal(s string) string {
1485
for i := len(s); i > 0; i-- {