Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/spf13/cast/time.go
2875 views
1
// Copyright © 2014 Steve Francia <[email protected]>.
2
//
3
// Use of this source code is governed by an MIT-style
4
// license that can be found in the LICENSE file.
5
6
package cast
7
8
import (
9
"encoding/json"
10
"errors"
11
"fmt"
12
"strings"
13
"time"
14
15
"github.com/spf13/cast/internal"
16
)
17
18
// ToTimeE any value to a [time.Time] type.
19
func ToTimeE(i any) (time.Time, error) {
20
return ToTimeInDefaultLocationE(i, time.UTC)
21
}
22
23
// ToTimeInDefaultLocationE casts an empty interface to [time.Time],
24
// interpreting inputs without a timezone to be in the given location,
25
// or the local timezone if nil.
26
func 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.
64
func ToDurationE(i any) (time.Duration, error) {
65
i, _ = indirect(i)
66
67
switch s := i.(type) {
68
case time.Duration:
69
return s, nil
70
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
71
v, err := ToInt64E(s)
72
if err != nil {
73
// TODO: once there is better error handling, this should be easier
74
return 0, errors.New(strings.ReplaceAll(err.Error(), " int64", "time.Duration"))
75
}
76
77
return time.Duration(v), nil
78
case float32, float64, float64EProvider, float64Provider:
79
v, err := ToFloat64E(s)
80
if err != nil {
81
// TODO: once there is better error handling, this should be easier
82
return 0, errors.New(strings.ReplaceAll(err.Error(), " float64", "time.Duration"))
83
}
84
85
return time.Duration(v), nil
86
case string:
87
if !strings.ContainsAny(s, "nsuµmh") {
88
return time.ParseDuration(s + "ns")
89
}
90
91
return time.ParseDuration(s)
92
case nil:
93
return time.Duration(0), nil
94
default:
95
if i, ok := resolveAlias(i); ok {
96
return ToDurationE(i)
97
}
98
99
return 0, fmt.Errorf(errorMsg, i, i, time.Duration(0))
100
}
101
}
102
103
// StringToDate attempts to parse a string into a [time.Time] type using a
104
// predefined list of formats.
105
//
106
// If no suitable format is found, an error is returned.
107
func StringToDate(s string) (time.Time, error) {
108
return internal.ParseDateWith(s, time.UTC, internal.TimeFormats)
109
}
110
111
// StringToDateInDefaultLocation casts an empty interface to a [time.Time],
112
// interpreting inputs without a timezone to be in the given location,
113
// or the local timezone if nil.
114
func StringToDateInDefaultLocation(s string, location *time.Location) (time.Time, error) {
115
return internal.ParseDateWith(s, location, internal.TimeFormats)
116
}
117
118