forked from preichenberger/go-coinbasepro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
82 lines (66 loc) · 1.58 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package coinbasepro
import (
"database/sql/driver"
"fmt"
"strings"
"time"
)
type ServerTime struct {
ISO string `json:"iso"`
Epoch float64 `json:"epoch,number"`
}
func (c *Client) GetTime() (ServerTime, error) {
var serverTime ServerTime
url := fmt.Sprintf("/time")
_, err := c.Request("GET", url, nil, &serverTime)
return serverTime, err
}
type Time time.Time
func (t *Time) UnmarshalJSON(data []byte) error {
var err error
var parsedTime time.Time
if string(data) == "null" {
*t = Time(time.Time{})
return nil
}
layouts := []string{
"2006-01-02 15:04:05+00",
"2006-01-02T15:04:05-07:00",
"2006-01-02T15:04:05.999999Z",
"2006-01-02 15:04:05.999999",
"2006-01-02T15:04:05Z",
"2006-01-02 15:04:05.999999+00"}
for _, layout := range layouts {
parsedTime, err = time.Parse(layout,
strings.Replace(string(data), "\"", "", -1))
if err != nil {
continue
}
break
}
if parsedTime.IsZero() {
return err
}
*t = Time(parsedTime)
return nil
}
// MarshalJSON marshal time back to time.Time for json encoding
func (t Time) MarshalJSON() ([]byte, error) {
return t.Time().MarshalJSON()
}
func (t *Time) Time() time.Time {
return time.Time(*t)
}
// Scan implements the sql.Scanner interface for database deserialization.
func (t *Time) Scan(value interface{}) error {
timeValue, ok := value.(time.Time)
if !ok {
return fmt.Errorf("failed to deserialize time: %#v", value)
}
*t = Time(timeValue)
return nil
}
// Value implements the driver.Valuer interface for database serialization.
func (t Time) Value() (driver.Value, error) {
return t.Time(), nil
}