-
Notifications
You must be signed in to change notification settings - Fork 89
/
time.go
54 lines (44 loc) · 1.03 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
package helix
import (
"strings"
"time"
)
const (
requestDateTimeFormat = "2006-01-02 15:04:05 -0700 MST"
)
var (
datetimeFields = []string{
"started_at",
"start_time",
"vacation_start_time",
"vacation_end_time",
"ended_at",
"end_time",
}
)
// Time is our custom time struct.
type Time struct {
time.Time
}
// UnmarshalJSON is our custom datetime unmarshaller. Twitch sometimes
// returns datetimes as empty strings, which casuses issues with the native time
// UnmarshalJSON method when decoding the JSON string. Here we handle that scenario,
// by returning a zero time value for any JSON time field that is either an
// empty string or "null".
func (t *Time) UnmarshalJSON(b []byte) (err error) {
timeStr := strings.Trim(string(b), "\"")
if timeStr == "" || timeStr == "null" {
t.Time = time.Time{}
return
}
t.Time, err = time.Parse(time.RFC3339, timeStr)
return
}
func isDatetimeTagField(tag string) bool {
for _, tagField := range datetimeFields {
if tagField == tag {
return true
}
}
return false
}