Skip to content

Commit c285e39

Browse files
committedFeb 10, 2015
Use digits10 and digits01 to format datetime.
1 parent 0c7ae46 commit c285e39

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed
 

‎connection.go

+22-27
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
253253
} else {
254254
v := v.In(mc.cfg.loc)
255255
year := v.Year()
256+
year100 := year / 100
257+
year1 := year % 100
256258
month := v.Month()
257259
day := v.Day()
258260
hour := v.Hour()
@@ -261,37 +263,30 @@ func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (strin
261263
micro := v.Nanosecond() / 1000
262264

263265
buf = append(buf, []byte{
264-
byte('\''),
265-
byte('0' + year/1000),
266-
byte('0' + year/100%10),
267-
byte('0' + year/10%10),
268-
byte('0' + year%10),
269-
byte('-'),
270-
byte('0' + month/10),
271-
byte('0' + month%10),
272-
byte('-'),
273-
byte('0' + day/10),
274-
byte('0' + day%10),
275-
byte(' '),
276-
byte('0' + hour/10),
277-
byte('0' + hour%10),
278-
byte(':'),
279-
byte('0' + minute/10),
280-
byte('0' + minute%10),
281-
byte(':'),
282-
byte('0' + second/10),
283-
byte('0' + second%10),
266+
'\'',
267+
digits10[year100], digits01[year100],
268+
digits10[year1], digits01[year1],
269+
'-',
270+
digits10[month], digits01[month],
271+
'-',
272+
digits10[day], digits01[day],
273+
' ',
274+
digits10[hour], digits01[hour],
275+
':',
276+
digits10[minute], digits01[minute],
277+
':',
278+
digits10[second], digits01[second],
284279
}...)
285280

286281
if micro != 0 {
282+
micro10000 := micro / 10000
283+
micro100 := micro / 100 % 100
284+
micro1 := micro % 100
287285
buf = append(buf, []byte{
288-
byte('.'),
289-
byte('0' + micro/100000),
290-
byte('0' + micro/10000%10),
291-
byte('0' + micro/1000%10),
292-
byte('0' + micro/100%10),
293-
byte('0' + micro/10%10),
294-
byte('0' + micro%10),
286+
'.',
287+
digits10[micro10000], digits01[micro10000],
288+
digits10[micro100], digits01[micro100],
289+
digits10[micro1], digits01[micro1],
295290
}...)
296291
}
297292
buf = append(buf, '\'')

‎utils.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,12 @@ func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (driver.Va
540540
// The current behavior depends on database/sql copying the result.
541541
var zeroDateTime = []byte("0000-00-00 00:00:00.000000")
542542

543+
const digits01 = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
544+
const digits10 = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
545+
543546
func formatBinaryDateTime(src []byte, length uint8, justTime bool) (driver.Value, error) {
544547
// length expects the deterministic length of the zero value,
545548
// negative time and 100+ hours are automatically added if needed
546-
const digits01 = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
547-
const digits10 = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
548549
if len(src) == 0 {
549550
if justTime {
550551
return zeroDateTime[11 : 11+length], nil

0 commit comments

Comments
 (0)
Please sign in to comment.