| 445 | } |
| 446 | |
| 447 | func 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 * |