github.com/XiaoMi/Gaea@v1.2.5/parser/tidb-types/mytime_test.go (about) 1 // Copyright 2016 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package types 15 16 import ( 17 . "github.com/pingcap/check" 18 ) 19 20 type testMyTimeSuite struct{} 21 22 var _ = Suite(&testMyTimeSuite{}) 23 24 func (s *testMyTimeSuite) TestWeekBehaviour(c *C) { 25 c.Assert(weekBehaviourMondayFirst, Equals, weekBehaviour(1)) 26 c.Assert(weekBehaviourYear, Equals, weekBehaviour(2)) 27 c.Assert(weekBehaviourFirstWeekday, Equals, weekBehaviour(4)) 28 29 c.Check(weekBehaviour(1).test(weekBehaviourMondayFirst), IsTrue) 30 c.Check(weekBehaviour(2).test(weekBehaviourYear), IsTrue) 31 c.Check(weekBehaviour(4).test(weekBehaviourFirstWeekday), IsTrue) 32 } 33 34 func (s *testMyTimeSuite) TestWeek(c *C) { 35 tests := []struct { 36 Input MysqlTime 37 Mode int 38 Expect int 39 }{ 40 {MysqlTime{2008, 2, 20, 0, 0, 0, 0}, 0, 7}, 41 {MysqlTime{2008, 2, 20, 0, 0, 0, 0}, 1, 8}, 42 {MysqlTime{2008, 12, 31, 0, 0, 0, 0}, 1, 53}, 43 } 44 45 for ith, tt := range tests { 46 _, week := calcWeek(&tt.Input, weekMode(tt.Mode)) 47 c.Check(week, Equals, tt.Expect, Commentf("%d failed.", ith)) 48 } 49 } 50 51 func (s *testMyTimeSuite) TestCalcDaynr(c *C) { 52 c.Assert(calcDaynr(0, 0, 0), Equals, 0) 53 c.Assert(calcDaynr(9999, 12, 31), Equals, 3652424) 54 c.Assert(calcDaynr(1970, 1, 1), Equals, 719528) 55 c.Assert(calcDaynr(2006, 12, 16), Equals, 733026) 56 c.Assert(calcDaynr(10, 1, 2), Equals, 3654) 57 c.Assert(calcDaynr(2008, 2, 20), Equals, 733457) 58 } 59 60 func (s *testMyTimeSuite) TestCalcTimeDiff(c *C) { 61 tests := []struct { 62 T1 MysqlTime 63 T2 MysqlTime 64 Sign int 65 Expect MysqlTime 66 }{ 67 // calcTimeDiff can be used for month = 0. 68 { 69 MysqlTime{2006, 0, 1, 12, 23, 21, 0}, 70 MysqlTime{2006, 0, 3, 21, 23, 22, 0}, 71 1, 72 MysqlTime{0, 0, 0, 57, 0, 1, 0}, 73 }, 74 { 75 MysqlTime{0, 0, 0, 21, 23, 24, 0}, 76 MysqlTime{0, 0, 0, 11, 23, 22, 0}, 77 1, 78 MysqlTime{0, 0, 0, 10, 0, 2, 0}, 79 }, 80 { 81 MysqlTime{0, 0, 0, 1, 2, 3, 0}, 82 MysqlTime{0, 0, 0, 5, 2, 0, 0}, 83 -1, 84 MysqlTime{0, 0, 0, 6, 4, 3, 0}, 85 }, 86 } 87 88 for i, tt := range tests { 89 seconds, microseconds, _ := calcTimeDiff(tt.T1, tt.T2, tt.Sign) 90 var result MysqlTime 91 calcTimeFromSec(&result, seconds, microseconds) 92 c.Assert(result, Equals, tt.Expect, Commentf("%d failed.", i)) 93 } 94 } 95 96 func (s *testMyTimeSuite) TestCompareTime(c *C) { 97 tests := []struct { 98 T1 MysqlTime 99 T2 MysqlTime 100 Expect int 101 }{ 102 {MysqlTime{0, 0, 0, 0, 0, 0, 0}, MysqlTime{0, 0, 0, 0, 0, 0, 0}, 0}, 103 {MysqlTime{0, 0, 0, 0, 1, 0, 0}, MysqlTime{0, 0, 0, 0, 0, 0, 0}, 1}, 104 {MysqlTime{2006, 1, 2, 3, 4, 5, 6}, MysqlTime{2016, 1, 2, 3, 4, 5, 0}, -1}, 105 {MysqlTime{0, 0, 0, 11, 22, 33, 0}, MysqlTime{0, 0, 0, 12, 21, 33, 0}, -1}, 106 {MysqlTime{9999, 12, 30, 23, 59, 59, 999999}, MysqlTime{0, 1, 2, 3, 4, 5, 6}, 1}, 107 } 108 109 for _, tt := range tests { 110 c.Assert(compareTime(tt.T1, tt.T2), Equals, tt.Expect) 111 c.Assert(compareTime(tt.T2, tt.T1), Equals, -tt.Expect) 112 } 113 } 114 115 func (s *testMyTimeSuite) TestGetDateFromDaynr(c *C) { 116 tests := []struct { 117 daynr uint 118 year uint 119 month uint 120 day uint 121 }{ 122 {730669, 2000, 7, 3}, 123 {720195, 1971, 10, 30}, 124 {719528, 1970, 01, 01}, 125 {719892, 1970, 12, 31}, 126 {730850, 2000, 12, 31}, 127 {730544, 2000, 2, 29}, 128 {204960, 561, 2, 28}, 129 {0, 0, 0, 0}, 130 {32, 0, 0, 0}, 131 {366, 1, 1, 1}, 132 {744729, 2038, 12, 31}, 133 {3652424, 9999, 12, 31}, 134 } 135 136 for _, tt := range tests { 137 yy, mm, dd := getDateFromDaynr(tt.daynr) 138 c.Assert(yy, Equals, tt.year) 139 c.Assert(mm, Equals, tt.month) 140 c.Assert(dd, Equals, tt.day) 141 } 142 } 143 144 func (s *testMyTimeSuite) TestMixDateAndTime(c *C) { 145 tests := []struct { 146 date MysqlTime 147 time MysqlTime 148 neg bool 149 expect MysqlTime 150 }{ 151 { 152 date: MysqlTime{1896, 3, 4, 0, 0, 0, 0}, 153 time: MysqlTime{0, 0, 0, 12, 23, 24, 5}, 154 neg: false, 155 expect: MysqlTime{1896, 3, 4, 12, 23, 24, 5}, 156 }, 157 { 158 date: MysqlTime{1896, 3, 4, 0, 0, 0, 0}, 159 time: MysqlTime{0, 0, 0, 24, 23, 24, 5}, 160 neg: false, 161 expect: MysqlTime{1896, 3, 5, 0, 23, 24, 5}, 162 }, 163 { 164 date: MysqlTime{2016, 12, 31, 0, 0, 0, 0}, 165 time: MysqlTime{0, 0, 0, 24, 0, 0, 0}, 166 neg: false, 167 expect: MysqlTime{2017, 1, 1, 0, 0, 0, 0}, 168 }, 169 { 170 date: MysqlTime{2016, 12, 0, 0, 0, 0, 0}, 171 time: MysqlTime{0, 0, 0, 24, 0, 0, 0}, 172 neg: false, 173 expect: MysqlTime{2016, 12, 1, 0, 0, 0, 0}, 174 }, 175 { 176 date: MysqlTime{2017, 1, 12, 3, 23, 15, 0}, 177 time: MysqlTime{0, 0, 0, 2, 21, 10, 0}, 178 neg: true, 179 expect: MysqlTime{2017, 1, 12, 1, 2, 5, 0}, 180 }, 181 } 182 183 for ith, t := range tests { 184 mixDateAndTime(&t.date, &t.time, t.neg) 185 c.Assert(compareTime(t.date, t.expect), Equals, 0, Commentf("%d", ith)) 186 } 187 } 188 189 func (s *testMyTimeSuite) TestIsLeapYear(c *C) { 190 tests := []struct { 191 T MysqlTime 192 Expect bool 193 }{ 194 {MysqlTime{1960, 1, 1, 0, 0, 0, 0}, true}, 195 {MysqlTime{1963, 2, 21, 0, 0, 0, 0}, false}, 196 {MysqlTime{2008, 11, 25, 0, 0, 0, 0}, true}, 197 {MysqlTime{2017, 4, 24, 0, 0, 0, 0}, false}, 198 {MysqlTime{1988, 2, 29, 0, 0, 0, 0}, true}, 199 {MysqlTime{2000, 3, 15, 0, 0, 0, 0}, true}, 200 {MysqlTime{1992, 5, 3, 0, 0, 0, 0}, true}, 201 {MysqlTime{2024, 10, 1, 0, 0, 0, 0}, true}, 202 {MysqlTime{2016, 6, 29, 0, 0, 0, 0}, true}, 203 {MysqlTime{2015, 6, 29, 0, 0, 0, 0}, false}, 204 {MysqlTime{2014, 9, 31, 0, 0, 0, 0}, false}, 205 {MysqlTime{2001, 12, 7, 0, 0, 0, 0}, false}, 206 {MysqlTime{1989, 7, 6, 0, 0, 0, 0}, false}, 207 } 208 209 for _, tt := range tests { 210 c.Assert(tt.T.IsLeapYear(), Equals, tt.Expect) 211 } 212 } 213 func (s *testMyTimeSuite) TestGetLastDay(c *C) { 214 tests := []struct { 215 year int 216 month int 217 expectedDay int 218 }{ 219 {2000, 1, 31}, 220 {2000, 2, 29}, 221 {2000, 4, 30}, 222 {1900, 2, 28}, 223 {1996, 2, 29}, 224 } 225 226 for _, t := range tests { 227 day := GetLastDay(t.year, t.month) 228 c.Assert(day, Equals, t.expectedDay) 229 } 230 }