-
Notifications
You must be signed in to change notification settings - Fork 59
/
timestamp.go
48 lines (39 loc) · 1.36 KB
/
timestamp.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
package buildkite
import "time"
// BuildKiteDateFormat is the format of the dates used throughout the
// api, note this odd string is used to parse/format dates in go
const BuildKiteDateFormat = time.RFC3339Nano
// BuildKiteEventDateFormat is the format of the dates used in webhook events.
const BuildKiteEventDateFormat = "2006-01-02 15:04:05 MST"
// Timestamp custom timestamp to support buildkite api timestamps
type Timestamp struct {
time.Time
}
// NewTimestamp make a new timestamp using the time suplied.
func NewTimestamp(t time.Time) *Timestamp {
return &Timestamp{t}
}
func (ts Timestamp) String() string {
return ts.Time.String()
}
// MarshalJSON implements the json.Marshaler interface.
func (ts Timestamp) MarshalJSON() ([]byte, error) {
return []byte(ts.Format(`"` + BuildKiteDateFormat + `"`)), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (ts *Timestamp) UnmarshalJSON(data []byte) (err error) {
(*ts).Time, err = time.Parse(`"`+BuildKiteDateFormat+`"`, string(data))
if err != nil {
// try the webhook format too; avoid clobbering the error if both fail
t, err2 := time.Parse(`"`+BuildKiteEventDateFormat+`"`, string(data))
if err2 == nil {
(*ts).Time = t
err = err2
}
}
return
}
// Equal reports whether t and u are equal based on time.Equal
func (ts Timestamp) Equal(u Timestamp) bool {
return ts.Time.Equal(u.Time)
}