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

Function encodeTime

server/types.go:566–590  ·  view source on GitHub ↗

encodeTime encodes a TIME value as int64 microseconds since midnight (PostgreSQL binary format). DuckDB Go driver returns TIME as time.Time with date fixed to 0001-01-01.

(v interface{})

Source from the content-addressed store, hash-verified

564// encodeTime encodes a TIME value as int64 microseconds since midnight (PostgreSQL binary format).
565// DuckDB Go driver returns TIME as time.Time with date fixed to 0001-01-01.
566func encodeTime(v interface{}) []byte {
567 buf := make([]byte, 8)
568 var micros int64
569
570 switch val := v.(type) {
571 case time.Time:
572 micros = int64(val.Hour())*3600000000 + int64(val.Minute())*60000000 +
573 int64(val.Second())*1000000 + int64(val.Nanosecond())/1000
574 case string:
575 t, err := time.Parse("15:04:05", val)
576 if err != nil {
577 t, err = time.Parse("15:04:05.000000", val)
578 if err != nil {
579 return nil
580 }
581 }
582 micros = int64(t.Hour())*3600000000 + int64(t.Minute())*60000000 +
583 int64(t.Second())*1000000 + int64(t.Nanosecond())/1000
584 default:
585 return nil
586 }
587
588 binary.BigEndian.PutUint64(buf, uint64(micros))
589 return buf
590}
591
592// decodeTime decodes int64 microseconds since midnight into a time string.
593func decodeTime(data []byte) (string, error) {

Callers 2

TestEncodeDecodeTimeFunction · 0.85
encodeBinaryFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestEncodeDecodeTimeFunction · 0.68