-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemas.go
74 lines (66 loc) · 1.52 KB
/
schemas.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
package gwhub
import (
"encoding/json"
"time"
)
// ISO8601 durations used by GW Hub
const (
PT1M = 1 * time.Minute
PT5M = 5 * time.Minute
PT15M = 15 * time.Minute
PT1H = 1 * time.Hour
PT3H = 3 * time.Hour
PT12H = 12 * time.Hour
P1D = 24 * time.Hour
P1W = 7 * 24 * time.Hour
//
// A month is not possible, as it is calendar based. Instead use time.AddDate(0, -1, 0) and similar.
//
// P1M = ...
//
)
// Time Windows
const (
Monday = "monday"
Tuesday = "tuesday"
Wednesday = "wednesday"
// ...
)
type Filter struct {
Entities string `json:"entities,omitempty"`
Range TimeRange `json:"range"`
TimeWindows []TimeWindow `json:"timeWindows,omitempty"`
}
type TimeRange struct {
From time.Time `json:"from"`
To time.Time `json:"to"`
}
// MarshalJSON is needed as Hub only accepts short form ISO times
func (t TimeRange) MarshalJSON() ([]byte, error) {
timerange := struct {
From string `json:"from"`
To string `json:"to"`
}{
From: t.From.Format(time.RFC3339),
To: t.To.Format(time.RFC3339),
}
return json.Marshal(timerange)
}
// TimeWindow is only time-of-day
type TimeWindow struct {
From time.Time `json:"from"`
To time.Time `json:"to"`
On []string `json:"on,omitempty"`
}
func (t TimeWindow) MarshalJSON() ([]byte, error) {
timewindow := struct {
From string `json:"from"`
To string `json:"to"`
On []string `json:"on,omitempty"`
}{
From: t.From.Format("15:04:05"),
To: t.To.Format("15:04:05"),
On: t.On,
}
return json.Marshal(timewindow)
}