github.com/schmorrison/Zoho@v1.1.4/time.go (about) 1 package zoho 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 ) 8 9 // Time is a time.Time which can be marshalled/unmarshalled according to Zoho's specific time scheme 10 type Time time.Time 11 12 var zohoTimeLayout = "2006-01-02T15:04:05-07:00" 13 14 // MarshalJSON is the json marshalling function for Time internal type 15 func (t *Time) MarshalJSON() ([]byte, error) { 16 if *t == Time(time.Time{}) { 17 return []byte("null"), nil 18 } 19 stamp := fmt.Sprintf("\"%s\"", time.Time(*t).Format(zohoTimeLayout)) 20 return []byte(stamp), nil 21 } 22 23 // UnmarshalJSON is the json unmarshalling function for Time internal type 24 func (t *Time) UnmarshalJSON(b []byte) error { 25 s := strings.Trim(string(b), "\"") 26 if s == "null" { 27 *t = Time(time.Time{}) 28 return nil 29 } 30 pTime, err := time.Parse(zohoTimeLayout, s) 31 if err == nil { 32 *t = Time(pTime) 33 } 34 return err 35 } 36 37 // Date iis a time.Time which can be marshalled/unmarshalled according to Zoho's specific date scheme 38 type Date time.Time 39 40 var zohoDateLayout = "2006-01-02" 41 42 // MarshalJSON is the json marshalling function for Date internal type 43 func (d *Date) MarshalJSON() ([]byte, error) { 44 if *d == Date(time.Time{}) { 45 return []byte("null"), nil 46 } 47 stamp := fmt.Sprintf("\"%s\"", time.Time(*d).Format(zohoDateLayout)) 48 return []byte(stamp), nil 49 } 50 51 // UnmarshalJSON is the json unmarshalling function for Date internal type 52 func (d *Date) UnmarshalJSON(b []byte) error { 53 s := strings.Trim(string(b), "\"") 54 if s == "null" { 55 *d = Date(time.Time{}) 56 return nil 57 } 58 pTime, err := time.Parse(zohoDateLayout, s) 59 if err == nil { 60 *d = Date(pTime) 61 } 62 return err 63 }