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

Function formatBinaryTime

utils.go:447–487  ·  view source on GitHub ↗
(src []byte, length uint8)

Source from the content-addressed store, hash-verified

445}
446
447func formatBinaryTime(src []byte, length uint8) (driver.Value, error) {
448 // length expects the deterministic length of the zero value,
449 // negative time and 100+ hours are automatically added if needed
450 if len(src) == 0 {
451 return zeroDateTime[11 : 11+length], nil
452 }
453 var dst []byte // return value
454
455 switch length {
456 case
457 8, // time (can be up to 10 when negative and 100+ hours)
458 10, 11, 12, 13, 14, 15: // time with fractional seconds
459 default:
460 return nil, fmt.Errorf("illegal TIME length %d", length)
461 }
462 switch len(src) {
463 case 8, 12:
464 default:
465 return nil, fmt.Errorf("invalid TIME packet length %d", len(src))
466 }
467 // +2 to enable negative time and 100+ hours
468 dst = make([]byte, 0, length+2)
469 if src[0] == 1 {
470 dst = append(dst, '-')
471 }
472 days := binary.LittleEndian.Uint32(src[1:5])
473 hours := int64(days)*24 + int64(src[5])
474
475 if hours >= 100 {
476 dst = strconv.AppendInt(dst, hours, 10)
477 } else {
478 dst = append(dst, digits10[hours], digits01[hours])
479 }
480
481 min, sec := src[6], src[7]
482 dst = append(dst, ':',
483 digits10[min], digits01[min], ':',
484 digits10[sec], digits01[sec],
485 )
486 return appendMicrosecs(dst, src[8:], int(length)-9), nil
487}
488
489/******************************************************************************
490* Convert from and to bytes *

Callers 2

TestFormatBinaryTimeFunction · 0.85
readRowMethod · 0.85

Calls 1

appendMicrosecsFunction · 0.85

Tested by 1

TestFormatBinaryTimeFunction · 0.68