-
Notifications
You must be signed in to change notification settings - Fork 2.3k
utils: parse using byteslice in parseDateTime #1113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
julienschmidt
merged 11 commits into
go-sql-driver:master
from
Code-Hex:fix/parse-byte-time
May 31, 2020
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ad990db
fixed the way of parsing datetime when byte slice string
Code-Hex 5201e8a
added line to AUTHORS file
Code-Hex 3a0b99c
fixed error handling
Code-Hex 128451e
fixed nanosec digits
Code-Hex b7a5492
added more tests for error
Code-Hex 7ba45c5
renamed parseByteDateTime to parseDateTime
Code-Hex c5a4dd2
reverted base null time
Code-Hex 9e639e4
Update utils.go
Code-Hex 0e3cd50
Update utils.go
Code-Hex 32ad982
Update utils.go
Code-Hex 2c6aa27
removed deprecatedParseDateTime from test
Code-Hex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import ( | |
"database/sql" | ||
"database/sql/driver" | ||
"encoding/binary" | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
@@ -293,44 +294,184 @@ func TestIsolationLevelMapping(t *testing.T) { | |
} | ||
} | ||
|
||
func TestParseDateTime(t *testing.T) { | ||
// UTC loc | ||
{ | ||
str := "2020-05-13 21:30:45" | ||
t1, err := parseDateTime(str, time.UTC) | ||
if err != nil { | ||
t.Error(err) | ||
func deprecatedParseDateTime(str string, loc *time.Location) (t time.Time, err error) { | ||
const base = "0000-00-00 00:00:00.000000" | ||
switch len(str) { | ||
case 10, 19, 21, 22, 23, 24, 25, 26: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM" | ||
if str == base[:len(str)] { | ||
return | ||
} | ||
t2 := time.Date(2020, 5, 13, | ||
21, 30, 45, 0, time.UTC) | ||
if !t1.Equal(t2) { | ||
t.Errorf("want equal, have: %v, want: %v", t1, t2) | ||
return | ||
if loc == time.UTC { | ||
return time.Parse(timeFormat[:len(str)], str) | ||
} | ||
return time.ParseInLocation(timeFormat[:len(str)], str, loc) | ||
default: | ||
err = fmt.Errorf("invalid time string: %s", str) | ||
return | ||
} | ||
// non-UTC loc | ||
{ | ||
str := "2020-05-13 21:30:45" | ||
loc := time.FixedZone("test", 8*60*60) | ||
t1, err := parseDateTime(str, loc) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
t2 := time.Date(2020, 5, 13, | ||
21, 30, 45, 0, loc) | ||
if !t1.Equal(t2) { | ||
t.Errorf("want equal, have: %v, want: %v", t1, t2) | ||
return | ||
} | ||
|
||
func TestParseDateTime(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
str string | ||
}{ | ||
{ | ||
name: "parse date", | ||
str: "2020-05-13", | ||
}, | ||
{ | ||
name: "parse null date", | ||
str: sDate0, | ||
}, | ||
{ | ||
name: "parse datetime", | ||
str: "2020-05-13 21:30:45", | ||
}, | ||
{ | ||
name: "parse null datetime", | ||
str: sDateTime0, | ||
}, | ||
{ | ||
name: "parse datetime nanosec 1-digit", | ||
str: "2020-05-25 23:22:01.1", | ||
}, | ||
{ | ||
name: "parse datetime nanosec 2-digits", | ||
str: "2020-05-25 23:22:01.15", | ||
}, | ||
{ | ||
name: "parse datetime nanosec 3-digits", | ||
str: "2020-05-25 23:22:01.159", | ||
}, | ||
{ | ||
name: "parse datetime nanosec 4-digits", | ||
str: "2020-05-25 23:22:01.1594", | ||
}, | ||
{ | ||
name: "parse datetime nanosec 5-digits", | ||
str: "2020-05-25 23:22:01.15949", | ||
}, | ||
{ | ||
name: "parse datetime nanosec 6-digits", | ||
str: "2020-05-25 23:22:01.159491", | ||
}, | ||
} | ||
|
||
for _, loc := range []*time.Location{ | ||
time.UTC, | ||
time.FixedZone("test", 8*60*60), | ||
} { | ||
for _, cc := range cases { | ||
t.Run(cc.name+"-"+loc.String(), func(t *testing.T) { | ||
want, err := deprecatedParseDateTime(cc.str, loc) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
got, err := parseDateTime([]byte(cc.str), loc) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !want.Equal(got) { | ||
t.Fatalf("want: %v, but got %v", want, got) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
func TestParseDateTimeFail(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
str string | ||
wantErr string | ||
}{ | ||
{ | ||
name: "parse invalid time", | ||
str: "hello", | ||
wantErr: "invalid time bytes: hello", | ||
}, | ||
{ | ||
name: "parse year", | ||
str: "000!-00-00 00:00:00.000000", | ||
wantErr: "not [0-9]", | ||
}, | ||
{ | ||
name: "parse month", | ||
str: "0000-!0-00 00:00:00.000000", | ||
wantErr: "not [0-9]", | ||
}, | ||
{ | ||
name: `parse "-" after parsed year`, | ||
str: "0000:00-00 00:00:00.000000", | ||
wantErr: "bad value for field: `:`", | ||
}, | ||
{ | ||
name: `parse "-" after parsed month`, | ||
str: "0000-00:00 00:00:00.000000", | ||
wantErr: "bad value for field: `:`", | ||
}, | ||
{ | ||
name: `parse " " after parsed date`, | ||
str: "0000-00-00+00:00:00.000000", | ||
wantErr: "bad value for field: `+`", | ||
}, | ||
{ | ||
name: `parse ":" after parsed date`, | ||
str: "0000-00-00 00-00:00.000000", | ||
wantErr: "bad value for field: `-`", | ||
}, | ||
{ | ||
name: `parse ":" after parsed hour`, | ||
str: "0000-00-00 00:00-00.000000", | ||
wantErr: "bad value for field: `-`", | ||
}, | ||
{ | ||
name: `parse "." after parsed sec`, | ||
str: "0000-00-00 00:00:00?000000", | ||
wantErr: "bad value for field: `?`", | ||
}, | ||
} | ||
|
||
for _, cc := range cases { | ||
t.Run(cc.name, func(t *testing.T) { | ||
got, err := parseDateTime([]byte(cc.str), time.UTC) | ||
if err == nil { | ||
t.Fatal("want error") | ||
} | ||
if cc.wantErr != err.Error() { | ||
t.Fatalf("want `%s`, but got `%s`", cc.wantErr, err) | ||
} | ||
if !got.IsZero() { | ||
t.Fatal("want zero time") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkParseDateTime(b *testing.B) { | ||
str := "2020-05-13 21:30:45" | ||
loc := time.FixedZone("test", 8*60*60) | ||
for i := 0; i < b.N; i++ { | ||
_, _ = parseDateTime(str, loc) | ||
_, _ = deprecatedParseDateTime(str, loc) | ||
} | ||
} | ||
|
||
func BenchmarkParseByteDateTime(b *testing.B) { | ||
bStr := []byte("2020-05-25 23:22:01.159491") | ||
loc := time.FixedZone("test", 8*60*60) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
_, _ = parseDateTime(bStr, loc) | ||
} | ||
} | ||
|
||
func BenchmarkParseByteDateTimeStringCast(b *testing.B) { | ||
bStr := []byte("2020-05-25 23:22:01.159491") | ||
loc := time.FixedZone("test", 8*60*60) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
_, _ = deprecatedParseDateTime(string(bStr), loc) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please post the result of this benchmark and remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed 2c6aa27 |
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.