提交 a0f7534f 编写于 作者: 王能's avatar 王能

Time类可自定义格式

上级 07abc356
......@@ -162,6 +162,10 @@ var (
//全局配置:保留那些空类型,也可以tag单独保留(`json:"data,keepEmpty"`)
StructKeepType = KeepEmptyBool
//全局配置:将Time转换成string时的格式,默认YY-MM-DD HH:mm:ss
//默认转换的时区是time.Local,想切换时区请修改Local
BaseTimeFormat = "2006-01-02 15:04:05"
)
const (
......@@ -497,8 +501,8 @@ func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
var err error = nil
//时间类型将格式化成YY-MM-DD HH:mm:ss
if times, ok := m.(time.Time); ok {
var buf [32]byte
b = times.In(time.Local).AppendFormat(buf[:0], "\"2006-01-02 15:04:05\"")
var buf [64]byte
b = times.In(time.Local).AppendFormat(buf[:0], "\""+BaseTimeFormat+"\"")
} else {
b, err = m.MarshalJSON()
}
......
......@@ -76,7 +76,7 @@ func Obj2Struct(obj interface{}, data reflect.Value) error {
if objKind == reflect.Slice && objValue.IsNil() {
return nil
}
length := objValue.Len();
length := objValue.Len()
for i := 0; i < length; i++ {
if i == 0 {
data.Set(reflect.MakeSlice(data.Type(), length, length))
......@@ -212,7 +212,7 @@ func Obj2Struct(obj interface{}, data reflect.Value) error {
default:
//时间类型
if times, ok := obj.(time.Time); ok {
dataSet = times.In(time.Local).Format("2006-01-02 15:04:05")
dataSet = times.In(time.Local).Format(BaseTimeFormat)
} else {
return errors.New("类型不匹配:结构体类型" + data.Kind().String() + ",数据类型" + objKind.String())
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册