1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package utils
import ( "time" //"strconv" "fmt" "database/sql/driver" "strconv" )
type LocalTime struct { time.Time }
func (t LocalTime) MarshalJSON() ([]byte, error) { //格式化秒 seconds := t.Unix() return []byte(strconv.FormatInt(seconds, 10)), nil }
func (t LocalTime) Value() (driver.Value, error) { var zeroTime time.Time if t.Time.UnixNano() == zeroTime.UnixNano() { return nil, nil } return t.Time, nil }
func (t *LocalTime) Scan(v interface{}) error { value, ok := v.(time.Time) if ok { *t = LocalTime{Time: value} return nil } return fmt.Errorf("can not convert %v to timestamp", v) }
|