Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 5 additions & 38 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,46 +245,13 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
if v.IsZero() {
buf = append(buf, "'0000-00-00'"...)
} else {
v := v.In(mc.cfg.Loc)
v = v.Add(time.Nanosecond * 500) // To round under microsecond
year := v.Year()
year100 := year / 100
year1 := year % 100
month := v.Month()
day := v.Day()
hour := v.Hour()
minute := v.Minute()
second := v.Second()
micro := v.Nanosecond() / 1000

buf = append(buf, []byte{
'\'',
digits10[year100], digits01[year100],
digits10[year1], digits01[year1],
'-',
digits10[month], digits01[month],
'-',
digits10[day], digits01[day],
' ',
digits10[hour], digits01[hour],
':',
digits10[minute], digits01[minute],
':',
digits10[second], digits01[second],
}...)

if micro != 0 {
micro10000 := micro / 10000
micro100 := micro / 100 % 100
micro1 := micro % 100
buf = append(buf, []byte{
'.',
digits10[micro10000], digits01[micro10000],
digits10[micro100], digits01[micro100],
digits10[micro1], digits01[micro1],
}...)
b, n, err := formatDateTime(v.In(mc.cfg.Loc))
if err != nil {
return "", err
}
buf = append(buf, '\'')
buf = append(buf, b[:n]...)
buf = append(buf, '\'')
}
case json.RawMessage:
buf = append(buf, '\'')
Expand Down
19 changes: 8 additions & 11 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,20 +1110,17 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
paramTypes[i+i] = byte(fieldTypeString)
paramTypes[i+i+1] = 0x00

var a [64]byte
var b = a[:0]

if v.IsZero() {
b = append(b, "0000-00-00"...)
paramValues = appendLengthEncodedInteger(paramValues, uint64(len("0000-00-00")))
paramValues = append(paramValues, "0000-00-00"...)
} else {
b = v.In(mc.cfg.Loc).AppendFormat(b, timeFormat)
b, n, err := formatDateTime(v.In(mc.cfg.Loc))
if err != nil {
return err
}
paramValues = appendLengthEncodedInteger(paramValues, uint64(n))
paramValues = append(paramValues, b[:n]...)
}

paramValues = appendLengthEncodedInteger(paramValues,
uint64(len(b)),
)
paramValues = append(paramValues, b...)

default:
return fmt.Errorf("cannot convert type: %T", arg)
}
Expand Down
51 changes: 51 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,57 @@ func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (driver.Va
return nil, fmt.Errorf("invalid DATETIME packet length %d", num)
}

func formatDateTime(t time.Time) (buf [32]byte, n int, err error) {
nsec := t.Nanosecond()
if nsec%1000 >= 500 {
t = t.Add(500 * time.Nanosecond) // to round under microsecond
nsec = t.Nanosecond()
}
year, month, day := t.Date()
hour, min, sec := t.Clock()
micro := nsec / 1000

if year < 1 || year > 9999 {
err = fmt.Errorf("invalid year: %d", year)
return
}
year100 := year / 100
year1 := year % 100

buf[0], buf[1], buf[2], buf[3] = digits10[year100], digits01[year100], digits10[year1], digits01[year1]
buf[4] = '-'
buf[5], buf[6] = digits10[month], digits01[month]
buf[7] = '-'
buf[8], buf[9] = digits10[day], digits01[day]

if hour == 0 && min == 0 && sec == 0 && micro == 0 {
n = 10
return
}

buf[10] = ' '
buf[11], buf[12] = digits10[hour], digits01[hour]
buf[13] = ':'
buf[14], buf[15] = digits10[min], digits01[min]
buf[16] = ':'
buf[17], buf[18] = digits10[sec], digits01[sec]

if micro == 0 {
n = 19
return
}

micro10000 := micro / 10000
micro100 := (micro / 100) % 100
micro1 := micro % 100
buf[19] = '.'
buf[20], buf[21], buf[22], buf[23], buf[24], buf[25] =
digits10[micro10000], digits01[micro10000], digits10[micro100], digits01[micro100], digits10[micro1], digits01[micro1]

n = 26
return
}

// zeroDateTime is used in formatBinaryDateTime to avoid an allocation
// if the DATE or DATETIME has the zero value.
// It must never be changed.
Expand Down
111 changes: 111 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,117 @@ func TestIsolationLevelMapping(t *testing.T) {
}
}

func TestFormatDateTime(t *testing.T) {
tests := []struct {
t time.Time
str string
}{
{
t: time.Date(2020, 05, 30, 0, 0, 0, 0, time.UTC),
str: "2020-05-30",
},
{
t: time.Date(2020, 05, 30, 22, 0, 0, 0, time.UTC),
str: "2020-05-30 22:00:00",
},
{
t: time.Date(2020, 05, 30, 22, 33, 0, 0, time.UTC),
str: "2020-05-30 22:33:00",
},
{
t: time.Date(2020, 05, 30, 22, 33, 44, 0, time.UTC),
str: "2020-05-30 22:33:44",
},
{
t: time.Date(2020, 05, 30, 22, 33, 44, 550000000, time.UTC),
str: "2020-05-30 22:33:44.550000",
},
{
t: time.Date(2020, 05, 30, 22, 33, 44, 550000499, time.UTC),
str: "2020-05-30 22:33:44.550000",
},
{
t: time.Date(2020, 05, 30, 22, 33, 44, 550000500, time.UTC),
str: "2020-05-30 22:33:44.550001",
},
{
t: time.Date(2020, 05, 30, 22, 33, 44, 550000567, time.UTC),
str: "2020-05-30 22:33:44.550001",
},
{
t: time.Date(2020, 05, 30, 22, 33, 44, 999999567, time.UTC),
str: "2020-05-30 22:33:45",
},
}
for _, v := range tests {
b, n, _ := formatDateTime(v.t)
if str := string(b[:n]); str != v.str {
t.Errorf("formatDateTime(%v), have: %s, want: %s", v.t, str, v.str)
return
}
}

// year out of range
{
v := time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC)
_, _, err := formatDateTime(v)
if err == nil {
t.Error("want an error")
return
}
}
{
v := time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC)
_, _, err := formatDateTime(v)
if err == nil {
t.Error("want an error")
return
}
}
}

func BenchmarkGetTimeDateClockIndependent(b *testing.B) {
t := time.Now()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.Year()
t.Month()
t.Day()
t.Hour()
t.Minute()
t.Second()
}
}

func BenchmarkGetTimeDateClockTogether(b *testing.B) {
t := time.Now()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.Date()
t.Clock()
}
}

func BenchmarkFormatDatetime(b *testing.B) {
t := time.Now()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
formatDateTime(t)
}
}

func BenchmarkFormatDatetimeViaStandardFormat(b *testing.B) {
t := time.Now()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.Format("2006-01-02 15:04:05.999999")
}
}

func TestParseDateTime(t *testing.T) {
// UTC loc
{
Expand Down