github.com/zhongdalu/gf@v1.0.0/g/os/gtime/gtime_z_unit_basic_test.go (about) 1 package gtime_test 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/zhongdalu/gf/g/os/gtime" 8 "github.com/zhongdalu/gf/g/test/gtest" 9 ) 10 11 func Test_SetTimeZone(t *testing.T) { 12 gtest.Case(t, func() { 13 gtime.SetTimeZone("Asia/Shanghai") 14 gtest.Assert(time.Local.String(), "Asia/Shanghai") 15 }) 16 } 17 18 func Test_Nanosecond(t *testing.T) { 19 gtest.Case(t, func() { 20 nanos := gtime.Nanosecond() 21 timeTemp := time.Unix(0, nanos) 22 gtest.Assert(nanos, timeTemp.UnixNano()) 23 }) 24 } 25 26 func Test_Microsecond(t *testing.T) { 27 gtest.Case(t, func() { 28 micros := gtime.Microsecond() 29 timeTemp := time.Unix(0, micros*1e3) 30 gtest.Assert(micros, timeTemp.UnixNano()/1e3) 31 }) 32 } 33 34 func Test_Millisecond(t *testing.T) { 35 gtest.Case(t, func() { 36 millis := gtime.Millisecond() 37 timeTemp := time.Unix(0, millis*1e6) 38 gtest.Assert(millis, timeTemp.UnixNano()/1e6) 39 }) 40 } 41 42 func Test_Second(t *testing.T) { 43 gtest.Case(t, func() { 44 s := gtime.Second() 45 timeTemp := time.Unix(s, 0) 46 gtest.Assert(s, timeTemp.Unix()) 47 }) 48 } 49 50 func Test_Date(t *testing.T) { 51 gtest.Case(t, func() { 52 gtest.Assert(gtime.Date(), time.Now().Format("2006-01-02")) 53 }) 54 } 55 56 func Test_Datetime(t *testing.T) { 57 gtest.Case(t, func() { 58 datetime := gtime.Datetime() 59 timeTemp, err := gtime.StrToTime(datetime, "Y-m-d H:i:s") 60 if err != nil { 61 t.Error("test fail") 62 } 63 gtest.Assert(datetime, timeTemp.Time.Format("2006-01-02 15:04:05")) 64 }) 65 } 66 67 func Test_StrToTime(t *testing.T) { 68 gtest.Case(t, func() { 69 //正常日期列表 70 //正则的原因,日期"06.01.02","2006.01","2006..01"无法覆盖gtime.go的百分百 71 var testDatetimes = []string{ 72 "2006-01-02 15:04:05", 73 "2006/01/02 15:04:05", 74 "2006.01.02 15:04:05.000", 75 "2006.01.02 - 15:04:05", 76 "2006.01.02 15:04:05 +0800 CST", 77 "2006-01-02T20:05:06+05:01:01", 78 "2006-01-02T14:03:04Z01:01:01", 79 "2006-01-02T15:04:05Z", 80 "02-jan-2006 15:04:05", 81 "02/jan/2006 15:04:05", 82 "02.jan.2006 15:04:05", 83 "02.jan.2006:15:04:05", 84 } 85 86 for _, item := range testDatetimes { 87 timeTemp, err := gtime.StrToTime(item) 88 if err != nil { 89 t.Error("test fail") 90 } 91 gtest.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05") 92 } 93 94 //正常日期列表,时间00:00:00 95 var testDates = []string{ 96 "2006.01.02", 97 "2006.01.02 00:00", 98 "2006.01.02 00:00:00.000", 99 } 100 101 for _, item := range testDates { 102 timeTemp, err := gtime.StrToTime(item) 103 if err != nil { 104 t.Error("test fail") 105 } 106 gtest.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2006-01-02 00:00:00") 107 } 108 109 //测试格式化formatToStdLayout 110 var testDateFormats = []string{ 111 "Y-m-d H:i:s", 112 "\\T\\i\\m\\e Y-m-d H:i:s", 113 "Y-m-d H:i:s\\", 114 "Y-m-j G:i:s.u", 115 "Y-m-j G:i:su", 116 } 117 118 var testDateFormatsResult = []string{ 119 "2007-01-02 15:04:05", 120 "Time 2007-01-02 15:04:05", 121 "2007-01-02 15:04:05", 122 "2007-01-02 15:04:05.000", 123 "2007-01-02 15:04:05.000", 124 } 125 126 for index, item := range testDateFormats { 127 timeTemp, err := gtime.StrToTime(testDateFormatsResult[index], item) 128 if err != nil { 129 t.Error("test fail") 130 } 131 gtest.Assert(timeTemp.Time.Format("2006-01-02 15:04:05.000"), "2007-01-02 15:04:05.000") 132 } 133 134 //异常日期列表 135 var testDatesFail = []string{ 136 "2006.01", 137 "06..02", 138 "20060102", 139 } 140 141 for _, item := range testDatesFail { 142 _, err := gtime.StrToTime(item) 143 if err == nil { 144 t.Error("test fail") 145 } 146 } 147 148 //test err 149 _, err := gtime.StrToTime("2006-01-02 15:04:05", "aabbccdd") 150 if err == nil { 151 t.Error("test fail") 152 } 153 }) 154 } 155 156 func Test_ConvertZone(t *testing.T) { 157 gtest.Case(t, func() { 158 //现行时间 159 nowUTC := time.Now().UTC() 160 testZone := "America/Los_Angeles" 161 162 //转换为洛杉矶时间 163 t1, err := gtime.ConvertZone(nowUTC.Format("2006-01-02 15:04:05"), testZone, "") 164 if err != nil { 165 t.Error("test fail") 166 } 167 168 //使用洛杉矶时区解析上面转换后的时间 169 laStr := t1.Time.Format("2006-01-02 15:04:05") 170 loc, err := time.LoadLocation(testZone) 171 t2, err := time.ParseInLocation("2006-01-02 15:04:05", laStr, loc) 172 173 //判断是否与现行时间匹配 174 gtest.Assert(t2.UTC().Unix(), nowUTC.Unix()) 175 176 }) 177 178 //test err 179 gtest.Case(t, func() { 180 //现行时间 181 nowUTC := time.Now().UTC() 182 //t.Log(nowUTC.Unix()) 183 testZone := "errZone" 184 185 //错误时间输入 186 _, err := gtime.ConvertZone(nowUTC.Format("06..02 15:04:05"), testZone, "") 187 if err == nil { 188 t.Error("test fail") 189 } 190 //错误时区输入 191 _, err = gtime.ConvertZone(nowUTC.Format("2006-01-02 15:04:05"), testZone, "") 192 if err == nil { 193 t.Error("test fail") 194 } 195 //错误时区输入 196 _, err = gtime.ConvertZone(nowUTC.Format("2006-01-02 15:04:05"), testZone, testZone) 197 if err == nil { 198 t.Error("test fail") 199 } 200 }) 201 } 202 203 func Test_StrToTimeFormat(t *testing.T) { 204 gtest.Case(t, func() { 205 206 }) 207 } 208 209 func Test_ParseTimeFromContent(t *testing.T) { 210 gtest.Case(t, func() { 211 timeTemp := gtime.ParseTimeFromContent("我是中文2006-01-02 15:04:05我也是中文", "Y-m-d H:i:s") 212 gtest.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05") 213 214 timeTemp1 := gtime.ParseTimeFromContent("我是中文2006-01-02 15:04:05我也是中文") 215 gtest.Assert(timeTemp1.Time.Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05") 216 217 timeTemp2 := gtime.ParseTimeFromContent("我是中文02.jan.2006 15:04:05我也是中文") 218 gtest.Assert(timeTemp2.Time.Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05") 219 220 //test err 221 timeTempErr := gtime.ParseTimeFromContent("我是中文", "Y-m-d H:i:s") 222 if timeTempErr != nil { 223 t.Error("test fail") 224 } 225 }) 226 } 227 228 func Test_FuncCost(t *testing.T) { 229 gtest.Case(t, func() { 230 gtime.FuncCost(func() { 231 232 }) 233 }) 234 }