forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
42 lines (37 loc) · 1.6 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
package tcclient
import (
"errors"
"time"
)
// Time wraps time.Time in order that json serialisation/deserialisation can be
// adapted. Marshaling time.Time types results in RFC3339 dates with nanosecond
// precision in the user's timezone. In order that the json date representation
// is consistent between what we send in json payloads, and what taskcluster
// services return, we wrap time.Time into type tcclient.Time which marshals
// instead to the same format used by the Taskcluster services; UTC based, with
// millisecond precision, using 'Z' timezone, e.g. 2015-10-27T20:36:19.255Z.
type Time time.Time
// MarshalJSON implements the json.Marshaler interface.
// The time is a quoted string in RFC 3339 format, with sub-second precision added if present.
func (t Time) MarshalJSON() ([]byte, error) {
if y := time.Time(t).Year(); y < 0 || y >= 10000 {
// RFC 3339 is clear that years are 4 digits exactly.
// See golang.org/issue/4556#c15 for more discussion.
return nil, errors.New("queue.Time.MarshalJSON: year outside of range [0,9999]")
}
return []byte(`"` + t.String() + `"`), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
// The time is expected to be a quoted string in RFC 3339 format.
func (t *Time) UnmarshalJSON(data []byte) (err error) {
// Fractional seconds are handled implicitly by Parse.
x := new(time.Time)
*x, err = time.Parse(`"`+time.RFC3339+`"`, string(data))
*t = Time(*x)
return
}
// Returns the Time in canonical RFC3339 representation, e.g.
// 2015-10-27T20:36:19.255Z
func (t Time) String() string {
return time.Time(t).UTC().Format("2006-01-02T15:04:05.000Z")
}