github.com/zhongdalu/gf@v1.0.0/g/os/gtime/gtime_z_unit_time_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_New(t *testing.T) { 12 gtest.Case(t, func() { 13 timeNow := time.Now() 14 timeTemp := gtime.New(timeNow) 15 gtest.Assert(timeTemp.Time.UnixNano(), timeNow.UnixNano()) 16 17 timeTemp1 := gtime.New() 18 gtest.Assert(timeTemp1.Time, time.Time{}) 19 }) 20 } 21 22 func Test_NewFromStr(t *testing.T) { 23 gtest.Case(t, func() { 24 timeTemp := gtime.NewFromStr("2006-01-02 15:04:05") 25 gtest.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:05") 26 27 timeTemp1 := gtime.NewFromStr("20060102") 28 if timeTemp1 != nil { 29 t.Error("test fail") 30 } 31 }) 32 } 33 34 func Test_NewFromStrFormat(t *testing.T) { 35 gtest.Case(t, func() { 36 timeTemp := gtime.NewFromStrFormat("2006-01-02 15:04:05", "Y-m-d H:i:s") 37 gtest.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:05") 38 39 timeTemp1 := gtime.NewFromStrFormat("2006-01-02 15:04:05", "aabbcc") 40 if timeTemp1 != nil { 41 t.Error("test fail") 42 } 43 }) 44 } 45 46 func Test_NewFromStrLayout(t *testing.T) { 47 gtest.Case(t, func() { 48 timeTemp := gtime.NewFromStrLayout("2006-01-02 15:04:05", "2006-01-02 15:04:05") 49 gtest.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:05") 50 51 timeTemp1 := gtime.NewFromStrLayout("2006-01-02 15:04:05", "aabbcc") 52 if timeTemp1 != nil { 53 t.Error("test fail") 54 } 55 }) 56 } 57 58 func Test_NewFromTimeStamp(t *testing.T) { 59 gtest.Case(t, func() { 60 timeTemp := gtime.NewFromTimeStamp(1554459846000) 61 gtest.Assert(timeTemp.Format("Y-m-d H:i:s"), "2019-04-05 18:24:06") 62 timeTemp1 := gtime.NewFromTimeStamp(0) 63 gtest.Assert(timeTemp1.Format("Y-m-d H:i:s"), "0001-01-01 00:00:00") 64 }) 65 } 66 67 func Test_tSecond(t *testing.T) { 68 gtest.Case(t, func() { 69 timeTemp := gtime.Now() 70 gtest.Assert(timeTemp.Second(), timeTemp.Time.Unix()) 71 }) 72 } 73 74 func Test_tNanosecond(t *testing.T) { 75 gtest.Case(t, func() { 76 timeTemp := gtime.Now() 77 gtest.Assert(timeTemp.Nanosecond(), timeTemp.Time.UnixNano()) 78 }) 79 } 80 81 func Test_tMicrosecond(t *testing.T) { 82 gtest.Case(t, func() { 83 timeTemp := gtime.Now() 84 gtest.Assert(timeTemp.Microsecond(), timeTemp.Time.UnixNano()/1e3) 85 }) 86 } 87 88 func Test_tMillisecond(t *testing.T) { 89 gtest.Case(t, func() { 90 timeTemp := gtime.Now() 91 gtest.Assert(timeTemp.Millisecond(), timeTemp.Time.UnixNano()/1e6) 92 }) 93 } 94 95 func Test_String(t *testing.T) { 96 gtest.Case(t, func() { 97 timeTemp := gtime.Now() 98 gtest.Assert(timeTemp.String(), timeTemp.Time.Format("2006-01-02 15:04:05")) 99 }) 100 } 101 102 func Test_Clone(t *testing.T) { 103 gtest.Case(t, func() { 104 timeTemp := gtime.Now() 105 timeTemp1 := timeTemp.Clone() 106 gtest.Assert(timeTemp.Time.Unix(), timeTemp1.Time.Unix()) 107 }) 108 } 109 110 func Test_ToTime(t *testing.T) { 111 gtest.Case(t, func() { 112 timeTemp := gtime.Now() 113 timeTemp1 := timeTemp.Time 114 gtest.Assert(timeTemp.ToTime().UnixNano(), timeTemp1.UnixNano()) 115 }) 116 } 117 118 func Test_Add(t *testing.T) { 119 gtest.Case(t, func() { 120 timeTemp := gtime.NewFromStr("2006-01-02 15:04:05") 121 timeTemp.Add(time.Second) 122 gtest.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:06") 123 }) 124 } 125 126 func Test_ToZone(t *testing.T) { 127 gtest.Case(t, func() { 128 timeTemp := gtime.Now() 129 // 130 timeTemp.ToZone("America/Los_Angeles") 131 gtest.Assert(timeTemp.Time.Location().String(), "America/Los_Angeles") 132 133 loc, err := time.LoadLocation("Asia/Shanghai") 134 if err != nil { 135 t.Error("test fail") 136 } 137 timeTemp.ToLocation(loc) 138 gtest.Assert(timeTemp.Time.Location().String(), "Asia/Shanghai") 139 140 timeTemp1, _ := timeTemp.ToZone("errZone") 141 if timeTemp1 != nil { 142 t.Error("test fail") 143 } 144 }) 145 } 146 147 func Test_AddDate(t *testing.T) { 148 gtest.Case(t, func() { 149 timeTemp := gtime.NewFromStr("2006-01-02 15:04:05") 150 timeTemp.AddDate(1, 2, 3) 151 gtest.Assert(timeTemp.Format("Y-m-d H:i:s"), "2007-03-05 15:04:05") 152 }) 153 } 154 155 func Test_UTC(t *testing.T) { 156 gtest.Case(t, func() { 157 timeTemp := gtime.Now() 158 timeTemp1 := timeTemp.Time 159 timeTemp.UTC() 160 gtest.Assert(timeTemp.UnixNano(), timeTemp1.UTC().UnixNano()) 161 }) 162 } 163 164 func Test_Local(t *testing.T) { 165 gtest.Case(t, func() { 166 timeTemp := gtime.Now() 167 timeTemp1 := timeTemp.Time 168 timeTemp.Local() 169 gtest.Assert(timeTemp.UnixNano(), timeTemp1.Local().UnixNano()) 170 }) 171 } 172 173 func Test_Round(t *testing.T) { 174 gtest.Case(t, func() { 175 timeTemp := gtime.Now() 176 timeTemp1 := timeTemp.Time 177 timeTemp.Round(time.Hour) 178 gtest.Assert(timeTemp.UnixNano(), timeTemp1.Round(time.Hour).UnixNano()) 179 }) 180 } 181 182 func Test_Truncate(t *testing.T) { 183 gtest.Case(t, func() { 184 timeTemp := gtime.Now() 185 timeTemp1 := timeTemp.Time 186 timeTemp.Truncate(time.Hour) 187 gtest.Assert(timeTemp.UnixNano(), timeTemp1.Truncate(time.Hour).UnixNano()) 188 }) 189 }