forked from gridscale/gsclient-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
37 lines (31 loc) · 785 Bytes
/
types.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
package gsclient
import (
"encoding/json"
"time"
)
const gsTimeLayout = "2006-01-02T15:04:05Z"
// GSTime is the custom time type of gridscale.
type GSTime struct {
time.Time
}
// UnmarshalJSON is the custom unmarshaller for GSTime.
func (t *GSTime) UnmarshalJSON(b []byte) error {
var tstring string
if err := json.Unmarshal(b, &tstring); err != nil {
return err
}
if tstring != "" {
parsedTime, err := time.Parse(gsTimeLayout, tstring)
*t = GSTime{parsedTime}
return err
}
return nil
}
// MarshalJSON is the custom marshaller for GSTime.
func (t GSTime) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Time.Format(gsTimeLayout))
}
// String returns string representation of GSTime.
func (t GSTime) String() string {
return t.Time.Format(gsTimeLayout)
}