package grmi import ( "encoding/json" "fmt" "leit.com/aps_engine/utils" "time" ) type DateTime time.Time const ( URLDateTimeFormat = "20060102150405" URLDateFormat = "20060102" DateOutFormat = "2006-01-02" DatetimeOutFormat = "2006-01-02 15:04:05" ) func (self DateTime) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf("\"%s\"", time.Time(self).Format(URLDateTimeFormat))), nil } func (self *DateTime) UnmarshalJSON(data []byte) error { var jsonString string err := json.Unmarshal(data, &jsonString) if err != nil { return err } result, err := time.ParseInLocation(URLDateTimeFormat, jsonString, utils.TimezoneLocation) if err != nil { return err } *self = DateTime(result) return nil } func (self *DateTime) Restore() time.Time { return time.Time(*self) } func (self *DateTime) ToString() string { date := self.Restore() return date.Format(DatetimeOutFormat) }