// Package util ----------------------------- // @file : copierOptions.go // @author : JJXu // @contact : wavingbear@163.com // @time : 2023/8/18 12:00 // ------------------------------------------- package utils import ( "errors" "github.com/jinzhu/copier" "time" ) var CopierProtoOptions = copier.Option{ IgnoreEmpty: true, DeepCopy: true, Converters: []copier.TypeConverter{ { SrcType: time.Time{}, DstType: copier.String, Fn: func(src interface{}) (interface{}, error) { s, ok := src.(time.Time) if !ok { return nil, errors.New("src type :time.Time not matching") } return s.Format("2006-01-02 15:04:05"), nil }, }, { SrcType: copier.String, DstType: time.Time{}, Fn: func(src interface{}) (interface{}, error) { s, ok := src.(string) if !ok { return nil, errors.New("src type :time.Time not matching") } tt, err := StringToTime(s, "2006-01-02 15:04:05") return *tt, err }, }, }, } func StringToTime(strTime string, timeFormat string) (*time.Time, error) { timeObj, err := time.ParseInLocation(timeFormat, strTime, LocShanghai()) return &timeObj, err } func LocShanghai() *time.Location { var shanghai, err = time.LoadLocation("Asia/Shanghai") if err != nil { shanghai = time.FixedZone("CST", 8*3600) } return shanghai }