-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nulltime.go
124 lines (112 loc) · 2.75 KB
/
nulltime.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package toki
import (
"database/sql/driver"
)
// NullTime is a nullable Time.
type NullTime struct {
Time
Valid bool
}
// UnmarshalText implements the encoding TextUnmarshaler interface.
// Empty strings and "null" will be considered null.
func (t *NullTime) UnmarshalText(text []byte) error {
t.Valid = true
str := string(text)
if str == "" || str == "null" {
t.Valid = false
return nil
}
return t.Time.UnmarshalText(text)
}
// UnmarshalJSON implements the JSON Unmarshaler interface.
// Empty strings and "null" will be considered null.
func (t *NullTime) UnmarshalJSON(data []byte) error {
t.Valid = true
text := string(data)
if text == `""` || text == "null" {
t.Valid = false
return nil
}
return t.UnmarshalText(data[1 : len(data)-1])
}
// Scan implements the driver Scanner interface.
func (t *NullTime) Scan(src interface{}) error {
t.Valid = true
switch x := src.(type) {
case []byte:
if len(x) == 0 {
t.Valid = false
return nil
}
case string:
if x == "" || x == "null" {
t.Valid = false
return nil
}
case nil:
t.Valid = false
return nil
}
return t.Time.Scan(src)
}
// MarshalText implements the encoding TextMarshaler interface.
// Encodes to hh:mm:ss and omits the seconds if 0.
// Encodes to an empty string if null.
func (t NullTime) MarshalText() (text []byte, err error) {
if !t.Valid {
return []byte{}, nil
}
return t.Time.MarshalText()
}
// MarshalJSON implements the JSON Marshaler interface.
// Encodes to hh:mm:ss and omits the seconds if 0.
// Encodes to null if null.
func (t NullTime) MarshalJSON() ([]byte, error) {
if !t.Valid {
return []byte("null"), nil
}
text, _ := t.MarshalText()
// what is the best way to do this?
out := make([]byte, 0, len(text)+2)
out = append(out, '"')
out = append(out, text...)
out = append(out, '"')
return out, nil
}
// Value implements the driver Valuer interface.
func (t NullTime) Value() (driver.Value, error) {
if !t.Valid {
return nil, nil
}
return t.Time.MarshalText()
}
// String returns a string representation of this Time.
func (t NullTime) String() string {
text, _ := t.MarshalText()
return string(text)
}
// Equals returns true if this Time and the given Time are equal.
// If they are both null it will return true.
func (t NullTime) Equals(other NullTime) bool {
switch {
case !t.Valid && !other.Valid:
return true
case t.Valid != other.Valid:
return false
}
return t.Time.Equals(other.Time)
}
// ParseNullTime tries to parse the given time.
func ParseNullTime(text string) (NullTime, error) {
t := &NullTime{}
err := t.UnmarshalText([]byte(text))
return *t, err
}
// MustParseNullTime parses the given time or panics.
func MustParseNullTime(text string) NullTime {
t, err := ParseNullTime(text)
if err != nil {
panic(err)
}
return t
}