...
1 package zeronull
2
3 import (
4 "database/sql/driver"
5 "fmt"
6 "time"
7
8 "github.com/jackc/pgx/v5/pgtype"
9 )
10
11 type Timestamp time.Time
12
13 func (Timestamp) SkipUnderlyingTypePlan() {}
14
15 func (ts *Timestamp) ScanTimestamp(v pgtype.Timestamp) error {
16 if !v.Valid {
17 *ts = Timestamp{}
18 return nil
19 }
20
21 switch v.InfinityModifier {
22 case pgtype.Finite:
23 *ts = Timestamp(v.Time)
24 return nil
25 case pgtype.Infinity:
26 return fmt.Errorf("cannot scan Infinity into *time.Time")
27 case pgtype.NegativeInfinity:
28 return fmt.Errorf("cannot scan -Infinity into *time.Time")
29 default:
30 return fmt.Errorf("invalid InfinityModifier: %v", v.InfinityModifier)
31 }
32 }
33
34 func (ts Timestamp) TimestampValue() (pgtype.Timestamp, error) {
35 if time.Time(ts).IsZero() {
36 return pgtype.Timestamp{}, nil
37 }
38
39 return pgtype.Timestamp{Time: time.Time(ts), Valid: true}, nil
40 }
41
42
43 func (ts *Timestamp) Scan(src any) error {
44 if src == nil {
45 *ts = Timestamp{}
46 return nil
47 }
48
49 var nullable pgtype.Timestamp
50 err := nullable.Scan(src)
51 if err != nil {
52 return err
53 }
54
55 *ts = Timestamp(nullable.Time)
56
57 return nil
58 }
59
60
61 func (ts Timestamp) Value() (driver.Value, error) {
62 if time.Time(ts).IsZero() {
63 return nil, nil
64 }
65
66 return time.Time(ts), nil
67 }
68
View as plain text