github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/mysql/time_test.go (about) 1 // Copyright 2015 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 mysql 15 16 import ( 17 "testing" 18 "time" 19 20 . "github.com/insionng/yougam/libraries/pingcap/check" 21 "github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak" 22 ) 23 24 func TestT(t *testing.T) { 25 TestingT(t) 26 } 27 28 var _ = Suite(&testTimeSuite{}) 29 30 type testTimeSuite struct { 31 } 32 33 func (s *testTimeSuite) TestDateTime(c *C) { 34 defer testleak.AfterTest(c)() 35 table := []struct { 36 Input string 37 Expect string 38 }{ 39 {"2012-12-31 11:30:45", "2012-12-31 11:30:45"}, 40 {"0000-00-00 00:00:00", "0000-00-00 00:00:00"}, 41 {"0001-01-01 00:00:00", "0001-01-01 00:00:00"}, 42 {"00-12-31 11:30:45", "2000-12-31 11:30:45"}, 43 {"12-12-31 11:30:45", "2012-12-31 11:30:45"}, 44 {"2012-12-31", "2012-12-31 00:00:00"}, 45 {"20121231", "2012-12-31 00:00:00"}, 46 {"121231", "2012-12-31 00:00:00"}, 47 {"2012^12^31 11+30+45", "2012-12-31 11:30:45"}, 48 {"2012^12^31T11+30+45", "2012-12-31 11:30:45"}, 49 {"2012-2-1 11:30:45", "2012-02-01 11:30:45"}, 50 {"12-2-1 11:30:45", "2012-02-01 11:30:45"}, 51 {"20121231113045", "2012-12-31 11:30:45"}, 52 {"121231113045", "2012-12-31 11:30:45"}, 53 } 54 55 for _, test := range table { 56 t, err := ParseDatetime(test.Input) 57 c.Assert(err, IsNil) 58 c.Assert(t.String(), Equals, test.Expect) 59 } 60 61 fspTbl := []struct { 62 Input string 63 Fsp int 64 Expect string 65 }{ 66 {"121231113045.123345", 6, "2012-12-31 11:30:45.123345"}, 67 {"20121231113045.123345", 6, "2012-12-31 11:30:45.123345"}, 68 {"121231113045.9999999", 6, "2012-12-31 11:30:46.000000"}, 69 } 70 71 for _, test := range fspTbl { 72 t, err := ParseTime(test.Input, TypeDatetime, test.Fsp) 73 c.Assert(err, IsNil) 74 c.Assert(t.String(), Equals, test.Expect) 75 } 76 77 // test error 78 errTable := []string{ 79 "1000-00-00 00:00:00", 80 "1000-01-01 00:00:70", 81 "1000-13-00 00:00:00", 82 "10000-01-01 00:00:00", 83 } 84 85 for _, test := range errTable { 86 _, err := ParseDatetime(test) 87 c.Assert(err, NotNil) 88 } 89 } 90 91 func (s *testTimeSuite) TestTimestamp(c *C) { 92 defer testleak.AfterTest(c)() 93 table := []struct { 94 Input string 95 Expect string 96 }{ 97 {"2012-12-31 11:30:45", "2012-12-31 11:30:45"}, 98 } 99 100 for _, test := range table { 101 t, err := ParseTimestamp(test.Input) 102 c.Assert(err, IsNil) 103 c.Assert(t.String(), Equals, test.Expect) 104 } 105 106 errTable := []string{ 107 "2048-12-31 11:30:45", 108 "1969-12-31 11:30:45", 109 } 110 111 for _, test := range errTable { 112 _, err := ParseTimestamp(test) 113 c.Assert(err, NotNil) 114 } 115 } 116 117 func (s *testTimeSuite) TestDate(c *C) { 118 defer testleak.AfterTest(c)() 119 table := []struct { 120 Input string 121 Expect string 122 }{ 123 {"2012-12-31", "2012-12-31"}, 124 {"00-12-31", "2000-12-31"}, 125 {"20121231", "2012-12-31"}, 126 {"121231", "2012-12-31"}, 127 {"2015-06-01 12:12:12", "2015-06-01"}, 128 {"0001-01-01 00:00:00", "0001-01-01"}, 129 {"0001-01-01", "0001-01-01"}, 130 } 131 132 for _, test := range table { 133 t, err := ParseDate(test.Input) 134 c.Assert(err, IsNil) 135 c.Assert(t.String(), Equals, test.Expect) 136 } 137 138 errTable := []string{ 139 "0121231", 140 } 141 142 for _, test := range errTable { 143 _, err := ParseDate(test) 144 c.Assert(err, NotNil) 145 } 146 } 147 148 func (s *testTimeSuite) TestTime(c *C) { 149 defer testleak.AfterTest(c)() 150 table := []struct { 151 Input string 152 Expect string 153 }{ 154 {"10:11:12", "10:11:12"}, 155 {"101112", "10:11:12"}, 156 {"10:11", "10:11:00"}, 157 {"101112.123456", "10:11:12"}, 158 {"1112", "00:11:12"}, 159 {"12", "00:00:12"}, 160 {"1 12", "36:00:00"}, 161 {"1 10:11:12", "34:11:12"}, 162 {"1 10:11:12.123456", "34:11:12"}, 163 {"10:11:12.123456", "10:11:12"}, 164 {"1 10:11", "34:11:00"}, 165 {"1 10", "34:00:00"}, 166 {"24 10", "586:00:00"}, 167 {"-24 10", "-586:00:00"}, 168 {"0 10", "10:00:00"}, 169 {"-10:10:10", "-10:10:10"}, 170 {"-838:59:59", "-838:59:59"}, 171 {"838:59:59", "838:59:59"}, 172 {"2011-11-11 00:00:01", "00:00:01"}, 173 {"2011-11-11", "00:00:00"}, 174 } 175 176 for _, test := range table { 177 t, err := ParseDuration(test.Input, MinFsp) 178 c.Assert(err, IsNil) 179 c.Assert(t.String(), Equals, test.Expect) 180 } 181 182 table = []struct { 183 Input string 184 Expect string 185 }{ 186 {"101112.123456", "10:11:12.123456"}, 187 {"1 10:11:12.123456", "34:11:12.123456"}, 188 {"10:11:12.123456", "10:11:12.123456"}, 189 } 190 191 for _, test := range table { 192 t, err := ParseDuration(test.Input, MaxFsp) 193 c.Assert(err, IsNil) 194 c.Assert(t.String(), Equals, test.Expect) 195 } 196 197 errTable := []string{ 198 "232 10", 199 "-232 10", 200 } 201 202 for _, test := range errTable { 203 _, err := ParseDuration(test, DefaultFsp) 204 c.Assert(err, NotNil) 205 } 206 207 // test time compare 208 cmpTable := []struct { 209 lhs int64 210 rhs int64 211 ret int 212 }{ 213 {1, 0, 1}, 214 {0, 1, -1}, 215 {0, 0, 0}, 216 } 217 218 for _, t := range cmpTable { 219 t1 := Duration{time.Duration(t.lhs), DefaultFsp} 220 t2 := Duration{time.Duration(t.rhs), DefaultFsp} 221 ret := t1.Compare(t2) 222 c.Assert(ret, Equals, t.ret) 223 } 224 } 225 226 func (s *testTimeSuite) TestTimeFsp(c *C) { 227 defer testleak.AfterTest(c)() 228 table := []struct { 229 Input string 230 Fsp int 231 Expect string 232 }{ 233 {"00:00:00.1", 0, "00:00:00"}, 234 {"00:00:00.1", 1, "00:00:00.1"}, 235 {"00:00:00.777777", 2, "00:00:00.78"}, 236 {"00:00:00.777777", 6, "00:00:00.777777"}, 237 // fsp -1 use default 0 238 {"00:00:00.777777", -1, "00:00:01"}, 239 {"00:00:00.001", 3, "00:00:00.001"}, 240 } 241 242 for _, test := range table { 243 t, err := ParseDuration(test.Input, test.Fsp) 244 c.Assert(err, IsNil) 245 c.Assert(t.String(), Equals, test.Expect) 246 } 247 248 errTable := []struct { 249 Input string 250 Fsp int 251 }{ 252 {"00:00:00.1", -2}, 253 {"00:00:00.1", 7}, 254 } 255 256 for _, test := range errTable { 257 _, err := ParseDuration(test.Input, test.Fsp) 258 c.Assert(err, NotNil) 259 } 260 } 261 func (s *testTimeSuite) TestYear(c *C) { 262 defer testleak.AfterTest(c)() 263 table := []struct { 264 Input string 265 Expect int16 266 }{ 267 {"1990", 1990}, 268 {"10", 2010}, 269 {"0", 2000}, 270 {"99", 1999}, 271 } 272 273 for _, test := range table { 274 t, err := ParseYear(test.Input) 275 c.Assert(err, IsNil) 276 c.Assert(t, Equals, test.Expect) 277 } 278 279 valids := []struct { 280 Year int64 281 Expect bool 282 }{ 283 {2000, true}, 284 {20000, false}, 285 {0, true}, 286 {-1, false}, 287 } 288 289 for _, test := range valids { 290 _, err := AdjustYear(test.Year) 291 if test.Expect { 292 c.Assert(err, IsNil) 293 } else { 294 c.Assert(err, NotNil) 295 } 296 } 297 298 } 299 300 func (s *testTimeSuite) getLocation(c *C) *time.Location { 301 locations := []string{"Asia/Shanghai", "Europe/Berlin"} 302 timeFormat := "Jan 2, 2006 at 3:04pm (MST)" 303 304 z, err := time.LoadLocation(locations[0]) 305 c.Assert(err, IsNil) 306 307 t1, err := time.ParseInLocation(timeFormat, "Jul 9, 2012 at 5:02am (CEST)", z) 308 c.Assert(err, IsNil) 309 t2, err := time.Parse(timeFormat, "Jul 9, 2012 at 5:02am (CEST)") 310 c.Assert(err, IsNil) 311 312 if t1.Equal(t2) { 313 z, err = time.LoadLocation(locations[1]) 314 c.Assert(err, IsNil) 315 } 316 317 return z 318 } 319 320 func (s *testTimeSuite) TestCodec(c *C) { 321 defer testleak.AfterTest(c)() 322 t, err := ParseTimestamp("2010-10-10 10:11:11") 323 c.Assert(err, IsNil) 324 b, err := t.Marshal() 325 c.Assert(err, IsNil) 326 327 var t1 Time 328 t1.Type = TypeTimestamp 329 330 z := s.getLocation(c) 331 332 err = t1.UnmarshalInLocation(b, z) 333 c.Assert(err, IsNil) 334 c.Assert(t.String(), Not(Equals), t1.String()) 335 336 err = t1.UnmarshalInLocation(b, time.Local) 337 c.Assert(err, IsNil) 338 c.Assert(t.String(), Equals, t1.String()) 339 340 t1.Time = time.Now() 341 b, err = t1.Marshal() 342 c.Assert(err, IsNil) 343 344 var t2 Time 345 t2.Type = TypeTimestamp 346 err = t2.Unmarshal(b) 347 c.Assert(err, IsNil) 348 c.Assert(t1.String(), Equals, t2.String()) 349 350 b, err = ZeroDatetime.Marshal() 351 c.Assert(err, IsNil) 352 353 var t3 Time 354 t3.Type = TypeDatetime 355 err = t3.Unmarshal(b) 356 c.Assert(err, IsNil) 357 c.Assert(t3.String(), Equals, ZeroDatetime.String()) 358 359 t, err = ParseDatetime("0001-01-01 00:00:00") 360 c.Assert(err, IsNil) 361 b, err = t.Marshal() 362 c.Assert(err, IsNil) 363 364 var t4 Time 365 t4.Type = TypeDatetime 366 err = t4.Unmarshal(b) 367 c.Assert(err, IsNil) 368 c.Assert(t.String(), Equals, t4.String()) 369 370 tbl := []string{ 371 "2000-01-01 00:00:00.000000", 372 "2000-01-01 00:00:00.123456", 373 "0001-01-01 00:00:00.123456", 374 "2000-06-01 00:00:00.999999", 375 } 376 377 for _, test := range tbl { 378 t, err := ParseTime(test, TypeDatetime, MaxFsp) 379 c.Assert(err, IsNil) 380 381 b, err := t.Marshal() 382 c.Assert(err, IsNil) 383 384 var dest Time 385 dest.Type = TypeDatetime 386 dest.Fsp = MaxFsp 387 err = dest.Unmarshal(b) 388 c.Assert(err, IsNil) 389 c.Assert(dest.String(), Equals, test) 390 } 391 } 392 393 func (s *testTimeSuite) TestParseTimeFromNum(c *C) { 394 defer testleak.AfterTest(c)() 395 table := []struct { 396 Input int64 397 ExpectDateTimeError bool 398 ExpectDateTimeValue string 399 ExpectTimeStampError bool 400 ExpectTimeStampValue string 401 ExpectDateError bool 402 ExpectDateValue string 403 }{ 404 {20101010111111, false, "2010-10-10 11:11:11", false, "2010-10-10 11:11:11", false, "2010-10-10"}, 405 {2010101011111, true, zeroDatetimeStr, true, zeroDatetimeStr, true, zeroDateStr}, 406 {201010101111, false, "2020-10-10 10:11:11", false, "2020-10-10 10:11:11", false, "2020-10-10"}, 407 {20101010111, false, "2002-01-01 01:01:11", false, "2002-01-01 01:01:11", false, "2002-01-01"}, 408 {2010101011, true, zeroDatetimeStr, true, zeroDatetimeStr, true, zeroDateStr}, 409 {201010101, false, "2000-02-01 01:01:01", false, "2000-02-01 01:01:01", false, "2000-02-01"}, 410 {20101010, false, "2010-10-10 00:00:00", false, "2010-10-10 00:00:00", false, "2010-10-10"}, 411 {2010101, true, zeroDatetimeStr, true, zeroDatetimeStr, true, zeroDateStr}, 412 {201010, false, "2020-10-10 00:00:00", false, "2020-10-10 00:00:00", false, "2020-10-10"}, 413 {20101, false, "2002-01-01 00:00:00", false, "2002-01-01 00:00:00", false, "2002-01-01"}, 414 {2010, true, zeroDatetimeStr, true, zeroDatetimeStr, true, zeroDateStr}, 415 {201, false, "2000-02-01 00:00:00", false, "2000-02-01 00:00:00", false, "2000-02-01"}, 416 {20, true, zeroDatetimeStr, true, zeroDatetimeStr, true, zeroDateStr}, 417 {2, true, zeroDatetimeStr, true, zeroDatetimeStr, true, zeroDateStr}, 418 {0, false, zeroDatetimeStr, false, zeroDatetimeStr, false, zeroDateStr}, 419 {-1, true, zeroDatetimeStr, true, zeroDatetimeStr, true, zeroDateStr}, 420 {99999999999999, true, zeroDatetimeStr, true, zeroDatetimeStr, true, zeroDateStr}, 421 {100000000000000, true, zeroDatetimeStr, true, zeroDatetimeStr, true, zeroDateStr}, 422 {10000102000000, false, "1000-01-02 00:00:00", true, zeroDatetimeStr, false, "1000-01-02"}, 423 {19690101000000, false, "1969-01-01 00:00:00", true, zeroDatetimeStr, false, "1969-01-01"}, 424 {991231235959, false, "1999-12-31 23:59:59", false, "1999-12-31 23:59:59", false, "1999-12-31"}, 425 {691231235959, false, "2069-12-31 23:59:59", true, zeroDatetimeStr, false, "2069-12-31"}, 426 {370119031407, false, "2037-01-19 03:14:07", false, "2037-01-19 03:14:07", false, "2037-01-19"}, 427 {380120031407, false, "2038-01-20 03:14:07", true, zeroDatetimeStr, false, "2038-01-20"}, 428 } 429 430 for _, test := range table { 431 // test ParseDatetimeFromNum 432 t, err := ParseDatetimeFromNum(test.Input) 433 if test.ExpectDateTimeError { 434 c.Assert(err, NotNil) 435 } else { 436 c.Assert(err, IsNil) 437 c.Assert(t.Type, Equals, TypeDatetime) 438 } 439 c.Assert(t.String(), Equals, test.ExpectDateTimeValue) 440 441 // test ParseTimestampFromNum 442 t, err = ParseTimestampFromNum(test.Input) 443 if test.ExpectTimeStampError { 444 c.Assert(err, NotNil) 445 } else { 446 c.Assert(err, IsNil) 447 c.Assert(t.Type, Equals, TypeTimestamp) 448 } 449 c.Assert(t.String(), Equals, test.ExpectTimeStampValue) 450 451 // test ParseDateFromNum 452 t, err = ParseDateFromNum(test.Input) 453 454 if test.ExpectDateTimeError { 455 c.Assert(err, NotNil) 456 } else { 457 c.Assert(err, IsNil) 458 c.Assert(t.Type, Equals, TypeDate) 459 } 460 c.Assert(t.String(), Equals, test.ExpectDateValue) 461 } 462 } 463 464 func (s *testTimeSuite) TestToNumber(c *C) { 465 defer testleak.AfterTest(c)() 466 tblDateTime := []struct { 467 Input string 468 Fsp int 469 Expect string 470 }{ 471 {"12-12-31 11:30:45", 0, "20121231113045"}, 472 {"12-12-31 11:30:45", 6, "20121231113045.000000"}, 473 {"12-12-31 11:30:45.123", 6, "20121231113045.123000"}, 474 {"12-12-31 11:30:45.123345", 0, "20121231113045"}, 475 {"12-12-31 11:30:45.123345", 3, "20121231113045.123"}, 476 {"12-12-31 11:30:45.123345", 5, "20121231113045.12335"}, 477 {"12-12-31 11:30:45.123345", 6, "20121231113045.123345"}, 478 {"12-12-31 11:30:45.1233457", 6, "20121231113045.123346"}, 479 {"12-12-31 11:30:45.823345", 0, "20121231113046"}, 480 } 481 482 for _, test := range tblDateTime { 483 t, err := ParseTime(test.Input, TypeDatetime, test.Fsp) 484 c.Assert(err, IsNil) 485 c.Assert(t.ToNumber().String(), Equals, test.Expect) 486 } 487 488 // Fix issue #1046 489 tblDate := []struct { 490 Input string 491 Fsp int 492 Expect string 493 }{ 494 {"12-12-31 11:30:45", 0, "20121231"}, 495 {"12-12-31 11:30:45", 6, "20121231"}, 496 {"12-12-31 11:30:45.123", 6, "20121231"}, 497 {"12-12-31 11:30:45.123345", 0, "20121231"}, 498 {"12-12-31 11:30:45.123345", 3, "20121231"}, 499 {"12-12-31 11:30:45.123345", 5, "20121231"}, 500 {"12-12-31 11:30:45.123345", 6, "20121231"}, 501 {"12-12-31 11:30:45.1233457", 6, "20121231"}, 502 {"12-12-31 11:30:45.823345", 0, "20121231"}, 503 } 504 505 for _, test := range tblDate { 506 t, err := ParseTime(test.Input, TypeDate, 0) 507 c.Assert(err, IsNil) 508 c.Assert(t.ToNumber().String(), Equals, test.Expect) 509 } 510 511 tblDuration := []struct { 512 Input string 513 Fsp int 514 Expect string 515 }{ 516 {"11:30:45", 0, "113045"}, 517 {"11:30:45", 6, "113045.000000"}, 518 {"11:30:45.123", 6, "113045.123000"}, 519 {"11:30:45.123345", 0, "113045"}, 520 {"11:30:45.123345", 3, "113045.123"}, 521 {"11:30:45.123345", 5, "113045.12335"}, 522 {"11:30:45.123345", 6, "113045.123345"}, 523 {"11:30:45.1233456", 6, "113045.123346"}, 524 {"11:30:45.9233456", 0, "113046"}, 525 {"-11:30:45.9233456", 0, "-113046"}, 526 } 527 528 for _, test := range tblDuration { 529 t, err := ParseDuration(test.Input, test.Fsp) 530 c.Assert(err, IsNil) 531 // now we can only change Duration's Fsp to check ToNumber with different Fsp 532 c.Assert(t.ToNumber().String(), Equals, test.Expect) 533 } 534 } 535 536 func (s *testTimeSuite) TestParseFrac(c *C) { 537 defer testleak.AfterTest(c)() 538 tbl := []struct { 539 S string 540 Fsp int 541 Ret int 542 }{ 543 {"1234567", 0, 0}, 544 {"1234567", 1, 100000}, 545 {"0000567", 5, 60}, 546 {"1234567", 5, 123460}, 547 {"1234567", 6, 123457}, 548 {"9999999", 6, 1000000}, 549 } 550 551 for _, t := range tbl { 552 v, err := parseFrac(t.S, t.Fsp) 553 c.Assert(err, IsNil) 554 c.Assert(t.Ret, Equals, v) 555 } 556 } 557 558 func (s *testTimeSuite) TestRoundFrac(c *C) { 559 defer testleak.AfterTest(c)() 560 tbl := []struct { 561 Input string 562 Fsp int 563 Except string 564 }{ 565 {"2012-12-31 11:30:45.123456", 4, "2012-12-31 11:30:45.1235"}, 566 {"2012-12-31 11:30:45.123456", 6, "2012-12-31 11:30:45.123456"}, 567 {"2012-12-31 11:30:45.123456", 0, "2012-12-31 11:30:45"}, 568 {"2012-12-31 11:30:45.123456", 1, "2012-12-31 11:30:45.1"}, 569 {"2012-12-31 11:30:45.999999", 4, "2012-12-31 11:30:46.0000"}, 570 {"2012-12-31 11:30:45.999999", 0, "2012-12-31 11:30:46"}, 571 } 572 573 for _, t := range tbl { 574 v, err := ParseTime(t.Input, TypeDatetime, MaxFsp) 575 c.Assert(err, IsNil) 576 nv, err := v.RoundFrac(t.Fsp) 577 c.Assert(err, IsNil) 578 c.Assert(nv.String(), Equals, t.Except) 579 } 580 581 tbl = []struct { 582 Input string 583 Fsp int 584 Except string 585 }{ 586 {"11:30:45.123456", 4, "11:30:45.1235"}, 587 {"11:30:45.123456", 6, "11:30:45.123456"}, 588 {"11:30:45.123456", 0, "11:30:45"}, 589 {"1 11:30:45.123456", 1, "35:30:45.1"}, 590 {"1 11:30:45.999999", 4, "35:30:46.0000"}, 591 {"-1 11:30:45.999999", 0, "-35:30:46"}, 592 } 593 594 for _, t := range tbl { 595 v, err := ParseDuration(t.Input, MaxFsp) 596 c.Assert(err, IsNil) 597 nv, err := v.RoundFrac(t.Fsp) 598 c.Assert(err, IsNil) 599 c.Assert(nv.String(), Equals, t.Except) 600 } 601 } 602 603 func (s *testTimeSuite) TestConvert(c *C) { 604 defer testleak.AfterTest(c)() 605 tbl := []struct { 606 Input string 607 Fsp int 608 Except string 609 }{ 610 {"2012-12-31 11:30:45.123456", 4, "11:30:45.1235"}, 611 {"2012-12-31 11:30:45.123456", 6, "11:30:45.123456"}, 612 {"2012-12-31 11:30:45.123456", 0, "11:30:45"}, 613 {"2012-12-31 11:30:45.999999", 0, "11:30:46"}, 614 {"0000-00-00 00:00:00", 6, "00:00:00"}, 615 } 616 617 for _, t := range tbl { 618 v, err := ParseTime(t.Input, TypeDatetime, t.Fsp) 619 c.Assert(err, IsNil) 620 nv, err := v.ConvertToDuration() 621 c.Assert(err, IsNil) 622 c.Assert(nv.String(), Equals, t.Except) 623 } 624 625 tblDuration := []struct { 626 Input string 627 Fsp int 628 }{ 629 {"11:30:45.123456", 4}, 630 {"11:30:45.123456", 6}, 631 {"11:30:45.123456", 0}, 632 {"1 11:30:45.999999", 0}, 633 } 634 635 for _, t := range tblDuration { 636 v, err := ParseDuration(t.Input, t.Fsp) 637 c.Assert(err, IsNil) 638 year, month, day := time.Now().Date() 639 n := time.Date(year, month, day, 0, 0, 0, 0, time.Local) 640 t, err := v.ConvertToTime(TypeDatetime) 641 c.Assert(err, IsNil) 642 c.Assert(t.Time.Sub(n), Equals, v.Duration) 643 } 644 } 645 646 func (s *testTimeSuite) TestCompare(c *C) { 647 defer testleak.AfterTest(c)() 648 tbl := []struct { 649 Arg1 string 650 Arg2 string 651 Ret int 652 }{ 653 {"2011-10-10 11:11:11", "2011-10-10 11:11:11", 0}, 654 {"2011-10-10 11:11:11.123456", "2011-10-10 11:11:11.1", 1}, 655 {"2011-10-10 11:11:11", "2011-10-10 11:11:11.123", -1}, 656 {"0000-00-00 00:00:00", "2011-10-10 11:11:11", -1}, 657 {"0000-00-00 00:00:00", "0000-00-00 00:00:00", 0}, 658 } 659 660 for _, t := range tbl { 661 v1, err := ParseTime(t.Arg1, TypeDatetime, MaxFsp) 662 c.Assert(err, IsNil) 663 664 ret, err := v1.CompareString(t.Arg2) 665 c.Assert(err, IsNil) 666 c.Assert(ret, Equals, t.Ret) 667 } 668 669 tbl = []struct { 670 Arg1 string 671 Arg2 string 672 Ret int 673 }{ 674 {"11:11:11", "11:11:11", 0}, 675 {"11:11:11.123456", "11:11:11.1", 1}, 676 {"11:11:11", "11:11:11.123", -1}, 677 } 678 679 for _, t := range tbl { 680 v1, err := ParseDuration(t.Arg1, MaxFsp) 681 c.Assert(err, IsNil) 682 683 ret, err := v1.CompareString(t.Arg2) 684 c.Assert(err, IsNil) 685 c.Assert(ret, Equals, t.Ret) 686 } 687 } 688 689 func (s *testTimeSuite) TestDurationClock(c *C) { 690 defer testleak.AfterTest(c)() 691 // test hour, minute, second and micro second 692 tbl := []struct { 693 Input string 694 Hour int 695 Minute int 696 Second int 697 MicroSecond int 698 }{ 699 {"11:11:11.11", 11, 11, 11, 110000}, 700 {"1 11:11:11.000011", 35, 11, 11, 11}, 701 {"2010-10-10 11:11:11.000011", 11, 11, 11, 11}, 702 } 703 704 for _, t := range tbl { 705 d, err := ParseDuration(t.Input, MaxFsp) 706 c.Assert(err, IsNil) 707 c.Assert(d.Hour(), Equals, t.Hour) 708 c.Assert(d.Minute(), Equals, t.Minute) 709 c.Assert(d.Second(), Equals, t.Second) 710 c.Assert(d.MicroSecond(), Equals, t.MicroSecond) 711 } 712 } 713 714 func (s *testTimeSuite) TestParseDateFormat(c *C) { 715 defer testleak.AfterTest(c)() 716 tbl := []struct { 717 Input string 718 Result []string 719 }{ 720 {"2011-11-11 10:10:10.123456", []string{"2011", "11", "11", "10", "10", "10", "123456"}}, 721 {" 2011-11-11 10:10:10.123456 ", []string{"2011", "11", "11", "10", "10", "10", "123456"}}, 722 {"2011-11-11 10", []string{"2011", "11", "11", "10"}}, 723 {"2011-11-11T10:10:10.123456", []string{"2011", "11", "11", "10", "10", "10", "123456"}}, 724 {"2011:11:11T10:10:10.123456", []string{"2011", "11", "11", "10", "10", "10", "123456"}}, 725 {"xx2011-11-11 10:10:10", nil}, 726 {"T10:10:10", nil}, 727 {"2011-11-11x", nil}, 728 {"2011-11-11 10:10:10", nil}, 729 {"xxx 10:10:10", nil}, 730 } 731 732 for _, t := range tbl { 733 r := parseDateFormat(t.Input) 734 c.Assert(r, DeepEquals, t.Result) 735 } 736 }