MCPcopy
hub / github.com/go-sql-driver/mysql / appendDateTime

Function appendDateTime

utils.go:268–328  ·  view source on GitHub ↗
(buf []byte, t time.Time, timeTruncate time.Duration)

Source from the content-addressed store, hash-verified

266}
267
268func appendDateTime(buf []byte, t time.Time, timeTruncate time.Duration) ([]byte, error) {
269 if timeTruncate > 0 {
270 t = t.Truncate(timeTruncate)
271 }
272
273 year, month, day := t.Date()
274 hour, min, sec := t.Clock()
275 nsec := t.Nanosecond()
276
277 if year < 1 || year > 9999 {
278 return buf, errors.New("year is not in the range [1, 9999]: " + strconv.Itoa(year)) // use errors.New instead of fmt.Errorf to avoid year escape to heap
279 }
280 year100 := year / 100
281 year1 := year % 100
282
283 var localBuf [len("2006-01-02T15:04:05.999999999")]byte // does not escape
284 localBuf[0], localBuf[1], localBuf[2], localBuf[3] = digits10[year100], digits01[year100], digits10[year1], digits01[year1]
285 localBuf[4] = '-'
286 localBuf[5], localBuf[6] = digits10[month], digits01[month]
287 localBuf[7] = '-'
288 localBuf[8], localBuf[9] = digits10[day], digits01[day]
289
290 if hour == 0 && min == 0 && sec == 0 && nsec == 0 {
291 return append(buf, localBuf[:10]...), nil
292 }
293
294 localBuf[10] = ' '
295 localBuf[11], localBuf[12] = digits10[hour], digits01[hour]
296 localBuf[13] = ':'
297 localBuf[14], localBuf[15] = digits10[min], digits01[min]
298 localBuf[16] = ':'
299 localBuf[17], localBuf[18] = digits10[sec], digits01[sec]
300
301 if nsec == 0 {
302 return append(buf, localBuf[:19]...), nil
303 }
304 nsec100000000 := nsec / 100000000
305 nsec1000000 := (nsec / 1000000) % 100
306 nsec10000 := (nsec / 10000) % 100
307 nsec100 := (nsec / 100) % 100
308 nsec1 := nsec % 100
309 localBuf[19] = '.'
310
311 // milli second
312 localBuf[20], localBuf[21], localBuf[22] =
313 digits01[nsec100000000], digits10[nsec1000000], digits01[nsec1000000]
314 // micro second
315 localBuf[23], localBuf[24], localBuf[25] =
316 digits10[nsec10000], digits01[nsec10000], digits10[nsec100]
317 // nano second
318 localBuf[26], localBuf[27], localBuf[28] =
319 digits01[nsec100], digits10[nsec1], digits01[nsec1]
320
321 // trim trailing zeros
322 n := len(localBuf)
323 for n > 0 && localBuf[n-1] == '0' {
324 n--
325 }

Callers 3

TestAppendDateTimeFunction · 0.85
writeExecutePacketMethod · 0.85
interpolateParamsMethod · 0.85

Calls

no outgoing calls

Tested by 1

TestAppendDateTimeFunction · 0.68