github.com/go-spring/spring-base@v1.1.3/cast/time_test.go (about) 1 /* 2 * Copyright 2012-2019 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package cast_test 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/go-spring/spring-base/assert" 24 "github.com/go-spring/spring-base/cast" 25 ) 26 27 func BenchmarkToTime(b *testing.B) { 28 // string/parse-8 4266552 277 ns/op 29 // string/go-spring-8 3559998 324 ns/op 30 b.Run("string", func(b *testing.B) { 31 format := "2006-01-02 15:04:05 -0700" 32 v := time.Now().Format(format) 33 b.Run("parse", func(b *testing.B) { 34 for i := 0; i < b.N; i++ { 35 _, err := time.Parse(format, v) 36 if err != nil { 37 b.Fatal(err) 38 } 39 } 40 }) 41 b.Run("go-spring", func(b *testing.B) { 42 for i := 0; i < b.N; i++ { 43 _, err := cast.ToTimeE(v, format) 44 if err != nil { 45 b.Fatal(err) 46 } 47 } 48 }) 49 }) 50 } 51 52 func TestToTime(t *testing.T) { 53 54 assert.Equal(t, cast.ToTime(nil), time.Time{}) 55 56 assert.Equal(t, cast.ToTime(int(3)), time.Unix(0, 3)) 57 assert.Equal(t, cast.ToTime(int8(3)), time.Unix(0, 3)) 58 assert.Equal(t, cast.ToTime(int16(3)), time.Unix(0, 3)) 59 assert.Equal(t, cast.ToTime(int32(3)), time.Unix(0, 3)) 60 assert.Equal(t, cast.ToTime(int64(3)), time.Unix(0, 3)) 61 assert.Equal(t, cast.ToTime(cast.IntPtr(3)), time.Unix(0, 3)) 62 assert.Equal(t, cast.ToTime(cast.Int8Ptr(3)), time.Unix(0, 3)) 63 assert.Equal(t, cast.ToTime(cast.Int16Ptr(3)), time.Unix(0, 3)) 64 assert.Equal(t, cast.ToTime(cast.Int32Ptr(3)), time.Unix(0, 3)) 65 assert.Equal(t, cast.ToTime(cast.Int64Ptr(3)), time.Unix(0, 3)) 66 67 assert.Equal(t, cast.ToTime(uint(3), "ns"), time.Unix(0, 3)) 68 assert.Equal(t, cast.ToTime(uint8(3), "ns"), time.Unix(0, 3)) 69 assert.Equal(t, cast.ToTime(uint16(3), "ns"), time.Unix(0, 3)) 70 assert.Equal(t, cast.ToTime(uint32(3), "ns"), time.Unix(0, 3)) 71 assert.Equal(t, cast.ToTime(uint64(3), "ns"), time.Unix(0, 3)) 72 assert.Equal(t, cast.ToTime(cast.UintPtr(3), "s"), time.Unix(3, 0)) 73 assert.Equal(t, cast.ToTime(cast.Uint8Ptr(3), "s"), time.Unix(3, 0)) 74 assert.Equal(t, cast.ToTime(cast.Uint16Ptr(3), "s"), time.Unix(3, 0)) 75 assert.Equal(t, cast.ToTime(cast.Uint32Ptr(3), "s"), time.Unix(3, 0)) 76 assert.Equal(t, cast.ToTime(cast.Uint64Ptr(3), "s"), time.Unix(3, 0)) 77 78 assert.Equal(t, cast.ToTime(float32(3), "ns"), time.Unix(0, 3)) 79 assert.Equal(t, cast.ToTime(float64(3), "ns"), time.Unix(0, 3)) 80 assert.Equal(t, cast.ToTime(cast.Float32Ptr(3)), time.Unix(0, 3)) 81 assert.Equal(t, cast.ToTime(cast.Float64Ptr(3)), time.Unix(0, 3)) 82 83 assert.Equal(t, cast.ToTime("3ns"), time.Unix(0, 3)) 84 assert.Equal(t, cast.ToTime(cast.StringPtr("3ns")), time.Unix(0, 3)) 85 location, err := time.LoadLocation("Asia/Shanghai") 86 if err != nil { 87 t.Fatal(err) 88 } 89 { 90 got := cast.ToTime("2022-09-30 15:30:00 +0800") 91 expect := time.Date(2022, 9, 30, 15, 30, 0, 0, location) 92 assert.True(t, got.Equal(expect)) 93 } 94 { 95 got := cast.ToTime(cast.StringPtr("2022-09-30 15:30:00 +0800")) 96 expect := time.Date(2022, 9, 30, 15, 30, 0, 0, location) 97 assert.True(t, got.Equal(expect)) 98 } 99 { 100 got := cast.ToTime("2022-09-30 15:30:00", "2006-01-02 15:04:05") 101 expect := time.Date(2022, 9, 30, 15, 30, 0, 0, time.UTC) 102 assert.True(t, got.Equal(expect)) 103 } 104 105 now := time.Now() 106 assert.Equal(t, cast.ToTime(now), now) 107 assert.Equal(t, cast.ToTime(&now), now) 108 assert.Equal(t, cast.ToTime(now.Add(time.Hour)), now.Add(time.Hour)) 109 110 _, err = cast.ToTimeE(true) 111 assert.Error(t, err, "unable to cast type \\(bool\\) to Time") 112 113 _, err = cast.ToTimeE(false) 114 assert.Error(t, err, "unable to cast type \\(bool\\) to Time") 115 116 _, err = cast.ToTimeE(cast.BoolPtr(true)) 117 assert.Error(t, err, "unable to cast type \\(\\*bool\\) to Time") 118 119 _, err = cast.ToTimeE(cast.BoolPtr(false)) 120 assert.Error(t, err, "unable to cast type \\(\\*bool\\) to Time") 121 122 _, err = cast.ToTimeE("abc") 123 assert.Error(t, err, "cannot parse \"abc\" as \"2006\"") 124 }