forked from appscode/go-hetzner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_time.go
43 lines (34 loc) · 920 Bytes
/
json_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
package hetzner
import (
"bytes"
"fmt"
"time"
)
// JSONTime For ordering API, MarketProduct.NextReduceDate
type JSONTime struct {
time.Time
}
func (t *JSONTime) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, t.Format("2006-01-02 15:04:05"))), nil
}
func (t *JSONTime) UnmarshalJSON(data []byte) (err error) {
if bytes.Equal(data, []byte(`"null"`)) {
return nil
}
t.Time, err = time.Parse(`"2006-01-02 15:04:05"`, string(data))
return
}
// JSONDate For server API, server Cancellation.EarliestCancellationDate and Cancellation.CancellationDate
type JSONDate struct {
time.Time
}
func (t *JSONDate) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, t.Format("2006-01-02"))), nil
}
func (t *JSONDate) UnmarshalJSON(data []byte) (err error) {
if bytes.Equal(data, []byte(`"null"`)) {
return nil
}
t.Time, err = time.Parse(`"2006-01-02"`, string(data))
return
}