forked from gophercloud/utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
37 lines (32 loc) · 993 Bytes
/
results.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
package gnocchi
import (
"bytes"
"encoding/json"
"time"
)
// RFC3339NanoTimezone describes a common timestamp format used by Gnocchi API responses.
const RFC3339NanoTimezone = "2006-01-02T15:04:05.999999+00:00"
// RFC3339NanoNoTimezone describes a common timestamp format that can be used for Gnocchi requests
// with time ranges.
const RFC3339NanoNoTimezone = "2006-01-02T15:04:05.999999"
// JSONRFC3339NanoTimezone is a type for Gnocchi responses timestamps with a timezone offset.
type JSONRFC3339NanoTimezone time.Time
// UnmarshalJSON helps to unmarshal timestamps from Gnocchi responses to the
// JSONRFC3339NanoTimezone type.
func (jt *JSONRFC3339NanoTimezone) UnmarshalJSON(data []byte) error {
b := bytes.NewBuffer(data)
dec := json.NewDecoder(b)
var s string
if err := dec.Decode(&s); err != nil {
return err
}
if s == "" {
return nil
}
t, err := time.Parse(RFC3339NanoTimezone, s)
if err != nil {
return err
}
*jt = JSONRFC3339NanoTimezone(t)
return nil
}