-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtime.go
124 lines (107 loc) · 2.74 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package meteomatics
import (
"strconv"
"strings"
"time"
)
// A TimeString is a string representing a time.
type TimeString string
// A TimeStringer can be converted to a TimeString.
type TimeStringer interface {
TimeString() TimeString
}
// Time shortcuts.
const (
TimeNow TimeString = "now"
TimeTomorrow TimeString = "tomorrow"
TimeYesterday TimeString = "yesterday"
)
// TimeString returns s as a TimeString.
func (s TimeString) TimeString() TimeString {
return s
}
// A NowOffset is a time relative to now.
type NowOffset time.Duration
// TimeString returns o as a TimeString.
func (o NowOffset) TimeString() TimeString {
if o == 0 {
return TimeNow
}
sign := "+"
if o < 0 {
o = -o
sign = "-"
}
for _, unit := range []struct {
divisor time.Duration
suffix string
}{
{divisor: time.Hour, suffix: "H"},
{divisor: time.Minute, suffix: "M"},
} {
if time.Duration(o)%unit.divisor == 0 {
return TimeNow + TimeString(sign+strconv.Itoa(int(time.Duration(o)/unit.divisor))+unit.suffix)
}
}
return TimeNow + TimeString(sign+strconv.Itoa(int(time.Duration(o)/time.Second))+"S")
}
// A TimePeriod is a time period.
type TimePeriod struct {
Start time.Time
Duration time.Duration
Step time.Duration
}
// TimeString returns p as a TimeString.
func (p TimePeriod) TimeString() TimeString {
return TimeString(formatTime(p.Start) +
"P" + formatDuration(p.Duration) +
":P" + formatDuration(p.Step))
}
// A Time is a time.
type Time time.Time
// TimeString returns t as a TimeString.
func (t Time) TimeString() TimeString {
return TimeString(formatTime(time.Time(t)))
}
// A TimeRange is a range of times.
type TimeRange struct {
Start time.Time
End time.Time
Step time.Duration
}
// TimeString returns r as a TimeString.
func (r TimeRange) TimeString() TimeString {
return TimeString(formatTime(r.Start) +
"--" + formatTime(r.End) +
":P" + formatDuration(r.Step))
}
// A TimeSlice is a slice of TimeStringers.
type TimeSlice []TimeStringer
// TimeString returns s as a TimeString.
func (s TimeSlice) TimeString() TimeString {
ss := make([]string, len(s))
for i, ts := range s {
ss[i] = string(ts.TimeString())
}
return TimeString(strings.Join(ss, ","))
}
func formatDuration(d time.Duration) string {
for _, unit := range []struct {
divisor time.Duration
prefix string
suffix string
}{
{divisor: 7 * 24 * time.Hour, suffix: "W"},
{divisor: 24 * time.Hour, suffix: "D"},
{divisor: time.Hour, prefix: "T", suffix: "H"},
{divisor: time.Minute, prefix: "T", suffix: "M"},
} {
if d%unit.divisor == 0 {
return unit.prefix + strconv.Itoa(int(d/unit.divisor)) + unit.suffix
}
}
return "T" + strconv.Itoa(int(d/time.Second)) + "S"
}
func formatTime(t time.Time) string {
return t.UTC().Format(time.RFC3339)
}