github.com/m3db/m3@v1.5.0/src/x/time/time_test.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package time 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestToNormalizedTime(t *testing.T) { 31 inputs := []struct { 32 t time.Time 33 u time.Duration 34 expected int64 35 }{ 36 {time.Unix(1, 0), time.Second, 1}, 37 {time.Unix(0, 150000000), time.Millisecond, 150}, 38 {time.Unix(0, 150000000), time.Microsecond, 150000}, 39 {time.Unix(0, 150000000), time.Nanosecond, 150000000}, 40 {time.Unix(0, 150000000), time.Second, 0}, 41 } 42 for _, input := range inputs { 43 require.Equal(t, input.expected, ToNormalizedTime(input.t, input.u)) 44 } 45 } 46 47 func TestFromNormalizedTime(t *testing.T) { 48 inputs := []struct { 49 nt int64 50 u time.Duration 51 expected time.Time 52 }{ 53 {1, time.Second, time.Unix(1, 0)}, 54 {150, time.Millisecond, time.Unix(0, 150000000)}, 55 {150000, time.Microsecond, time.Unix(0, 150000000)}, 56 {150000000, time.Nanosecond, time.Unix(0, 150000000)}, 57 {60100, time.Millisecond, time.Unix(60, 100000000)}, 58 } 59 for _, input := range inputs { 60 exTime := ToUnixNano(input.expected) 61 require.Equal(t, exTime, FromNormalizedTime(input.nt, input.u)) 62 } 63 } 64 65 func TestToNormalizedDuration(t *testing.T) { 66 inputs := []struct { 67 d time.Duration 68 u time.Duration 69 expected int64 70 }{ 71 {60 * time.Second, time.Second, 60}, 72 {60 * time.Second, time.Millisecond, 60000}, 73 {60100 * time.Millisecond, time.Second, 60}, 74 } 75 for _, input := range inputs { 76 require.Equal(t, input.expected, ToNormalizedDuration(input.d, input.u)) 77 } 78 } 79 80 func TestFromNormalizedDuration(t *testing.T) { 81 inputs := []struct { 82 nd int64 83 u time.Duration 84 expected time.Duration 85 }{ 86 {60, time.Second, time.Minute}, 87 {60100, time.Millisecond, 60100 * time.Millisecond}, 88 {1000000000, time.Nanosecond, time.Second}, 89 } 90 for _, input := range inputs { 91 require.Equal(t, input.expected, FromNormalizedDuration(input.nd, input.u)) 92 } 93 } 94 95 func TestToNanoseconds(t *testing.T) { 96 input := time.Unix(1, 100000000) 97 require.Equal(t, int64(1100000000), ToNanoseconds(input)) 98 } 99 100 func TestFromNanoseconds(t *testing.T) { 101 expected := time.Unix(1, 100000000) 102 require.Equal(t, expected, FromNanoseconds(1100000000)) 103 } 104 105 var testTime = time.Date(2015, 5, 21, 18, 17, 46, 0, time.UTC) 106 107 func TestToUnixMillis(t *testing.T) { 108 ms := ToUnixMillis(testTime) 109 require.Equal(t, int64(1432232266000), ms) 110 } 111 112 func TestFromUnixMillis(t *testing.T) { 113 tm := FromUnixMillis(1432232266000).UTC() 114 require.Equal(t, testTime, tm) 115 } 116 117 func TestCeil(t *testing.T) { 118 var timeZero time.Time 119 inputs := []struct { 120 t time.Time 121 d time.Duration 122 expected time.Time 123 }{ 124 {timeZero.Add(2 * time.Hour), time.Hour, timeZero.Add(2 * time.Hour)}, 125 {timeZero.Add(2 * time.Hour), 2 * time.Hour, timeZero.Add(2 * time.Hour)}, 126 {timeZero.Add(15 * time.Minute), 2 * time.Hour, timeZero.Add(2 * time.Hour)}, 127 } 128 for _, input := range inputs { 129 require.Equal(t, input.expected, Ceil(input.t, input.d)) 130 } 131 } 132 133 func TestMinTime(t *testing.T) { 134 inputs := []struct { 135 t1 time.Time 136 t2 time.Time 137 expected time.Time 138 }{ 139 {time.Unix(10, 0), time.Unix(20, 0), time.Unix(10, 0)}, 140 {time.Unix(20, 0), time.Unix(10, 0), time.Unix(10, 0)}, 141 {time.Unix(10, 0), time.Unix(10, 0), time.Unix(10, 0)}, 142 } 143 for _, input := range inputs { 144 require.Equal(t, input.expected, MinTime(input.t1, input.t2)) 145 } 146 } 147 148 func TestMaxTime(t *testing.T) { 149 inputs := []struct { 150 t1 time.Time 151 t2 time.Time 152 expected time.Time 153 }{ 154 {time.Unix(10, 0), time.Unix(20, 0), time.Unix(20, 0)}, 155 {time.Unix(20, 0), time.Unix(10, 0), time.Unix(20, 0)}, 156 {time.Unix(10, 0), time.Unix(10, 0), time.Unix(10, 0)}, 157 } 158 for _, input := range inputs { 159 require.Equal(t, input.expected, MaxTime(input.t1, input.t2)) 160 } 161 } 162 163 func TestTruncate(t *testing.T) { 164 var ( 165 n = time.Now().UTC() 166 format = "1 2 3:04:05PM" 167 168 day = time.Hour * 24 169 week = day * 7 170 month = day * 30 171 year = day * 365 172 173 blockSizes = []time.Duration{ 174 time.Nanosecond, 175 time.Nanosecond * 123, 176 time.Microsecond, 177 time.Microsecond, 178 time.Millisecond * 123, 179 time.Second, 180 time.Second * 11, 181 time.Minute, 182 time.Hour, 183 day, 184 day * 2, 185 day*2 + time.Hour*6, 186 week, 187 month, 188 year, 189 } 190 ) 191 192 for _, blockSize := range blockSizes { 193 blockSize := blockSize 194 t.Run(blockSize.String(), func(t *testing.T) { 195 xNow := ToUnixNano(n).Truncate(blockSize) 196 now := n.Truncate(blockSize) 197 198 require.Equal(t, now.String(), xNow.String()) 199 require.Equal(t, now.Format(format), xNow.Format(format)) 200 require.Equal(t, now.UnixNano(), int64(xNow)) 201 }) 202 } 203 }