-
Notifications
You must be signed in to change notification settings - Fork 797
/
day.go
59 lines (49 loc) · 1.23 KB
/
day.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
package flagext
import (
"time"
"github.com/prometheus/common/model"
)
const secondsInDay = 24 * 60 * 60
// DayValue is a model.Time that can be used as a flag.
// NB it only parses days!
type DayValue struct {
model.Time
set bool
}
// NewDayValue makes a new DayValue; will round t down to the nearest midnight.
func NewDayValue(t model.Time) DayValue {
return DayValue{
Time: model.TimeFromUnix((t.Unix() / secondsInDay) * secondsInDay),
set: true,
}
}
// String implements flag.Value
func (v DayValue) String() string {
return v.Time.Time().Format(time.RFC3339)
}
// Set implements flag.Value
func (v *DayValue) Set(s string) error {
t, err := time.Parse("2006-01-02", s)
if err != nil {
return err
}
v.Time = model.TimeFromUnix(t.Unix())
v.set = true
return nil
}
// IsSet returns true is the DayValue has been set.
func (v *DayValue) IsSet() bool {
return v.set
}
// UnmarshalYAML implements yaml.Unmarshaler.
func (v *DayValue) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
return v.Set(s)
}
// MarshalYAML implements yaml.Marshaler.
func (v DayValue) MarshalYAML() (interface{}, error) {
return v.Time.Time().Format("2006-01-02"), nil
}