Debian Codegraph @

flag_timestamp.go

raw symbols SCIP
1 package cli
2
3 import (
4 "errors"
5 "fmt"
6 "time"
7 )
8
9 type TimestampFlag = FlagBase[time.Time, TimestampConfig, timestampValue]
10
11 // TimestampConfig defines the config for timestamp flags
12 type TimestampConfig struct {
13 Timezone *time.Location
14 // Available layouts for flag value.
15 //
16 // Note that value for formats with missing year/date will be interpreted as current year/date respectively.
17 //
18 // Read more about time layouts: https://pkg.go.dev/time#pkg-constants
19 Layouts []string
20 }
21
22 // timestampValue wrap to satisfy golang's flag interface.
23 type timestampValue struct {
24 timestamp *time.Time
25 hasBeenSet bool
26 layouts []string
27 location *time.Location
28 }
29
30 var _ ValueCreator[time.Time, TimestampConfig] = timestampValue{}
31
32 // Below functions are to satisfy the ValueCreator interface
33
34 func (t timestampValue) Create(val time.Time, p *time.Time, c TimestampConfig) Value {
35 *p = val
36 return &timestampValue{
37 timestamp: p,
38 layouts: c.Layouts,
39 location: c.Timezone,
40 }
41 }
42
43 func (t timestampValue) ToString(b time.Time) string {
44 if b.IsZero() {
45 return ""
46 }
47 t.timestamp = &b
48 return t.String()
49 }
50
51 // Below functions are to satisfy the Value interface
52
53 // Parses the string value to timestamp
54 func (t *timestampValue) Set(value string) error {
55 var timestamp time.Time
56 var err error
57
58 if t.location == nil {
59 t.location = time.UTC
60 }
61
62 if len(t.layouts) == 0 {
63 return errors.New("got nil/empty layouts slice")
64 }
65
66 for _, layout := range t.layouts {
67 var locErr error
68
69 timestamp, locErr = time.ParseInLocation(layout, value, t.location)
70 if locErr != nil {
71 if err == nil {
72 err = locErr
73 continue
74 }
75
76 err = newMultiError(err, locErr)
77 continue
78 }
79
80 err = nil
81 break
82 }
83
84 if err != nil {
85 return err
86 }
87
88 defaultTS, _ := time.ParseInLocation(time.TimeOnly, time.TimeOnly, timestamp.Location())
89
90 n := time.Now().In(timestamp.Location())
91
92 // If format is missing date (or year only), set it explicitly to current
93 if timestamp.Truncate(time.Hour*24).UnixNano() == defaultTS.Truncate(time.Hour*24).UnixNano() {
94 timestamp = time.Date(
95 n.Year(),
96 n.Month(),
97 n.Day(),
98 timestamp.Hour(),
99 timestamp.Minute(),
100 timestamp.Second(),
101 timestamp.Nanosecond(),
102 timestamp.Location(),
103 )
104 } else if timestamp.Year() == 0 {
105 timestamp = time.Date(
106 n.Year(),
107 timestamp.Month(),
108 timestamp.Day(),
109 timestamp.Hour(),
110 timestamp.Minute(),
111 timestamp.Second(),
112 timestamp.Nanosecond(),
113 timestamp.Location(),
114 )
115 }
116
117 if t.timestamp != nil {
118 *t.timestamp = timestamp
119 }
120 t.hasBeenSet = true
121 return nil
122 }
123
124 // String returns a readable representation of this value (for usage defaults)
125 func (t *timestampValue) String() string {
126 return fmt.Sprintf("%v", t.timestamp)
127 }
128
129 // Get returns the flag structure
130 func (t *timestampValue) Get() any {
131 return *t.timestamp
132 }
133
134 // Timestamp gets the timestamp from a flag name
135 func (cmd *Command) Timestamp(name string) time.Time {
136 if v, ok := cmd.Value(name).(time.Time); ok {
137 tracef("time.Time available for flag name %[1]q with value=%[2]v (cmd=%[3]q)", name, v, cmd.Name)
138 return v
139 }
140
141 tracef("time.Time NOT available for flag name %[1]q (cmd=%[2]q)", name, cmd.Name)
142 return time.Time{}
143 }