-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnulltime.go
More file actions
47 lines (38 loc) · 794 Bytes
/
nulltime.go
File metadata and controls
47 lines (38 loc) · 794 Bytes
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
package gitbot
import (
"strings"
"time"
)
// NullTime allows us to parse time OR a null from JSON
type NullTime struct {
Valid bool
Time time.Time
}
// MarshalJSON turns null time into json
func (n NullTime) MarshalJSON() ([]byte, error) {
if !n.Valid {
return []byte(`null`), nil
}
return []byte(n.Time.Format(time.RFC3339)), nil
}
// UnmarshalJSON parses time from json
func (n *NullTime) UnmarshalJSON(data []byte) error {
str := strings.Trim(string(data), `"`)
if str == "null" {
n.Valid = false
return nil
}
var err error
if n.Time, err = time.Parse(time.RFC3339, str); err != nil {
return err
}
n.Valid = true
return nil
}
// String for fmt.Stringer
func (n NullTime) String() string {
if !n.Valid {
return ""
}
return n.Time.Format(time.RFC3339)
}