Skip to content

Commit 7ba45c5

Browse files
committed
renamed parseByteDateTime to parseDateTime
1 parent b7a5492 commit 7ba45c5

File tree

4 files changed

+23
-56
lines changed

4 files changed

+23
-56
lines changed

nulltime.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ func (nt *NullTime) Scan(value interface{}) (err error) {
2828
nt.Time, nt.Valid = v, true
2929
return
3030
case []byte:
31-
nt.Time, err = parseByteDateTime(v, time.UTC)
31+
nt.Time, err = parseDateTime(v, time.UTC)
3232
nt.Valid = (err == nil)
3333
return
3434
case string:
35-
nt.Time, err = parseDateTime(v, time.UTC)
35+
nt.Time, err = parseDateTime([]byte(v), time.UTC)
3636
nt.Valid = (err == nil)
3737
return
3838
}

packets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ func (rows *textRows) readRow(dest []driver.Value) error {
777777
switch rows.rs.columns[i].fieldType {
778778
case fieldTypeTimestamp, fieldTypeDateTime,
779779
fieldTypeDate, fieldTypeNewDate:
780-
dest[i], err = parseByteDateTime(
780+
dest[i], err = parseDateTime(
781781
dest[i].([]byte),
782782
mc.cfg.Loc,
783783
)

utils.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,7 @@ var (
112112
nullTimeBaseByte = []byte(nullTimeBaseStr)
113113
)
114114

115-
func parseDateTime(str string, loc *time.Location) (t time.Time, err error) {
116-
switch len(str) {
117-
case 10, 19, 21, 22, 23, 24, 25, 26: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM"
118-
if str == nullTimeBaseStr[:len(str)] {
119-
return
120-
}
121-
if loc == time.UTC {
122-
return time.Parse(timeFormat[:len(str)], str)
123-
}
124-
return time.ParseInLocation(timeFormat[:len(str)], str, loc)
125-
default:
126-
err = fmt.Errorf("invalid time string: %s", str)
127-
return
128-
}
129-
}
130-
131-
func parseByteDateTime(b []byte, loc *time.Location) (time.Time, error) {
115+
func parseDateTime(b []byte, loc *time.Location) (time.Time, error) {
132116
switch len(b) {
133117
case 10, 19, 21, 22, 23, 24, 25, 26: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM"
134118
if bytes.Compare(b, nullTimeBaseByte[:len(b)]) == 0 {

utils_test.go

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)