@@ -13,6 +13,7 @@ import (
13
13
"database/sql"
14
14
"database/sql/driver"
15
15
"encoding/binary"
16
+ "fmt"
16
17
"testing"
17
18
"time"
18
19
)
@@ -293,41 +294,23 @@ func TestIsolationLevelMapping(t *testing.T) {
293
294
}
294
295
}
295
296
296
- func TestParseDateTime (t * testing.T ) {
297
- // UTC loc
298
- {
299
- str := "2020-05-13 21:30:45"
300
- t1 , err := parseDateTime (str , time .UTC )
301
- if err != nil {
302
- t .Error (err )
303
- return
304
- }
305
- t2 := time .Date (2020 , 5 , 13 ,
306
- 21 , 30 , 45 , 0 , time .UTC )
307
- if ! t1 .Equal (t2 ) {
308
- t .Errorf ("want equal, have: %v, want: %v" , t1 , t2 )
309
- return
310
- }
311
- }
312
- // non-UTC loc
313
- {
314
- str := "2020-05-13 21:30:45"
315
- loc := time .FixedZone ("test" , 8 * 60 * 60 )
316
- t1 , err := parseDateTime (str , loc )
317
- if err != nil {
318
- t .Error (err )
297
+ func deprecatedParseDateTime (str string , loc * time.Location ) (t time.Time , err error ) {
298
+ switch len (str ) {
299
+ case 10 , 19 , 21 , 22 , 23 , 24 , 25 , 26 : // up to "YYYY-MM-DD HH:MM:SS.MMMMMM"
300
+ if str == nullTimeBaseStr [:len (str )] {
319
301
return
320
302
}
321
- t2 := time .Date (2020 , 5 , 13 ,
322
- 21 , 30 , 45 , 0 , loc )
323
- if ! t1 .Equal (t2 ) {
324
- t .Errorf ("want equal, have: %v, want: %v" , t1 , t2 )
325
- return
303
+ if loc == time .UTC {
304
+ return time .Parse (timeFormat [:len (str )], str )
326
305
}
306
+ return time .ParseInLocation (timeFormat [:len (str )], str , loc )
307
+ default :
308
+ err = fmt .Errorf ("invalid time string: %s" , str )
309
+ return
327
310
}
328
311
}
329
312
330
- func TestParseByteDateTime (t * testing.T ) {
313
+ func TestParseDateTime (t * testing.T ) {
331
314
cases := []struct {
332
315
name string
333
316
str string
@@ -380,11 +363,11 @@ func TestParseByteDateTime(t *testing.T) {
380
363
} {
381
364
for _ , cc := range cases {
382
365
t .Run (cc .name + "-" + loc .String (), func (t * testing.T ) {
383
- want , err := parseDateTime (cc .str , loc )
366
+ want , err := deprecatedParseDateTime (cc .str , loc )
384
367
if err != nil {
385
368
t .Fatal (err )
386
369
}
387
- got , err := parseByteDateTime ([]byte (cc .str ), loc )
370
+ got , err := parseDateTime ([]byte (cc .str ), loc )
388
371
if err != nil {
389
372
t .Fatal (err )
390
373
}
@@ -397,7 +380,7 @@ func TestParseByteDateTime(t *testing.T) {
397
380
}
398
381
}
399
382
400
- func TestParseByteDateTimeFail (t * testing.T ) {
383
+ func TestParseDateTimeFail (t * testing.T ) {
401
384
cases := []struct {
402
385
name string
403
386
str string
@@ -452,7 +435,7 @@ func TestParseByteDateTimeFail(t *testing.T) {
452
435
453
436
for _ , cc := range cases {
454
437
t .Run (cc .name , func (t * testing.T ) {
455
- got , err := parseByteDateTime ([]byte (cc .str ), time .UTC )
438
+ got , err := parseDateTime ([]byte (cc .str ), time .UTC )
456
439
if err == nil {
457
440
t .Fatal ("want error" )
458
441
}
@@ -470,7 +453,7 @@ func BenchmarkParseDateTime(b *testing.B) {
470
453
str := "2020-05-13 21:30:45"
471
454
loc := time .FixedZone ("test" , 8 * 60 * 60 )
472
455
for i := 0 ; i < b .N ; i ++ {
473
- _ , _ = parseDateTime (str , loc )
456
+ _ , _ = deprecatedParseDateTime (str , loc )
474
457
}
475
458
}
476
459
@@ -479,7 +462,7 @@ func BenchmarkParseByteDateTime(b *testing.B) {
479
462
loc := time .FixedZone ("test" , 8 * 60 * 60 )
480
463
b .ResetTimer ()
481
464
for i := 0 ; i < b .N ; i ++ {
482
- _ , _ = parseByteDateTime (bStr , loc )
465
+ _ , _ = parseDateTime (bStr , loc )
483
466
}
484
467
}
485
468
@@ -488,6 +471,6 @@ func BenchmarkParseByteDateTimeStringCast(b *testing.B) {
488
471
loc := time .FixedZone ("test" , 8 * 60 * 60 )
489
472
b .ResetTimer ()
490
473
for i := 0 ; i < b .N ; i ++ {
491
- _ , _ = parseDateTime (string (bStr ), loc )
474
+ _ , _ = deprecatedParseDateTime (string (bStr ), loc )
492
475
}
493
476
}
0 commit comments