-
Notifications
You must be signed in to change notification settings - Fork 4
/
time.go
81 lines (70 loc) · 1.56 KB
/
time.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
package flagvar
import (
"fmt"
"strings"
"time"
)
// Time is a `flag.Value` for `time.Time` arguments.
// The value of the `Layout` field is used for parsing when specified.
// Otherwise, `time.RFC3339` is used.
type Time struct {
Layout string
Value time.Time
Text string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *Time) Help() string {
layout := time.RFC3339
if fv.Layout != "" {
layout = fv.Layout
}
return fmt.Sprintf("a time, e.g. %s", layout)
}
// Set is flag.Value.Set
func (fv *Time) Set(v string) error {
layout := fv.Layout
if layout == "" {
layout = time.RFC3339
}
t, err := time.Parse(layout, v)
if err == nil {
fv.Text = v
fv.Value = t
}
return err
}
func (fv *Time) String() string {
return fv.Text
}
// Times is a `flag.Value` for `time.Time` arguments.
// The value of the `Layout` field is used for parsing when specified.
// Otherwise, `time.RFC3339` is used.
type Times struct {
Layout string
Values []time.Time
Texts []string
}
// Help returns a string suitable for inclusion in a flag help message.
func (fv *Times) Help() string {
layout := time.RFC3339
if fv.Layout != "" {
layout = fv.Layout
}
return fmt.Sprintf("a time, e.g. %s", layout)
}
// Set is flag.Value.Set
func (fv *Times) Set(v string) error {
layout := fv.Layout
if layout == "" {
layout = time.RFC3339
}
t, err := time.Parse(layout, v)
if err == nil {
fv.Texts = append(fv.Texts, v)
fv.Values = append(fv.Values, t)
}
return err
}
func (fv *Times) String() string {
return strings.Join(fv.Texts, ",")
}