forked from zoom-lib-golang/zoom-lib-golang
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdate.go
More file actions
46 lines (38 loc) · 1.03 KB
/
date.go
File metadata and controls
46 lines (38 loc) · 1.03 KB
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
package zoom
import (
"fmt"
"strings"
"time"
)
const (
// DateFormat is a date only format string
DateFormat = "2006-01-02"
)
// Date is a custom Date type that accounts for null values and empty strings // during JSON marshaling and unmarshaling
type Date struct {
time.Time
}
// UnmarshalJSON describes JSON unmarshaling for custom Date objects, handling
// empty string values
func (d *Date) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" || s == "" {
d.Time = time.Time{}
return
}
d.Time, err = time.Parse(DateFormat, s)
return
}
// MarshalJSON describes JSON unmarshaling for custom Date objects, handling
// empty string values
func (d *Date) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%s\"", d.Time.Format(DateFormat))), nil
}
// Format calls format on the underlying date object
func (d *Date) Format(format string) string {
return d.Time.Format(format)
}
// String defines how date is printed out
func (d *Date) String() string {
return d.Format(DateFormat)
}