github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/gf.
     6  
     7  package gconv_test
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/wangyougui/gf/v2/container/gvar"
    14  	"github.com/wangyougui/gf/v2/frame/g"
    15  	"github.com/wangyougui/gf/v2/os/gtime"
    16  	"github.com/wangyougui/gf/v2/test/gtest"
    17  	"github.com/wangyougui/gf/v2/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  		t.AssertEQ(gconv.GTime(nil), nil)
    25  	})
    26  
    27  	gtest.C(t, func(t *gtest.T) {
    28  		s := "2011-10-10 01:02:03.456"
    29  		t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s))
    30  		t.AssertEQ(gconv.Time(nil), time.Time{})
    31  		t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time)
    32  		t.AssertEQ(gconv.Duration(100), 100*time.Nanosecond)
    33  	})
    34  	gtest.C(t, func(t *gtest.T) {
    35  		s := "01:02:03.456"
    36  		t.AssertEQ(gconv.GTime(s).Hour(), 1)
    37  		t.AssertEQ(gconv.GTime(s).Minute(), 2)
    38  		t.AssertEQ(gconv.GTime(s).Second(), 3)
    39  		t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s))
    40  		t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time)
    41  	})
    42  	gtest.C(t, func(t *gtest.T) {
    43  		s := "0000-01-01 01:02:03"
    44  		t.AssertEQ(gconv.GTime(s).Year(), 0)
    45  		t.AssertEQ(gconv.GTime(s).Month(), 1)
    46  		t.AssertEQ(gconv.GTime(s).Day(), 1)
    47  		t.AssertEQ(gconv.GTime(s).Hour(), 1)
    48  		t.AssertEQ(gconv.GTime(s).Minute(), 2)
    49  		t.AssertEQ(gconv.GTime(s).Second(), 3)
    50  		t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s))
    51  		t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time)
    52  	})
    53  	gtest.C(t, func(t *gtest.T) {
    54  		t1 := gtime.NewFromStr("2021-05-21 05:04:51.206547+00")
    55  		t2 := gconv.GTime(gvar.New(t1))
    56  		t3 := gvar.New(t1).GTime()
    57  		t.AssertEQ(t1, t2)
    58  		t.AssertEQ(t1.Local(), t2.Local())
    59  		t.AssertEQ(t1, t3)
    60  		t.AssertEQ(t1.Local(), t3.Local())
    61  	})
    62  }
    63  
    64  func Test_Time_Slice_Attribute(t *testing.T) {
    65  	type SelectReq struct {
    66  		Arr []*gtime.Time
    67  		One *gtime.Time
    68  	}
    69  	gtest.C(t, func(t *gtest.T) {
    70  		var s *SelectReq
    71  		err := gconv.Struct(g.Map{
    72  			"arr": g.Slice{"2021-01-12 12:34:56", "2021-01-12 12:34:57"},
    73  			"one": "2021-01-12 12:34:58",
    74  		}, &s)
    75  		t.AssertNil(err)
    76  		t.Assert(s.One, "2021-01-12 12:34:58")
    77  		t.Assert(s.Arr[0], "2021-01-12 12:34:56")
    78  		t.Assert(s.Arr[1], "2021-01-12 12:34:57")
    79  	})
    80  }