-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
58 lines (49 loc) · 1.08 KB
/
time.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
package pg
import (
"bytes"
"database/sql"
"encoding/json"
"time"
"gopkg.in/pg.v5/types"
)
var jsonNull = []byte("null")
// NullTime is a time.Time wrapper that marshals zero time as JSON null and
// PostgreSQL NULL.
type NullTime struct {
time.Time
}
var _ json.Marshaler = (*NullTime)(nil)
var _ json.Unmarshaler = (*NullTime)(nil)
var _ sql.Scanner = (*NullTime)(nil)
var _ types.ValueAppender = (*NullTime)(nil)
func (tm NullTime) MarshalJSON() ([]byte, error) {
if tm.IsZero() {
return jsonNull, nil
}
return tm.Time.MarshalJSON()
}
func (tm *NullTime) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, jsonNull) {
tm.Time = time.Time{}
return nil
}
return tm.Time.UnmarshalJSON(b)
}
func (tm NullTime) AppendValue(b []byte, quote int) ([]byte, error) {
if tm.IsZero() {
return types.AppendNull(b, quote), nil
}
return types.AppendTime(b, tm.Time, quote), nil
}
func (tm *NullTime) Scan(b interface{}) error {
if b == nil {
tm.Time = time.Time{}
return nil
}
newtm, err := types.ParseTime(b.([]byte))
if err != nil {
return err
}
tm.Time = newtm
return nil
}