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