This commit is contained in:
Jan Bader 2022-02-20 21:06:17 +00:00
parent 260ac2d4ad
commit 545f223a97

View File

@ -9,7 +9,7 @@ import (
type JSONDate time.Time
// Implement Marshaler and Unmarshaler interface
// UnmarshalJSON parses the JSONDate from a JSON input.
func (j *JSONDate) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
t, err := time.Parse("2006-01-02", s)
@ -20,6 +20,7 @@ func (j *JSONDate) UnmarshalJSON(b []byte) error {
return nil
}
// MarshalJSON converts the JSONDate to a JSON in ISO format.
func (j JSONDate) MarshalJSON() ([]byte, error) {
result, err := json.Marshal(time.Time(j))
if err != nil {
@ -29,7 +30,7 @@ func (j JSONDate) MarshalJSON() ([]byte, error) {
return result, nil
}
// Maybe a Format function for printing your date
// Format formats the time using the regular time.Time mechanics..
func (j JSONDate) Format(s string) string {
t := time.Time(j)
return t.Format(s)