forked from zoom-lib-golang/zoom-lib-golang
-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.go
41 lines (34 loc) · 978 Bytes
/
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
package zoom
import (
"fmt"
"strings"
"time"
)
// Time is a custom Time type that accounts for null values and empty strings // during JSON marshaling and unmarshaling
type Time struct {
time.Time
}
// UnmarshalJSON describes JSON unmarshaling for custom Time objects, handling
// empty string values
func (t *Time) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" || s == "" {
t.Time = time.Time{}
return
}
t.Time, err = time.Parse(time.RFC3339, s)
return
}
// MarshalJSON describes JSON unmarshaling for custom Time objects, handling
// empty string values
func (t *Time) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%s\"", t.Time.Format(time.RFC3339))), nil
}
// Format calls format on the underlying time object
func (t *Time) Format(format string) string {
return t.Time.Format(format)
}
// String defines how time is printed out
func (t *Time) String() string {
return t.Format(time.RFC3339)
}