forked from guregu/null
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime.go
More file actions
112 lines (99 loc) · 2.6 KB
/
time.go
File metadata and controls
112 lines (99 loc) · 2.6 KB
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
package null
import (
"encoding/json"
pq "github.com/lib/pq"
"reflect"
"time"
"fmt"
)
// Time is an even nuller nullable Time.
// It does not consider zero values to be null.
// It will decode to null, not zero, if null.
type Time struct {
pq.NullTime
}
// NewTime creates a new Time
func NewTime(t time.Time, valid bool) Time {
return Time{
NullTime: pq.NullTime{
Time: t,
Valid: valid,
},
}
}
// TimeFrom creates a new Time that will always be valid.
func TimeFrom(t time.Time) Time {
return NewTime(t, true)
}
// TimeFromPtr creates a new Time that be null if i is nil.
func TimeFromPtr(t *time.Time) Time {
if t == nil {
return NewTime(time.Time{}, false)
}
return NewTime(*t, true)
}
// UnmarshalJSON implements json.Unmarshaler.
// It supports null.Time JSON or nil values
// It also supports unmarshalling a pq.NullTime
func (t *Time) UnmarshalJSON(data []byte) error {
var err error
var v interface{}
json.Unmarshal(data, &v)
switch v.(type) {
case map[string]interface{}:
err = json.Unmarshal(data, &t.NullTime)
case nil:
t.Valid = false
return nil
default:
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Time", reflect.TypeOf(v).Name())
}
t.Valid = err == nil
return err
}
// UnmarshalText implements encoding.TextUnmarshaler.
// It will unmarshal to a null Int if the input is a blank or not an time.Time RFC3339.
// It will return an error if the input is not an time.Time RFC3339, blank, or "null".
func (t *Time) UnmarshalText(text []byte) error {
str := string(text)
if str == "" || str == "null" {
t.Valid = false
return nil
}
var err error
t.Time, err = time.Parse(time.RFC3339, str)
t.Valid = err == nil
return err
}
// MarshalJSON implements json.Marshaler.
// It will encode time.Time RFC3339 or null if this Time is null
func (t Time) MarshalJSON() ([]byte, error) {
if !t.Valid {
return []byte("null"), nil
}
return []byte(t.Time.Format(time.RFC3339)), nil
}
// MarshalText implements encoding.TextMarshaler.
// It will encode a blank string if this Time is null.
func (t Time) MarshalText() ([]byte, error) {
if !t.Valid {
return []byte{}, nil
}
return []byte(t.Time.Format(time.RFC3339)), nil
}
// SetValid changes this Time's value and also sets it to be non-null.
func (t *Time) SetValid(n time.Time) {
t.Time = n
t.Valid = true
}
// Ptr returns a pointer to this Time's value, or a nil pointer if this Time is null.
func (t Time) Ptr() *time.Time {
if !t.Valid {
return nil
}
return &t.Time
}
// IsZero returns true for invalid Times, for future omitempty support (Go 1.4?)
func (t Time) IsZero() bool {
return !t.Valid
}