Skip to content

Commit

Permalink
fix issue gogf#1256
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Jun 1, 2021
1 parent 392c81a commit 3e6b986
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
7 changes: 7 additions & 0 deletions util/gconv/gconv_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package gconv

import "github.com/gogf/gf/os/gtime"

// apiString is used for type assert api for String().
type apiString interface {
String() string
Expand Down Expand Up @@ -92,3 +94,8 @@ type apiUnmarshalText interface {
type apiSet interface {
Set(value interface{}) (old interface{})
}

// apiGTime is the interface for gtime.Time converting.
type apiGTime interface {
GTime(format ...string) *gtime.Time
}
9 changes: 9 additions & 0 deletions util/gconv/gconv_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,20 @@ func GTime(any interface{}, format ...string) *gtime.Time {
if any == nil {
return nil
}
if v, ok := any.(apiGTime); ok {
return v.GTime(format...)
}
// It's already this type.
if len(format) == 0 {
if v, ok := any.(*gtime.Time); ok {
return v
}
if t, ok := any.(time.Time); ok {
return gtime.New(t)
}
if t, ok := any.(*time.Time); ok {
return gtime.New(t)
}
}
s := String(any)
if len(s) == 0 {
Expand Down
7 changes: 0 additions & 7 deletions util/gconv/gconv_z_unit_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,13 +647,6 @@ func Test_Convert_All(t *testing.T) {
})
}

func Test_Time_All(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Duration(""), time.Duration(int64(0)))
t.AssertEQ(gconv.GTime(""), gtime.New())
})
}

func Test_Slice_All(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
value := 123.456
Expand Down
15 changes: 15 additions & 0 deletions util/gconv/gconv_z_unit_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package gconv_test

import (
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/frame/g"
"testing"
"time"
Expand All @@ -17,6 +18,11 @@ import (
)

func Test_Time(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Duration(""), time.Duration(int64(0)))
t.AssertEQ(gconv.GTime(""), gtime.New())
})

gtest.C(t, func(t *gtest.T) {
s := "2011-10-10 01:02:03.456"
t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s))
Expand All @@ -42,6 +48,15 @@ func Test_Time(t *testing.T) {
t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s))
t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time)
})
gtest.C(t, func(t *gtest.T) {
t1 := gtime.NewFromStr("2021-05-21 05:04:51.206547+00")
t2 := gconv.GTime(gvar.New(t1))
t3 := gvar.New(t1).GTime()
t.AssertEQ(t1, t2)
t.AssertEQ(t1.Local(), t2.Local())
t.AssertEQ(t1, t3)
t.AssertEQ(t1.Local(), t3.Local())
})
}

func Test_Time_Slice_Attribute(t *testing.T) {
Expand Down

0 comments on commit 3e6b986

Please sign in to comment.