forked from jackc/pgtype
/
timestamptz.go
91 lines (71 loc) · 1.7 KB
/
timestamptz.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package zeronull
import (
"database/sql/driver"
"time"
"github.com/jackc/pgtype"
)
type Timestamptz time.Time
func (dst *Timestamptz) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
var nullable pgtype.Timestamptz
err := nullable.DecodeText(ci, src)
if err != nil {
return err
}
if nullable.Status == pgtype.Present {
*dst = Timestamptz(nullable.Time)
} else {
*dst = Timestamptz{}
}
return nil
}
func (dst *Timestamptz) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
var nullable pgtype.Timestamptz
err := nullable.DecodeBinary(ci, src)
if err != nil {
return err
}
if nullable.Status == pgtype.Present {
*dst = Timestamptz(nullable.Time)
} else {
*dst = Timestamptz{}
}
return nil
}
func (src Timestamptz) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
if (src == Timestamptz{}) {
return nil, nil
}
nullable := pgtype.Timestamptz{
Time: time.Time(src),
Status: pgtype.Present,
}
return nullable.EncodeText(ci, buf)
}
func (src Timestamptz) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
if (src == Timestamptz{}) {
return nil, nil
}
nullable := pgtype.Timestamptz{
Time: time.Time(src),
Status: pgtype.Present,
}
return nullable.EncodeBinary(ci, buf)
}
// Scan implements the database/sql Scanner interface.
func (dst *Timestamptz) Scan(src interface{}) error {
if src == nil {
*dst = Timestamptz{}
return nil
}
var nullable pgtype.Timestamptz
err := nullable.Scan(src)
if err != nil {
return err
}
*dst = Timestamptz(nullable.Time)
return nil
}
// Value implements the database/sql/driver Valuer interface.
func (src Timestamptz) Value() (driver.Value, error) {
return pgtype.EncodeValueText(src)
}