Skip to content

Commit

Permalink
Allow scanning time without time zone into string
Browse files Browse the repository at this point in the history
  • Loading branch information
jackc committed May 10, 2024
1 parent 579a320 commit 48cdd7b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions pgtype/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ func (TimeCodec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPlan
switch target.(type) {
case TimeScanner:
return scanPlanBinaryTimeToTimeScanner{}
case TextScanner:
return scanPlanBinaryTimeToTextScanner{}
}
case TextFormatCode:
switch target.(type) {
Expand Down Expand Up @@ -170,6 +172,34 @@ func (scanPlanBinaryTimeToTimeScanner) Scan(src []byte, dst any) error {
return scanner.ScanTime(Time{Microseconds: usec, Valid: true})
}

type scanPlanBinaryTimeToTextScanner struct{}

func (scanPlanBinaryTimeToTextScanner) Scan(src []byte, dst any) error {
ts, ok := (dst).(TextScanner)
if !ok {
return ErrScanTargetTypeChanged
}

if src == nil {
return ts.ScanText(Text{})
}

if len(src) != 8 {
return fmt.Errorf("invalid length for time: %v", len(src))
}

usec := int64(binary.BigEndian.Uint64(src))

tim := Time{Microseconds: usec, Valid: true}

buf, err := TimeCodec{}.PlanEncode(nil, 0, TextFormatCode, tim).Encode(tim, nil)
if err != nil {
return err
}

return ts.ScanText(Text{String: string(buf), Valid: true})
}

type scanPlanTextAnyToTimeScanner struct{}

func (scanPlanTextAnyToTimeScanner) Scan(src []byte, dst any) error {
Expand Down

0 comments on commit 48cdd7b

Please sign in to comment.