MCPcopy
hub / github.com/spf13/cast / ToTimeInDefaultLocationE

Function ToTimeInDefaultLocationE

time.go:26–61  ·  view source on GitHub ↗

ToTimeInDefaultLocationE casts an empty interface to [time.Time], interpreting inputs without a timezone to be in the given location, or the local timezone if nil.

(i any, location *time.Location)

Source from the content-addressed store, hash-verified

24// interpreting inputs without a timezone to be in the given location,
25// or the local timezone if nil.
26func ToTimeInDefaultLocationE(i any, location *time.Location) (tim time.Time, err error) {
27 i, _ = indirect(i)
28
29 switch v := i.(type) {
30 case time.Time:
31 return v, nil
32 case string:
33 return StringToDateInDefaultLocation(v, location)
34 case json.Number:
35 // Originally this used ToInt64E, but adding string float conversion broke ToTime.
36 // the behavior of ToTime would have changed if we continued using it.
37 // For now, using json.Number's own Int64 method should be good enough to preserve backwards compatibility.
38 v = json.Number(trimZeroDecimal(string(v)))
39 s, err1 := v.Int64()
40 if err1 != nil {
41 return time.Time{}, fmt.Errorf(errorMsg, i, i, time.Time{})
42 }
43 return time.Unix(s, 0), nil
44 case int:
45 return time.Unix(int64(v), 0), nil
46 case int32:
47 return time.Unix(int64(v), 0), nil
48 case int64:
49 return time.Unix(v, 0), nil
50 case uint:
51 return time.Unix(int64(v), 0), nil
52 case uint32:
53 return time.Unix(int64(v), 0), nil
54 case uint64:
55 return time.Unix(int64(v), 0), nil
56 case nil:
57 return time.Time{}, nil
58 default:
59 return time.Time{}, fmt.Errorf(errorMsg, i, i, time.Time{})
60 }
61}
62
63// ToDurationE casts any value to a [time.Duration] type.
64func ToDurationE(i any) (time.Duration, error) {

Callers 3

TestTimeWithTimezonesFunction · 0.92
ToTimeInDefaultLocationFunction · 0.85
ToTimeEFunction · 0.85

Calls 3

indirectFunction · 0.85
trimZeroDecimalFunction · 0.85

Tested by 1

TestTimeWithTimezonesFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…