Skip to content

Commit 0b75396

Browse files
committed
Inline datetime formatting
benchmark old ns/op new ns/op delta BenchmarkInterpolation 2536 2209 -12.89% benchmark old allocs new allocs delta BenchmarkInterpolation 6 4 -33.33% benchmark old bytes new bytes delta BenchmarkInterpolation 560 496 -11.43%
1 parent 0297315 commit 0b75396

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

connection.go

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,50 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
240240
if v.IsZero() {
241241
buf = append(buf, []byte("'0000-00-00'")...)
242242
} else {
243-
fmt := "'2006-01-02 15:04:05.999999'"
244-
if v.Nanosecond() == 0 {
245-
fmt = "'2006-01-02 15:04:05'"
243+
v := v.In(mc.cfg.loc)
244+
year := v.Year()
245+
month := v.Month()
246+
day := v.Day()
247+
hour := v.Hour()
248+
minute := v.Minute()
249+
second := v.Second()
250+
micro := v.Nanosecond() / 1000
251+
252+
buf = append(buf, []byte{
253+
byte('\''),
254+
byte('0' + year/1000),
255+
byte('0' + year/100%10),
256+
byte('0' + year/10%10),
257+
byte('0' + year%10),
258+
byte('-'),
259+
byte('0' + month/10),
260+
byte('0' + month%10),
261+
byte('-'),
262+
byte('0' + day/10),
263+
byte('0' + day%10),
264+
byte(' '),
265+
byte('0' + hour/10),
266+
byte('0' + hour%10),
267+
byte(':'),
268+
byte('0' + minute/10),
269+
byte('0' + minute%10),
270+
byte(':'),
271+
byte('0' + second/10),
272+
byte('0' + second%10),
273+
}...)
274+
275+
if micro != 0 {
276+
buf = append(buf, []byte{
277+
byte('.'),
278+
byte('0' + micro/100000),
279+
byte('0' + micro/10000%10),
280+
byte('0' + micro/1000%10),
281+
byte('0' + micro/100%10),
282+
byte('0' + micro/10%10),
283+
byte('0' + micro%10),
284+
}...)
246285
}
247-
s := v.In(mc.cfg.loc).Format(fmt)
248-
buf = append(buf, []byte(s)...)
286+
buf = append(buf, '\'')
249287
}
250288
case []byte:
251289
if v == nil {

0 commit comments

Comments
 (0)