-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtime.go
62 lines (51 loc) · 1.12 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package omgo
import (
"fmt"
"strings"
"time"
)
const atLayout = "2006-01-02T15:04"
const adLayout = "2006-01-02"
var nilTime = (time.Time{}).UnixNano()
type ApiTime struct {
time.Time
}
type ApiDate struct {
time.Time
}
func (ct *ApiTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
ct.Time = time.Time{}
return
}
ct.Time, err = time.Parse(atLayout, s)
return
}
func (ct *ApiTime) MarshalJSON() ([]byte, error) {
if ct.Time.UnixNano() == nilTime {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("\"%s\"", ct.Time.Format(atLayout))), nil
}
func (ct *ApiTime) IsSet() bool {
return ct.UnixNano() != nilTime
}
func (ct *ApiDate) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
ct.Time = time.Time{}
return
}
ct.Time, err = time.Parse(adLayout, s)
return
}
func (ct *ApiDate) MarshalJSON() ([]byte, error) {
if ct.Time.UnixNano() == nilTime {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("\"%s\"", ct.Time.Format(adLayout))), nil
}
func (ct *ApiDate) IsSet() bool {
return ct.UnixNano() != nilTime
}