package time import ( "database/sql/driver" "time" ) type Time time.Time const ( timeFormat = "2006-01-02 15:04:05" ) func (t *Time) UnmarshalJSON(data []byte) (err error) { now, err := time.ParseInLocation(`"`+timeFormat+`"`, string(data), time.Local) *t = Time(now) return } func (t Time) MarshalJSON() ([]byte, error) { b := make([]byte, 0, len(timeFormat)+2) b = append(b, '"') b = time.Time(t).AppendFormat(b, timeFormat) b = append(b, '"') return b, nil } func (t Time) GobEncode() ([]byte, error) { return t.Time().MarshalBinary() } func (t Time) String() string { return time.Time(t).Format(timeFormat) } func (t Time) Time() time.Time { return time.Time(t) } func (t Time) IsZero() bool { return t.Time().IsZero() } func (t Time) After(u Time) bool { return t.Time().After(u.Time()) } func (t Time) Before(u Time) bool { return t.Time().Before(u.Time()) } func (t Time) Equal(u Time) bool { return t.Time().Equal(u.Time()) } func (t Time) Add(d time.Duration) Time { return Time(t.Time().Add(d)) } func (t Time) AddDate(years int, months int, days int) Time { return Time(t.Time().AddDate(years, months, days)) } func (t Time) Sub(u Time) time.Duration { return t.Time().Sub(u.Time()) } // 实现sql驱动 func (t *Time) Scan(value interface{}) error { switch s := value.(type) { case time.Time: *t = Time(s) } return nil } //实现sql驱动 func (t Time) Value() (driver.Value, error) { return t.Time(), nil } func Now() Time { t := time.Now() return Time(t) }