MCPcopy Create free account
hub / github.com/PostHog/duckgres / decodeInterval

Function decodeInterval

server/types.go:626–671  ·  view source on GitHub ↗

decodeInterval decodes PostgreSQL binary INTERVAL (16 bytes) into an interval string.

(data []byte)

Source from the content-addressed store, hash-verified

624
625// decodeInterval decodes PostgreSQL binary INTERVAL (16 bytes) into an interval string.
626func decodeInterval(data []byte) (string, error) {
627 if len(data) < 16 {
628 return "", fmt.Errorf("insufficient data for interval: got %d bytes, need 16", len(data))
629 }
630 micros := int64(binary.BigEndian.Uint64(data[0:8]))
631 days := int32(binary.BigEndian.Uint32(data[8:12]))
632 months := int32(binary.BigEndian.Uint32(data[12:16]))
633
634 var parts []string
635 if months != 0 {
636 years := months / 12
637 remMonths := months % 12
638 if years != 0 {
639 parts = append(parts, fmt.Sprintf("%d year", years))
640 }
641 if remMonths != 0 {
642 parts = append(parts, fmt.Sprintf("%d month", remMonths))
643 }
644 }
645 if days != 0 {
646 parts = append(parts, fmt.Sprintf("%d day", days))
647 }
648 if micros != 0 || len(parts) == 0 {
649 neg := micros < 0
650 if neg {
651 micros = -micros
652 }
653 h := micros / 3600000000
654 micros %= 3600000000
655 m := micros / 60000000
656 micros %= 60000000
657 s := micros / 1000000
658 remainMicros := micros % 1000000
659 var timePart string
660 if remainMicros > 0 {
661 timePart = fmt.Sprintf("%02d:%02d:%02d.%06d", h, m, s, remainMicros)
662 } else {
663 timePart = fmt.Sprintf("%02d:%02d:%02d", h, m, s)
664 }
665 if neg {
666 timePart = "-" + timePart
667 }
668 parts = append(parts, timePart)
669 }
670 return strings.Join(parts, " "), nil
671}
672
673// encodeNumeric encodes a value in PostgreSQL binary numeric format.
674//

Callers 3

TestEncodeDecodeIntervalFunction · 0.85
decodeBinaryCopyFunction · 0.85
decodeBinaryFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestEncodeDecodeIntervalFunction · 0.68