gf/util/gconv/gconv_z_unit_time_test.go

79 lines
2.2 KiB
Go
Raw Normal View History

2021-01-03 23:37:45 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-01-15 21:54:34 +08:00
//
// 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,
// You can obtain one at https://github.com/gogf/gf.
2019-01-15 21:54:34 +08:00
package gconv_test
import (
2021-06-01 19:47:02 +08:00
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/frame/g"
2019-06-19 09:06:52 +08:00
"testing"
"time"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/test/gtest"
"github.com/gogf/gf/util/gconv"
2019-01-15 21:54:34 +08:00
)
func Test_Time(t *testing.T) {
2021-06-01 19:47:02 +08:00
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Duration(""), time.Duration(int64(0)))
t.AssertEQ(gconv.GTime(""), gtime.New())
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2021-01-03 23:37:45 +08:00
s := "2011-10-10 01:02:03.456"
t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s))
t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time)
2020-03-19 22:56:12 +08:00
t.AssertEQ(gconv.Duration(100), 100*time.Nanosecond)
})
2021-01-03 23:37:45 +08:00
gtest.C(t, func(t *gtest.T) {
s := "01:02:03.456"
t.AssertEQ(gconv.GTime(s).Hour(), 1)
t.AssertEQ(gconv.GTime(s).Minute(), 2)
t.AssertEQ(gconv.GTime(s).Second(), 3)
t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s))
t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time)
})
gtest.C(t, func(t *gtest.T) {
s := "0000-01-01 01:02:03"
t.AssertEQ(gconv.GTime(s).Year(), 0)
t.AssertEQ(gconv.GTime(s).Month(), 1)
t.AssertEQ(gconv.GTime(s).Day(), 1)
t.AssertEQ(gconv.GTime(s).Hour(), 1)
t.AssertEQ(gconv.GTime(s).Minute(), 2)
t.AssertEQ(gconv.GTime(s).Second(), 3)
2021-01-03 23:37:45 +08:00
t.AssertEQ(gconv.GTime(s), gtime.NewFromStr(s))
t.AssertEQ(gconv.Time(s), gtime.NewFromStr(s).Time)
})
2021-06-01 19:47:02 +08:00
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())
})
2019-01-15 21:54:34 +08:00
}
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.AssertNil(err)
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")
})
}