github.com/wangyougui/gf/v2@v2.6.5/os/gtime/gtime_z_unit_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 gtime_test 8 9 import ( 10 "testing" 11 "time" 12 13 "github.com/wangyougui/gf/v2/frame/g" 14 "github.com/wangyougui/gf/v2/os/gtime" 15 "github.com/wangyougui/gf/v2/test/gtest" 16 ) 17 18 func Test_TimestampStr(t *testing.T) { 19 gtest.C(t, func(t *gtest.T) { 20 t.AssertGT(len(gtime.TimestampMilliStr()), 0) 21 t.AssertGT(len(gtime.TimestampMicroStr()), 0) 22 t.AssertGT(len(gtime.TimestampNanoStr()), 0) 23 }) 24 } 25 26 func Test_Nanosecond(t *testing.T) { 27 gtest.C(t, func(t *gtest.T) { 28 nanos := gtime.TimestampNano() 29 timeTemp := time.Unix(0, nanos) 30 t.Assert(nanos, timeTemp.UnixNano()) 31 }) 32 } 33 34 func Test_Microsecond(t *testing.T) { 35 gtest.C(t, func(t *gtest.T) { 36 micros := gtime.TimestampMicro() 37 timeTemp := time.Unix(0, micros*1e3) 38 t.Assert(micros, timeTemp.UnixNano()/1e3) 39 }) 40 } 41 42 func Test_Millisecond(t *testing.T) { 43 gtest.C(t, func(t *gtest.T) { 44 millis := gtime.TimestampMilli() 45 timeTemp := time.Unix(0, millis*1e6) 46 t.Assert(millis, timeTemp.UnixNano()/1e6) 47 }) 48 } 49 50 func Test_Second(t *testing.T) { 51 gtest.C(t, func(t *gtest.T) { 52 s := gtime.Timestamp() 53 timeTemp := time.Unix(s, 0) 54 t.Assert(s, timeTemp.Unix()) 55 }) 56 } 57 58 func Test_Date(t *testing.T) { 59 gtest.C(t, func(t *gtest.T) { 60 t.Assert(gtime.Date(), time.Now().Format("2006-01-02")) 61 }) 62 } 63 64 func Test_Datetime(t *testing.T) { 65 gtest.C(t, func(t *gtest.T) { 66 datetime := gtime.Datetime() 67 timeTemp, err := gtime.StrToTime(datetime, "Y-m-d H:i:s") 68 if err != nil { 69 t.Error("test fail") 70 } 71 t.Assert(datetime, timeTemp.Time.Format("2006-01-02 15:04:05")) 72 }) 73 gtest.C(t, func(t *gtest.T) { 74 timeTemp, err := gtime.StrToTime("") 75 t.AssertNil(err) 76 t.AssertLT(timeTemp.Unix(), 0) 77 timeTemp, err = gtime.StrToTime("2006-01") 78 t.AssertNE(err, nil) 79 t.Assert(timeTemp, nil) 80 }) 81 } 82 83 func Test_ISO8601(t *testing.T) { 84 gtest.C(t, func(t *gtest.T) { 85 iso8601 := gtime.ISO8601() 86 t.Assert(iso8601, gtime.Now().Format("c")) 87 }) 88 } 89 90 func Test_RFC822(t *testing.T) { 91 gtest.C(t, func(t *gtest.T) { 92 rfc822 := gtime.RFC822() 93 t.Assert(rfc822, gtime.Now().Format("r")) 94 }) 95 } 96 97 func Test_StrToTime(t *testing.T) { 98 gtest.C(t, func(t *gtest.T) { 99 // Correct datetime string. 100 var testDateTimes = []string{ 101 "2006-01-02 15:04:05", 102 "2006/01/02 15:04:05", 103 "2006.01.02 15:04:05.000", 104 "2006.01.02 - 15:04:05", 105 "2006.01.02 15:04:05 +0800 CST", 106 "2006-01-02T20:05:06+05:01:01", 107 "2006-01-02T14:03:04Z01:01:01", 108 "2006-01-02T15:04:05Z", 109 "02-jan-2006 15:04:05", 110 "02/jan/2006 15:04:05", 111 "02.jan.2006 15:04:05", 112 "02.jan.2006:15:04:05", 113 } 114 115 for _, item := range testDateTimes { 116 timeTemp, err := gtime.StrToTime(item) 117 t.AssertNil(err) 118 t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05") 119 } 120 121 // Correct date string,. 122 var testDates = []string{ 123 "2006.01.02", 124 "2006.01.02 00:00", 125 "2006.01.02 00:00:00.000", 126 } 127 128 for _, item := range testDates { 129 timeTemp, err := gtime.StrToTime(item) 130 t.AssertNil(err) 131 t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2006-01-02 00:00:00") 132 } 133 134 // Correct time string. 135 var testTimes = g.MapStrStr{ 136 "16:12:01": "15:04:05", 137 "16:12:01.789": "15:04:05.000", 138 } 139 140 for k, v := range testTimes { 141 time1, err := gtime.StrToTime(k) 142 t.AssertNil(err) 143 time2, err := time.ParseInLocation(v, k, time.Local) 144 t.AssertNil(err) 145 t.Assert(time1.Time, time2) 146 } 147 148 // formatToStdLayout 149 var testDateFormats = []string{ 150 "Y-m-d H:i:s", 151 "\\T\\i\\m\\e Y-m-d H:i:s", 152 "Y-m-d H:i:s\\", 153 "Y-m-j G:i:s.u", 154 "Y-m-j G:i:su", 155 } 156 157 var testDateFormatsResult = []string{ 158 "2007-01-02 15:04:05", 159 "Time 2007-01-02 15:04:05", 160 "2007-01-02 15:04:05", 161 "2007-01-02 15:04:05.000", 162 "2007-01-02 15:04:05.000", 163 } 164 165 for index, item := range testDateFormats { 166 timeTemp, err := gtime.StrToTime(testDateFormatsResult[index], item) 167 if err != nil { 168 t.Error("test fail") 169 } 170 t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05.000"), "2007-01-02 15:04:05.000") 171 } 172 173 // 异常日期列表 174 var testDatesFail = []string{ 175 "2006.01", 176 "06..02", 177 } 178 179 for _, item := range testDatesFail { 180 _, err := gtime.StrToTime(item) 181 if err == nil { 182 t.Error("test fail") 183 } 184 } 185 186 // test err 187 _, err := gtime.StrToTime("2006-01-02 15:04:05", "aabbccdd") 188 if err == nil { 189 t.Error("test fail") 190 } 191 }) 192 } 193 194 func Test_ConvertZone(t *testing.T) { 195 gtest.C(t, func(t *gtest.T) { 196 // 现行时间 197 nowUTC := time.Now().UTC() 198 testZone := "America/Los_Angeles" 199 200 // 转换为洛杉矶时间 201 t1, err := gtime.ConvertZone(nowUTC.Format("2006-01-02 15:04:05"), testZone, "") 202 if err != nil { 203 t.Error("test fail") 204 } 205 206 // 使用洛杉矶时区解析上面转换后的时间 207 laStr := t1.Time.Format("2006-01-02 15:04:05") 208 loc, err := time.LoadLocation(testZone) 209 t2, err := time.ParseInLocation("2006-01-02 15:04:05", laStr, loc) 210 211 // 判断是否与现行时间匹配 212 t.Assert(t2.UTC().Unix(), nowUTC.Unix()) 213 214 }) 215 216 // test err 217 gtest.C(t, func(t *gtest.T) { 218 // 现行时间 219 nowUTC := time.Now().UTC() 220 // t.Log(nowUTC.Unix()) 221 testZone := "errZone" 222 223 // 错误时间输入 224 _, err := gtime.ConvertZone(nowUTC.Format("06..02 15:04:05"), testZone, "") 225 if err == nil { 226 t.Error("test fail") 227 } 228 // 错误时区输入 229 _, err = gtime.ConvertZone(nowUTC.Format("2006-01-02 15:04:05"), testZone, "") 230 if err == nil { 231 t.Error("test fail") 232 } 233 // 错误时区输入 234 _, err = gtime.ConvertZone(nowUTC.Format("2006-01-02 15:04:05"), testZone, testZone) 235 if err == nil { 236 t.Error("test fail") 237 } 238 }) 239 } 240 241 func Test_ParseDuration(t *testing.T) { 242 gtest.C(t, func(t *gtest.T) { 243 d, err := gtime.ParseDuration("1d") 244 t.AssertNil(err) 245 t.Assert(d.String(), "24h0m0s") 246 }) 247 gtest.C(t, func(t *gtest.T) { 248 d, err := gtime.ParseDuration("1d2h3m") 249 t.AssertNil(err) 250 t.Assert(d.String(), "26h3m0s") 251 }) 252 gtest.C(t, func(t *gtest.T) { 253 d, err := gtime.ParseDuration("-1d2h3m") 254 t.AssertNil(err) 255 t.Assert(d.String(), "-26h3m0s") 256 }) 257 gtest.C(t, func(t *gtest.T) { 258 d, err := gtime.ParseDuration("3m") 259 t.AssertNil(err) 260 t.Assert(d.String(), "3m0s") 261 }) 262 // error 263 gtest.C(t, func(t *gtest.T) { 264 d, err := gtime.ParseDuration("-1dd2h3m") 265 t.AssertNE(err, nil) 266 t.Assert(d.String(), "0s") 267 }) 268 } 269 270 func Test_ParseTimeFromContent(t *testing.T) { 271 gtest.C(t, func(t *gtest.T) { 272 timeTemp := gtime.ParseTimeFromContent("我是中文2006-01-02 15:04:05我也是中文", "Y-m-d H:i:s") 273 t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05") 274 275 timeTemp1 := gtime.ParseTimeFromContent("我是中文2006-01-02 15:04:05我也是中文") 276 t.Assert(timeTemp1.Time.Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05") 277 278 timeTemp2 := gtime.ParseTimeFromContent("我是中文02.jan.2006 15:04:05我也是中文") 279 t.Assert(timeTemp2.Time.Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05") 280 281 // test err 282 timeTempErr := gtime.ParseTimeFromContent("我是中文", "Y-m-d H:i:s") 283 if timeTempErr != nil { 284 t.Error("test fail") 285 } 286 }) 287 288 gtest.C(t, func(t *gtest.T) { 289 timeStr := "2021-1-27 9:10:24" 290 t.Assert(gtime.ParseTimeFromContent(timeStr, "Y-n-d g:i:s").String(), "2021-01-27 09:10:24") 291 }) 292 } 293 294 func Test_FuncCost(t *testing.T) { 295 gtest.C(t, func(t *gtest.T) { 296 gtime.FuncCost(func() { 297 298 }) 299 }) 300 }