-
Notifications
You must be signed in to change notification settings - Fork 34
/
time.go
63 lines (54 loc) · 1.54 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
63
package zoho
import (
"fmt"
"strings"
"time"
)
// Time is a time.Time which can be marshalled/unmarshalled according to Zoho's specific time scheme
type Time time.Time
var zohoTimeLayout = "2006-01-02T15:04:05-07:00"
// MarshalJSON is the json marshalling function for Time internal type
func (t *Time) MarshalJSON() ([]byte, error) {
if *t == Time(time.Time{}) {
return []byte("null"), nil
}
stamp := fmt.Sprintf("\"%s\"", time.Time(*t).Format(zohoTimeLayout))
return []byte(stamp), nil
}
// UnmarshalJSON is the json unmarshalling function for Time internal type
func (t *Time) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
if s == "null" {
*t = Time(time.Time{})
return nil
}
pTime, err := time.Parse(zohoTimeLayout, s)
if err == nil {
*t = Time(pTime)
}
return err
}
// Date iis a time.Time which can be marshalled/unmarshalled according to Zoho's specific date scheme
type Date time.Time
var zohoDateLayout = "2006-01-02"
// MarshalJSON is the json marshalling function for Date internal type
func (d *Date) MarshalJSON() ([]byte, error) {
if *d == Date(time.Time{}) {
return []byte("null"), nil
}
stamp := fmt.Sprintf("\"%s\"", time.Time(*d).Format(zohoDateLayout))
return []byte(stamp), nil
}
// UnmarshalJSON is the json unmarshalling function for Date internal type
func (d *Date) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
if s == "null" {
*d = Date(time.Time{})
return nil
}
pTime, err := time.Parse(zohoDateLayout, s)
if err == nil {
*d = Date(pTime)
}
return err
}