package model import ( "errors" "fmt" "time" ) type Date time.Time func (d Date) Equal(other Date) bool { // usec and nsec are lost in ISO8601 conversion dr := time.Time(d).Round(time.Minute) or := time.Time(other).Round(time.Minute) return dr.Equal(or) } func (d Date) After(other Date) bool { return time.Time(d).After(time.Time(other)) } func (d Date) Before(other Date) bool { return time.Time(d).Before(time.Time(other)) } func (d Date) Format(layout string) string { return time.Time(d).Format(layout) } func (d Date) MarshalJSON() ([]byte, error) { // Must use ISO8601 formatted := fmt.Sprintf("\"%s\"", time.Time(d).Format("2006-01-02T15:04:05+00:00")) return []byte(formatted), nil } func (d *Date) UnmarshalJSON(data []byte) error { if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' { return errors.New("Time.UnmarshalJSON: input is not a JSON string") } data = data[len(`"`) : len(data)-len(`"`)] var err error var t time.Time t, err = time.ParseInLocation("2006-01-02T15:04:05+00:00", string(data), time.Now().Location()) if err != nil { return err } *d = Date(t) return nil }