github.com/gogf/gf@v1.16.9/util/gconv/gconv_z_unit_time_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gconv_test 8 9 import ( 10 "github.com/gogf/gf/container/gvar" 11 "github.com/gogf/gf/frame/g" 12 "testing" 13 "time" 14 15 "github.com/gogf/gf/os/gtime" 16 "github.com/gogf/gf/test/gtest" 17 "github.com/gogf/gf/util/gconv" 18 ) 19 20 func Test_Time(t *testing.T) { 21 gtest.C(t, func(t *gtest.T) { 22 t.AssertEQ(gconv.Duration(""), time.Duration(int64(0))) 23 t.AssertEQ(gconv.GTime(""), gtime.New()) 24 }) 25 26 gtest.C(t, func(t *gtest.T) { 27 s := "2011-10-10 01:02:03.456" 28 t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s)) 29 t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time) 30 t.AssertEQ(gconv.Duration(100), 100*time.Nanosecond) 31 }) 32 gtest.C(t, func(t *gtest.T) { 33 s := "01:02:03.456" 34 t.AssertEQ(gconv.GTime(s).Hour(), 1) 35 t.AssertEQ(gconv.GTime(s).Minute(), 2) 36 t.AssertEQ(gconv.GTime(s).Second(), 3) 37 t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s)) 38 t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time) 39 }) 40 gtest.C(t, func(t *gtest.T) { 41 s := "0000-01-01 01:02:03" 42 t.AssertEQ(gconv.GTime(s).Year(), 0) 43 t.AssertEQ(gconv.GTime(s).Month(), 1) 44 t.AssertEQ(gconv.GTime(s).Day(), 1) 45 t.AssertEQ(gconv.GTime(s).Hour(), 1) 46 t.AssertEQ(gconv.GTime(s).Minute(), 2) 47 t.AssertEQ(gconv.GTime(s).Second(), 3) 48 t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s)) 49 t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time) 50 }) 51 gtest.C(t, func(t *gtest.T) { 52 t1 := gtime.NewFromStr("2021-05-21 05:04:51.206547+00") 53 t2 := gconv.GTime(gvar.New(t1)) 54 t3 := gvar.New(t1).GTime() 55 t.AssertEQ(t1, t2) 56 t.AssertEQ(t1.Local(), t2.Local()) 57 t.AssertEQ(t1, t3) 58 t.AssertEQ(t1.Local(), t3.Local()) 59 }) 60 } 61 62 func Test_Time_Slice_Attribute(t *testing.T) { 63 type SelectReq struct { 64 Arr []*gtime.Time 65 One *gtime.Time 66 } 67 gtest.C(t, func(t *gtest.T) { 68 var s *SelectReq 69 err := gconv.Struct(g.Map{ 70 "arr": g.Slice{"2021-01-12 12:34:56", "2021-01-12 12:34:57"}, 71 "one": "2021-01-12 12:34:58", 72 }, &s) 73 t.AssertNil(err) 74 t.Assert(s.One, "2021-01-12 12:34:58") 75 t.Assert(s.Arr[0], "2021-01-12 12:34:56") 76 t.Assert(s.Arr[1], "2021-01-12 12:34:57") 77 }) 78 }