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

Function decodeTimestamp

server/types.go:967–983  ·  view source on GitHub ↗
(data []byte)

Source from the content-addressed store, hash-verified

965}
966
967func decodeTimestamp(data []byte) (time.Time, error) {
968 if len(data) < 8 {
969 return time.Time{}, fmt.Errorf("insufficient data for timestamp: got %d bytes, need 8", len(data))
970 }
971 // Microseconds since PostgreSQL epoch (2000-01-01)
972 micros := int64(binary.BigEndian.Uint64(data))
973 // Use time.Unix to avoid time.Duration overflow for dates far from epoch.
974 // time.Duration is int64 nanoseconds, which overflows at ~292 years.
975 const pgEpochUnix int64 = 946684800 // 2000-01-01 00:00:00 UTC in Unix seconds
976 secs := micros / 1_000_000
977 remainMicros := micros % 1_000_000
978 if remainMicros < 0 {
979 secs--
980 remainMicros += 1_000_000
981 }
982 return time.Unix(pgEpochUnix+secs, remainMicros*1000).UTC(), nil
983}
984
985// decodeNumeric decodes PostgreSQL binary numeric format into a string.
986func decodeNumeric(data []byte) (string, error) {

Callers 4

TestDecodeTimestampFunction · 0.85
decodeBinaryCopyFunction · 0.85
decodeBinaryFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestDecodeTimestampFunction · 0.68