1package godo
2
3import (
4	"strconv"
5	"time"
6)
7
8// Timestamp represents a time that can be unmarshalled from a JSON string
9// formatted as either an RFC3339 or Unix timestamp. All
10// exported methods of time.Time can be called on Timestamp.
11type Timestamp struct {
12	time.Time
13}
14
15func (t Timestamp) String() string {
16	return t.Time.String()
17}
18
19// UnmarshalJSON implements the json.Unmarshaler interface.
20// Time is expected in RFC3339 or Unix format.
21func (t *Timestamp) UnmarshalJSON(data []byte) error {
22	str := string(data)
23	i, err := strconv.ParseInt(str, 10, 64)
24	if err == nil {
25		t.Time = time.Unix(i, 0)
26	} else {
27		t.Time, err = time.Parse(`"`+time.RFC3339+`"`, str)
28	}
29	return err
30}
31
32// Equal reports whether t and u are equal based on time.Equal
33func (t Timestamp) Equal(u Timestamp) bool {
34	return t.Time.Equal(u.Time)
35}
36