-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
example_custom_test.go
76 lines (59 loc) · 1.24 KB
/
example_custom_test.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
package pg_test
import (
"fmt"
"time"
"github.com/go-pg/pg/v10/orm"
"github.com/go-pg/pg/v10/types"
)
const pgTimeFormat = "15:04:05.999999999"
type Time struct {
time.Time
}
var _ types.ValueAppender = (*Time)(nil)
func (tm Time) AppendValue(b []byte, flags int) ([]byte, error) {
if flags == 1 {
b = append(b, '\'')
}
b = tm.UTC().AppendFormat(b, pgTimeFormat)
if flags == 1 {
b = append(b, '\'')
}
return b, nil
}
var _ types.ValueScanner = (*Time)(nil)
func (tm *Time) ScanValue(rd types.Reader, n int) error {
if n <= 0 {
tm.Time = time.Time{}
return nil
}
tmp, err := rd.ReadFullTemp()
if err != nil {
return err
}
tm.Time, err = time.ParseInLocation(pgTimeFormat, string(tmp), time.UTC)
if err != nil {
return err
}
return nil
}
type Event struct {
Id int
Time Time `pg:"type:time"`
}
func ExampleDB_Model_customType() {
db := connect()
defer db.Close()
err := db.Model((*Event)(nil)).CreateTable(&orm.CreateTableOptions{
Temp: true,
})
panicIf(err)
_, err = db.Model(&Event{
Time: Time{time.Date(0, 0, 0, 12, 00, 00, 00, time.UTC)}, // noon
}).Insert()
panicIf(err)
evt := new(Event)
err = db.Model(evt).Select()
panicIf(err)
fmt.Println(evt.Time)
// Output: 0000-01-01 12:00:00 +0000 UTC
}