fix issue in gconv.Struct when given atrr of struct is nil time.Time

This commit is contained in:
John
2019-01-14 09:00:31 +08:00
parent 602592a354
commit 9415419324
4 changed files with 19 additions and 12 deletions

View File

@ -62,6 +62,9 @@ func NewFromStrLayout (str string, layout string) *Time {
// 时间戳转换为时间对象,时间戳支持到纳秒的数值
func NewFromTimeStamp (timestamp int64) *Time {
if timestamp == 0 {
return &Time {}
}
for timestamp < 1e18 {
timestamp *= 10
}

View File

@ -25,6 +25,9 @@ func TimeDuration(i interface{}) time.Duration {
// 将变量i转换为time.Time类型
func GTime(i interface{}, format...string) *gtime.Time {
s := String(i)
if len(s) == 0 {
return gtime.New()
}
// 优先使用用户输入日期格式进行转换
if len(format) > 0 {
t, _ := gtime.StrToTimeFormat(s, format[0])

View File

@ -96,6 +96,10 @@ func IsLetterUpper(b byte) bool {
// 判断锁给字符串是否为数字
func IsNumeric(s string) bool {
length := len(s)
if length == 0 {
return false
}
for i := 0; i < len(s); i++ {
if s[i] < byte('0') || s[i] > byte('9') {
return false

View File

@ -1,22 +1,19 @@
package main
import (
"container/list"
"gitee.com/johng/gf/g/os/glog"
"fmt"
"gitee.com/johng/gf/g/util/gconv"
"time"
)
func main(){
list := list.New()
glog.Println("start1")
for i := 0; i < 10000000; i++ {
list.PushBack(i)
type Test struct {
Date time.Time `json:"date"`
}
glog.Println("end1")
glog.Println("start2")
for e := list.Front(); e != nil; e = e.Next() {
time.Sleep(25*time.Nanosecond)
o := new(Test)
m := map[string]interface{}{
"Date" : "",
}
glog.Println("end2")
gconv.Struct(m, o)
fmt.Println(o)
}