@@ -13,6 +13,7 @@ import (
1313 "database/sql"
1414 "database/sql/driver"
1515 "encoding/binary"
16+ "fmt"
1617 "testing"
1718 "time"
1819)
@@ -293,41 +294,23 @@ func TestIsolationLevelMapping(t *testing.T) {
293294 }
294295}
295296
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 )] {
319301 return
320302 }
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 )
326305 }
306+ return time .ParseInLocation (timeFormat [:len (str )], str , loc )
307+ default :
308+ err = fmt .Errorf ("invalid time string: %s" , str )
309+ return
327310 }
328311}
329312
330- func TestParseByteDateTime (t * testing.T ) {
313+ func TestParseDateTime (t * testing.T ) {
331314 cases := []struct {
332315 name string
333316 str string
@@ -380,11 +363,11 @@ func TestParseByteDateTime(t *testing.T) {
380363 } {
381364 for _ , cc := range cases {
382365 t .Run (cc .name + "-" + loc .String (), func (t * testing.T ) {
383- want , err := parseDateTime (cc .str , loc )
366+ want , err := deprecatedParseDateTime (cc .str , loc )
384367 if err != nil {
385368 t .Fatal (err )
386369 }
387- got , err := parseByteDateTime ([]byte (cc .str ), loc )
370+ got , err := parseDateTime ([]byte (cc .str ), loc )
388371 if err != nil {
389372 t .Fatal (err )
390373 }
@@ -397,7 +380,7 @@ func TestParseByteDateTime(t *testing.T) {
397380 }
398381}
399382
400- func TestParseByteDateTimeFail (t * testing.T ) {
383+ func TestParseDateTimeFail (t * testing.T ) {
401384 cases := []struct {
402385 name string
403386 str string
@@ -452,7 +435,7 @@ func TestParseByteDateTimeFail(t *testing.T) {
452435
453436 for _ , cc := range cases {
454437 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 )
456439 if err == nil {
457440 t .Fatal ("want error" )
458441 }
@@ -470,7 +453,7 @@ func BenchmarkParseDateTime(b *testing.B) {
470453 str := "2020-05-13 21:30:45"
471454 loc := time .FixedZone ("test" , 8 * 60 * 60 )
472455 for i := 0 ; i < b .N ; i ++ {
473- _ , _ = parseDateTime (str , loc )
456+ _ , _ = deprecatedParseDateTime (str , loc )
474457 }
475458}
476459
@@ -479,7 +462,7 @@ func BenchmarkParseByteDateTime(b *testing.B) {
479462 loc := time .FixedZone ("test" , 8 * 60 * 60 )
480463 b .ResetTimer ()
481464 for i := 0 ; i < b .N ; i ++ {
482- _ , _ = parseByteDateTime (bStr , loc )
465+ _ , _ = parseDateTime (bStr , loc )
483466 }
484467}
485468
@@ -488,6 +471,6 @@ func BenchmarkParseByteDateTimeStringCast(b *testing.B) {
488471 loc := time .FixedZone ("test" , 8 * 60 * 60 )
489472 b .ResetTimer ()
490473 for i := 0 ; i < b .N ; i ++ {
491- _ , _ = parseDateTime (string (bStr ), loc )
474+ _ , _ = deprecatedParseDateTime (string (bStr ), loc )
492475 }
493476}
0 commit comments