想让go返回的时间是"YYYY-MM-DD HH:ii:ss"的格式,但是他始终返回为2006-01-02T15:04:05Z07:00。
查看源代码后发现,go的时间format里面封装了下面这些时间格式
const ( ANSIC = "Mon Jan _2 15:04:05 2006" UnixDate = "Mon Jan _2 15:04:05 MST 2006" RubyDate = "Mon Jan 02 15:04:05 -0700 2006" RFC822 = "02 Jan 06 15:04 MST" RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone RFC850 = "Monday, 02-Jan-06 15:04:05 MST" RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST" RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone RFC3339 = "2006-01-02T15:04:05Z07:00" RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" Kitchen = "3:04PM" // Handy time stamps. Stamp = "Jan _2 15:04:05" StampMilli = "Jan _2 15:04:05.000" StampMicro = "Jan _2 15:04:05.000000" StampNano = "Jan _2 15:04:05.000000000" )
没有发现我需要的时间格式,始终返回RFC3339格式的2006-01-02T15:04:05Z07:00格式
查看了json对于时间的格式化方法,实现下面的接口
// JSONTime format json time field by myself type JSONTime struct { time.Time } // MarshalJSON on JSONTime format Time field with %Y-%m-%d %H:%M:%S func (t JSONTime) MarshalJSON() ([]byte, error) { formatted := fmt.Sprintf("\"%s\"", t.Format("2006-01-02 15:04:05")) return []byte(formatted), nil } // Value insert timestamp into mysql need this function. func (t JSONTime) Value() (driver.Value, error) { var zeroTime time.Time if t.Time.UnixNano() == zeroTime.UnixNano() { return nil, nil } return t.Time, nil } // Scan valueof time.Time func (t *JSONTime) Scan(v interface{}) error { value, ok := v.(time.Time) if ok { *t = JSONTime{Time: value} return nil } return fmt.Errorf("can not convert %v to timestamp", v) }