-
Notifications
You must be signed in to change notification settings - Fork 3
/
flag_time.go
54 lines (47 loc) · 954 Bytes
/
flag_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
package cli
import (
"fmt"
"time"
)
type timeValue struct {
val *time.Time
}
func (v *timeValue) Set(value string) error {
val, err := stringToTime(value)
if err != nil {
return err
}
*v.val = *val
return nil
}
func (v *timeValue) String() string {
return (*v.val).String()
}
var timePatterns = []string{
time.RFC3339,
time.RFC3339Nano,
"2006-01-02T15:04:05", // iso8601 without timezone
time.RFC1123Z,
time.RFC1123,
"2006-01-02 15:04:05.999999999 -0700 MST",
time.RFC822Z,
time.RFC822,
time.ANSIC,
time.UnixDate,
time.RubyDate,
"2006-01-02 15:04:05Z07:00",
"2006-01-02 15:04:05",
"02 Jan 06 15:04 MST",
"2006-01-02",
"02 Jan 2006",
"2006-01-02 15:04:05 -07:00",
"2006-01-02 15:04:05 -0700",
}
func stringToTime(s string) (*time.Time, error) {
for _, pattern := range timePatterns {
if d, err := time.Parse(pattern, s); err == nil {
return &d, nil
}
}
return nil, fmt.Errorf("unable to parse date: %s", s)
}