Files
gf/os/gtime/gtime_z_unit_basic_test.go

255 lines
6.1 KiB
Go
Raw Normal View History

2019-09-30 15:51:15 +08:00
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// 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 (
"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_SetTimeZone(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
gtime.SetTimeZone("Asia/Shanghai")
2020-03-19 22:56:12 +08:00
t.Assert(time.Local.String(), "Asia/Shanghai")
2019-04-06 22:48:47 +08:00
})
}
func Test_Nanosecond(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
nanos := gtime.TimestampNano()
2019-04-06 22:48:47 +08:00
timeTemp := time.Unix(0, nanos)
2020-03-19 22:56:12 +08:00
t.Assert(nanos, timeTemp.UnixNano())
2019-04-06 22:48:47 +08:00
})
}
func Test_Microsecond(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
micros := gtime.TimestampMicro()
2019-04-06 22:48:47 +08:00
timeTemp := time.Unix(0, micros*1e3)
2020-03-19 22:56:12 +08:00
t.Assert(micros, timeTemp.UnixNano()/1e3)
2019-04-06 22:48:47 +08:00
})
}
func Test_Millisecond(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
millis := gtime.TimestampMilli()
2019-04-06 22:48:47 +08:00
timeTemp := time.Unix(0, millis*1e6)
2020-03-19 22:56:12 +08:00
t.Assert(millis, timeTemp.UnixNano()/1e6)
2019-04-06 22:48:47 +08:00
})
}
func Test_Second(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
s := gtime.Timestamp()
2019-04-06 22:48:47 +08:00
timeTemp := time.Unix(s, 0)
2020-03-19 22:56:12 +08:00
t.Assert(s, timeTemp.Unix())
2019-04-06 22:48:47 +08:00
})
}
func Test_Date(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
t.Assert(gtime.Date(), time.Now().Format("2006-01-02"))
2019-04-06 22:48:47 +08:00
})
}
func Test_Datetime(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
datetime := gtime.Datetime()
timeTemp, err := gtime.StrToTime(datetime, "Y-m-d H:i:s")
if err != nil {
t.Error("test fail")
}
2020-03-19 22:56:12 +08:00
t.Assert(datetime, timeTemp.Time.Format("2006-01-02 15:04:05"))
2019-04-06 22:48:47 +08:00
})
}
func Test_ISO8601(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
iso8601 := gtime.ISO8601()
2020-03-19 22:56:12 +08:00
t.Assert(iso8601, gtime.Now().Format("c"))
})
}
func Test_RFC822(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
rfc822 := gtime.RFC822()
2020-03-19 22:56:12 +08:00
t.Assert(rfc822, gtime.Now().Format("r"))
})
}
2019-04-06 22:48:47 +08:00
func Test_StrToTime(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
//正常日期列表
2019-04-06 23:53:06 +08:00
//正则的原因,日期"06.01.02""2006.01""2006..01"无法覆盖gtime.go的百分百
2019-04-06 22:48:47 +08:00
var testDatetimes = []string{
"2006-01-02 15:04:05",
"2006/01/02 15:04:05",
"2006.01.02 15:04:05.000",
"2006.01.02 - 15:04:05",
"2006.01.02 15:04:05 +0800 CST",
"2006-01-02T20:05:06+05:01:01",
"2006-01-02T14:03:04Z01:01:01",
"2006-01-02T15:04:05Z",
"02-jan-2006 15:04:05",
"02/jan/2006 15:04:05",
"02.jan.2006 15:04:05",
"02.jan.2006:15:04:05",
}
for _, item := range testDatetimes {
timeTemp, err := gtime.StrToTime(item)
if err != nil {
t.Error("test fail")
}
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05")
2019-04-06 22:48:47 +08:00
}
//正常日期列表时间00:00:00
var testDates = []string{
"2006.01.02",
"2006.01.02 00:00",
"2006.01.02 00:00:00.000",
}
for _, item := range testDates {
timeTemp, err := gtime.StrToTime(item)
if err != nil {
t.Error("test fail")
}
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2006-01-02 00:00:00")
2019-04-06 22:48:47 +08:00
}
2019-04-06 23:53:06 +08:00
//测试格式化formatToStdLayout
var testDateFormats = []string{
"Y-m-d H:i:s",
"\\T\\i\\m\\e Y-m-d H:i:s",
"Y-m-d H:i:s\\",
"Y-m-j G:i:s.u",
"Y-m-j G:i:su",
}
var testDateFormatsResult = []string{
"2007-01-02 15:04:05",
"Time 2007-01-02 15:04:05",
"2007-01-02 15:04:05",
"2007-01-02 15:04:05.000",
"2007-01-02 15:04:05.000",
}
for index, item := range testDateFormats {
timeTemp, err := gtime.StrToTime(testDateFormatsResult[index], item)
if err != nil {
t.Error("test fail")
}
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05.000"), "2007-01-02 15:04:05.000")
2019-04-06 23:53:06 +08:00
}
2019-04-06 22:48:47 +08:00
//异常日期列表
var testDatesFail = []string{
"2006.01",
"06..02",
"20060102",
}
for _, item := range testDatesFail {
_, err := gtime.StrToTime(item)
if err == nil {
t.Error("test fail")
}
}
//test err
_, err := gtime.StrToTime("2006-01-02 15:04:05", "aabbccdd")
if err == nil {
t.Error("test fail")
}
})
}
2019-04-06 23:53:06 +08:00
func Test_ConvertZone(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 23:53:06 +08:00
//现行时间
nowUTC := time.Now().UTC()
testZone := "America/Los_Angeles"
//转换为洛杉矶时间
t1, err := gtime.ConvertZone(nowUTC.Format("2006-01-02 15:04:05"), testZone, "")
if err != nil {
t.Error("test fail")
}
//使用洛杉矶时区解析上面转换后的时间
laStr := t1.Time.Format("2006-01-02 15:04:05")
loc, err := time.LoadLocation(testZone)
t2, err := time.ParseInLocation("2006-01-02 15:04:05", laStr, loc)
//判断是否与现行时间匹配
2020-03-19 22:56:12 +08:00
t.Assert(t2.UTC().Unix(), nowUTC.Unix())
2019-04-06 23:53:06 +08:00
})
//test err
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 23:53:06 +08:00
//现行时间
nowUTC := time.Now().UTC()
//t.Log(nowUTC.Unix())
testZone := "errZone"
//错误时间输入
_, err := gtime.ConvertZone(nowUTC.Format("06..02 15:04:05"), testZone, "")
if err == nil {
t.Error("test fail")
}
//错误时区输入
_, err = gtime.ConvertZone(nowUTC.Format("2006-01-02 15:04:05"), testZone, "")
if err == nil {
t.Error("test fail")
}
//错误时区输入
_, err = gtime.ConvertZone(nowUTC.Format("2006-01-02 15:04:05"), testZone, testZone)
if err == nil {
t.Error("test fail")
}
})
}
func Test_StrToTimeFormat(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-04-06 23:53:06 +08:00
})
}
2019-04-06 22:48:47 +08:00
func Test_ParseTimeFromContent(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.ParseTimeFromContent("我是中文2006-01-02 15:04:05我也是中文", "Y-m-d H:i:s")
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05")
2019-04-06 22:48:47 +08:00
timeTemp1 := gtime.ParseTimeFromContent("我是中文2006-01-02 15:04:05我也是中文")
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp1.Time.Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05")
2019-04-06 22:48:47 +08:00
timeTemp2 := gtime.ParseTimeFromContent("我是中文02.jan.2006 15:04:05我也是中文")
2020-03-19 22:56:12 +08:00
t.Assert(timeTemp2.Time.Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05")
2019-04-06 22:48:47 +08:00
//test err
timeTempErr := gtime.ParseTimeFromContent("我是中文", "Y-m-d H:i:s")
if timeTempErr != nil {
t.Error("test fail")
}
})
}
func Test_FuncCost(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
gtime.FuncCost(func() {
})
})
}