31 lines
551 B
Go
31 lines
551 B
Go
package http
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type JSONDate time.Time
|
|
|
|
// Implement Marshaler and Unmarshaler interface
|
|
func (j *JSONDate) UnmarshalJSON(b []byte) error {
|
|
s := strings.Trim(string(b), "\"")
|
|
t, err := time.Parse("2006-01-02", s)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*j = JSONDate(t)
|
|
return nil
|
|
}
|
|
|
|
func (j JSONDate) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(time.Time(j))
|
|
}
|
|
|
|
// Maybe a Format function for printing your date
|
|
func (j JSONDate) Format(s string) string {
|
|
t := time.Time(j)
|
|
return t.Format(s)
|
|
}
|