gf/os/gtime/gtime_z_unit_time_test.go

387 lines
10 KiB
Go
Raw Normal View History

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-09-30 15:51:15 +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-04-06 22:48:47 +08:00
package gtime_test
import (
2020-02-16 18:07:05 +08:00
"fmt"
2019-04-06 22:48:47 +08:00
"testing"
"time"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/test/gtest"
2019-04-06 22:48:47 +08:00
)
func Test_New(t *testing.T) {
// time.Time
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeNow := time.Now()
timeTemp := gtime.New(timeNow)
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Time.UnixNano(), timeNow.UnixNano())
2019-04-06 22:48:47 +08:00
timeTemp1 := gtime.New()
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp1.Time, time.Time{})
2019-04-06 22:48:47 +08:00
})
// string
gtest.C(t, func(t *gtest.T) {
timeNow := gtime.Now()
timeTemp := gtime.New(timeNow.String())
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), timeNow.Time.Format("2006-01-02 15:04:05"))
})
gtest.C(t, func(t *gtest.T) {
timeNow := gtime.Now()
timeTemp := gtime.New(timeNow.TimestampMicroStr())
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), timeNow.Time.Format("2006-01-02 15:04:05"))
})
// int64
gtest.C(t, func(t *gtest.T) {
timeNow := gtime.Now()
timeTemp := gtime.New(timeNow.TimestampMicro())
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), timeNow.Time.Format("2006-01-02 15:04:05"))
})
2019-04-06 22:48:47 +08:00
}
2020-02-16 15:11:21 +08:00
func Test_Nil(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2020-03-19 23:53:03 +08:00
var t1 *gtime.Time
t.Assert(t1.String(), "")
2020-02-16 15:11:21 +08:00
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2020-03-19 23:53:03 +08:00
var t1 gtime.Time
t.Assert(t1.String(), "")
2020-02-16 18:07:05 +08:00
})
2020-02-16 15:11:21 +08:00
}
2019-04-06 22:48:47 +08:00
func Test_NewFromStr(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.NewFromStr("2006-01-02 15:04:05")
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:05")
2019-04-06 22:48:47 +08:00
timeTemp1 := gtime.NewFromStr("2006.0102")
2019-04-06 22:48:47 +08:00
if timeTemp1 != nil {
t.Error("test fail")
}
})
}
2020-02-16 16:25:03 +08:00
func Test_String(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2020-02-16 16:25:03 +08:00
t1 := gtime.NewFromStr("2006-01-02 15:04:05")
2020-03-19 22:56:12 +08:00
t.Assert(t1.String(), "2006-01-02 15:04:05")
t.Assert(fmt.Sprintf("%s", t1), "2006-01-02 15:04:05")
2020-02-16 16:25:03 +08:00
t2 := *t1
2020-03-19 22:56:12 +08:00
t.Assert(t2.String(), "2006-01-02 15:04:05")
t.Assert(fmt.Sprintf("{%s}", t2.String()), "{2006-01-02 15:04:05}")
2020-02-16 16:25:03 +08:00
})
}
2019-04-06 22:48:47 +08:00
func Test_NewFromStrFormat(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.NewFromStrFormat("2006-01-02 15:04:05", "Y-m-d H:i:s")
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:05")
2019-04-06 22:48:47 +08:00
timeTemp1 := gtime.NewFromStrFormat("2006-01-02 15:04:05", "aabbcc")
if timeTemp1 != nil {
t.Error("test fail")
}
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
t1 := gtime.NewFromStrFormat("2019/2/1", "Y/n/j")
2020-03-19 22:56:12 +08:00
t.Assert(t1.Format("Y-m-d"), "2019-02-01")
t2 := gtime.NewFromStrFormat("2019/10/12", "Y/n/j")
2020-03-19 22:56:12 +08:00
t.Assert(t2.Format("Y-m-d"), "2019-10-12")
})
2019-04-06 22:48:47 +08:00
}
func Test_NewFromStrLayout(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.NewFromStrLayout("2006-01-02 15:04:05", "2006-01-02 15:04:05")
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:05")
2019-04-06 22:48:47 +08:00
timeTemp1 := gtime.NewFromStrLayout("2006-01-02 15:04:05", "aabbcc")
if timeTemp1 != nil {
t.Error("test fail")
}
})
}
func Test_NewFromTimeStamp(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.NewFromTimeStamp(1554459846000)
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2019-04-05 18:24:06")
2019-04-06 22:48:47 +08:00
timeTemp1 := gtime.NewFromTimeStamp(0)
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "0001-01-01 00:00:00")
2019-04-06 22:48:47 +08:00
})
}
func Test_Time_Second(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.Now()
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Second(), timeTemp.Time.Second())
2019-04-06 22:48:47 +08:00
})
}
func Test_Time_Nanosecond(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.Now()
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Nanosecond(), timeTemp.Time.Nanosecond())
2019-04-06 22:48:47 +08:00
})
}
func Test_Time_Microsecond(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.Now()
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Microsecond(), timeTemp.Time.Nanosecond()/1e3)
2019-04-06 22:48:47 +08:00
})
}
func Test_Time_Millisecond(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.Now()
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Millisecond(), timeTemp.Time.Nanosecond()/1e6)
2019-04-06 22:48:47 +08:00
})
}
func Test_Time_String(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.Now()
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.String(), timeTemp.Time.Format("2006-01-02 15:04:05"))
2019-04-06 22:48:47 +08:00
})
}
func Test_Time_ISO8601(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
now := gtime.Now()
2020-03-19 22:56:12 +08:00
t.Assert(now.ISO8601(), now.Format("c"))
})
}
func Test_Time_RFC822(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
now := gtime.Now()
2020-03-19 22:56:12 +08:00
t.Assert(now.RFC822(), now.Format("r"))
})
}
2019-04-06 22:48:47 +08:00
func Test_Clone(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.Now()
timeTemp1 := timeTemp.Clone()
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Time.Unix(), timeTemp1.Time.Unix())
2019-04-06 22:48:47 +08:00
})
}
func Test_ToTime(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.Now()
timeTemp1 := timeTemp.Time
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Time.UnixNano(), timeTemp1.UnixNano())
2019-04-06 22:48:47 +08:00
})
}
func Test_Add(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.NewFromStr("2006-01-02 15:04:05")
2020-10-12 23:32:32 +08:00
timeTemp = timeTemp.Add(time.Second)
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:06")
2019-04-06 22:48:47 +08:00
})
}
func Test_ToZone(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.Now()
2020-10-12 23:32:32 +08:00
timeTemp, _ = timeTemp.ToZone("America/Los_Angeles")
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Time.Location().String(), "America/Los_Angeles")
2019-04-06 22:48:47 +08:00
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
t.Error("test fail")
}
2020-10-12 23:32:32 +08:00
timeTemp = timeTemp.ToLocation(loc)
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Time.Location().String(), "Asia/Shanghai")
2019-04-06 22:48:47 +08:00
timeTemp1, _ := timeTemp.ToZone("errZone")
2019-04-06 22:48:47 +08:00
if timeTemp1 != nil {
t.Error("test fail")
}
})
}
func Test_AddDate(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.NewFromStr("2006-01-02 15:04:05")
2020-10-12 23:32:32 +08:00
timeTemp = timeTemp.AddDate(1, 2, 3)
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2007-03-05 15:04:05")
2019-04-06 22:48:47 +08:00
})
}
func Test_UTC(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.Now()
timeTemp1 := timeTemp.Time
timeTemp.UTC()
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.UnixNano(), timeTemp1.UTC().UnixNano())
2019-04-06 22:48:47 +08:00
})
}
func Test_Local(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.Now()
timeTemp1 := timeTemp.Time
timeTemp.Local()
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.UnixNano(), timeTemp1.Local().UnixNano())
2019-04-06 22:48:47 +08:00
})
}
func Test_Round(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.Now()
timeTemp1 := timeTemp.Time
2020-10-12 23:32:32 +08:00
timeTemp = timeTemp.Round(time.Hour)
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.UnixNano(), timeTemp1.Round(time.Hour).UnixNano())
2019-04-06 22:48:47 +08:00
})
}
func Test_Truncate(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 22:48:47 +08:00
timeTemp := gtime.Now()
timeTemp1 := timeTemp.Time
2020-10-12 23:32:32 +08:00
timeTemp = timeTemp.Truncate(time.Hour)
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.UnixNano(), timeTemp1.Truncate(time.Hour).UnixNano())
2019-04-06 22:48:47 +08:00
})
}
2020-12-29 18:05:00 +08:00
func Test_StartOfMinute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
timeTemp1 := timeTemp.StartOfMinute()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 18:24:00")
})
}
func Test_EndOfMinute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
timeTemp1 := timeTemp.EndOfMinute()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 18:24:59")
})
}
func Test_StartOfHour(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
timeTemp1 := timeTemp.StartOfHour()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 18:00:00")
})
}
func Test_EndOfHour(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
timeTemp1 := timeTemp.EndOfHour()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 18:59:59")
})
}
func Test_StartOfDay(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
timeTemp1 := timeTemp.StartOfDay()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 00:00:00")
})
}
func Test_EndOfDay(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
timeTemp1 := timeTemp.EndOfDay()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 23:59:59")
})
}
func Test_StartOfWeek(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
timeTemp1 := timeTemp.StartOfWeek()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-06 00:00:00")
})
}
func Test_EndOfWeek(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
timeTemp1 := timeTemp.EndOfWeek()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 23:59:59")
})
}
func Test_StartOfMonth(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
timeTemp1 := timeTemp.StartOfMonth()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-01 00:00:00")
})
}
func Test_EndOfMonth(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-12 18:24:06")
timeTemp1 := timeTemp.EndOfMonth()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-31 23:59:59")
})
}
func Test_StartOfQuarter(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
timeTemp1 := timeTemp.StartOfQuarter()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-10-01 00:00:00")
})
}
func Test_EndOfQuarter(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
timeTemp1 := timeTemp.EndOfQuarter()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-31 23:59:59")
})
}
func Test_StartOfHalf(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
timeTemp1 := timeTemp.StartOfHalf()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-07-01 00:00:00")
})
}
func Test_EndOfHalf(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
timeTemp1 := timeTemp.EndOfHalf()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-31 23:59:59")
})
}
func Test_StartOfYear(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
timeTemp1 := timeTemp.StartOfYear()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-01-01 00:00:00")
})
}
func Test_EndOfYear(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeTemp := gtime.NewFromStr("2020-12-06 18:24:06")
timeTemp1 := timeTemp.EndOfYear()
t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-31 23:59:59")
})
}