improve time converting for package gconv

This commit is contained in:
John Guo
2021-01-17 21:39:17 +08:00
parent 5c9cee7fb9
commit 6f968a125e
4 changed files with 31 additions and 10 deletions

View File

@ -8,6 +8,7 @@ package gtime
import (
"bytes"
"github.com/gogf/gf/errors/gerror"
"strconv"
"time"
)
@ -444,11 +445,13 @@ func (t *Time) UnmarshalJSON(b []byte) error {
return nil
}
//// UnmarshalValue is an interface implement which sets any type of value for Time.
//func (t *Time) UnmarshalValue(value interface{}) error {
// vTime := New(value)
// if vTime != nil {
// *t = *vTime
// }
// return gerror.Newf(`invalid time value: %v`, value)
//}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
// Note that it overwrites the same implementer of `time.Time`.
func (t *Time) UnmarshalText(data []byte) error {
vTime := New(data)
if vTime != nil {
*t = *vTime
return nil
}
return gerror.Newf(`invalid time value: %s`, data)
}

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,

View File

@ -131,7 +131,6 @@ func doStruct(params interface{}, pointer interface{}, mapping ...map[string]str
e := reflect.New(pointerElemReflectValue.Type().Elem()).Elem()
pointerElemReflectValue.Set(e.Addr())
}
fmt.Println(pointerElemReflectValue.Type())
//if v, ok := pointerElemReflectValue.Interface().(apiUnmarshalValue); ok {
// return v.UnmarshalValue(params)
//}

View File

@ -7,6 +7,7 @@
package gconv_test
import (
"github.com/gogf/gf/frame/g"
"testing"
"time"
@ -42,3 +43,21 @@ func Test_Time(t *testing.T) {
t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time)
})
}
func Test_Time_Slice_Attribute(t *testing.T) {
type SelectReq struct {
Arr []*gtime.Time
One *gtime.Time
}
gtest.C(t, func(t *gtest.T) {
var s *SelectReq
err := gconv.Struct(g.Map{
"arr": g.Slice{"2021-01-12 12:34:56", "2021-01-12 12:34:57"},
"one": "2021-01-12 12:34:58",
}, &s)
t.Assert(err, nil)
t.Assert(s.One, "2021-01-12 12:34:58")
t.Assert(s.Arr[0], "2021-01-12 12:34:56")
t.Assert(s.Arr[1], "2021-01-12 12:34:57")
})
}