Debian Codegraph @

zero/time.go

raw symbols SCIP
1 package zero
2
3 import (
4 "database/sql/driver"
5 "encoding/json"
6 "fmt"
7 "reflect"
8 "time"
9 )
10
11 // Time is a nullable time.Time.
12 // JSON marshals to the zero value for time.Time if null.
13 // Considered to be null to SQL if zero.
14 type Time struct {
15 Time time.Time
16 Valid bool
17 }
18
19 // Scan implements Scanner interface.
20 func (t *Time) Scan(value interface{}) error {
21 var err error
22 switch x := value.(type) {
23 case time.Time:
24 t.Time = x
25 case nil:
26 t.Valid = false
27 return nil
28 default:
29 err = fmt.Errorf("null: cannot scan type %T into null.Time: %v", value, value)
30 }
31 t.Valid = err == nil
32 return err
33 }
34
35 // Value implements the driver Valuer interface.
36 func (t Time) Value() (driver.Value, error) {
37 if !t.Valid {
38 return nil, nil
39 }
40 return t.Time, nil
41 }
42
43 // NewTime creates a new Time.
44 func NewTime(t time.Time, valid bool) Time {
45 return Time{
46 Time: t,
47 Valid: valid,
48 }
49 }
50
51 // TimeFrom creates a new Time that will
52 // be null if t is the zero value.
53 func TimeFrom(t time.Time) Time {
54 return NewTime(t, !t.IsZero())
55 }
56
57 // TimeFromPtr creates a new Time that will
58 // be null if t is nil or *t is the zero value.
59 func TimeFromPtr(t *time.Time) Time {
60 if t == nil {
61 return NewTime(time.Time{}, false)
62 }
63 return TimeFrom(*t)
64 }
65
66 // ValueOrZero returns the inner value if valid, otherwise zero.
67 func (t Time) ValueOrZero() time.Time {
68 if !t.Valid {
69 return time.Time{}
70 }
71 return t.Time
72 }
73
74 // MarshalJSON implements json.Marshaler.
75 // It will encode the zero value of time.Time
76 // if this time is invalid.
77 func (t Time) MarshalJSON() ([]byte, error) {
78 if !t.Valid {
79 return (time.Time{}).MarshalJSON()
80 }
81 return t.Time.MarshalJSON()
82 }
83
84 // UnmarshalJSON implements json.Unmarshaler.
85 // It supports string, object (e.g. pq.NullTime and friends)
86 // and null input.
87 func (t *Time) UnmarshalJSON(data []byte) error {
88 var err error
89 var v interface{}
90 if err = json.Unmarshal(data, &v); err != nil {
91 return err
92 }
93 switch x := v.(type) {
94 case string:
95 var ti time.Time
96 if err = ti.UnmarshalJSON(data); err != nil {
97 return err
98 }
99 *t = TimeFrom(ti)
100 return nil
101 case map[string]interface{}:
102 ti, tiOK := x["Time"].(string)
103 valid, validOK := x["Valid"].(bool)
104 if !tiOK || !validOK {
105 return fmt.Errorf(`json: unmarshalling object into Go value of type null.Time requires key "Time" to be of type string and key "Valid" to be of type bool; found %T and %T, respectively`, x["Time"], x["Valid"])
106 }
107 err = t.Time.UnmarshalText([]byte(ti))
108 t.Valid = valid
109 return err
110 case nil:
111 t.Valid = false
112 return nil
113 default:
114 return fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Time", reflect.TypeOf(v).Name())
115 }
116 }
117
118 func (t Time) MarshalText() ([]byte, error) {
119 ti := t.Time
120 if !t.Valid {
121 ti = time.Time{}
122 }
123 return ti.MarshalText()
124 }
125
126 func (t *Time) UnmarshalText(text []byte) error {
127 str := string(text)
128 if str == "" || str == "null" {
129 t.Valid = false
130 return nil
131 }
132 if err := t.Time.UnmarshalText(text); err != nil {
133 return err
134 }
135 t.Valid = true
136 return nil
137 }
138
139 // SetValid changes this Time's value and
140 // sets it to be non-null.
141 func (t *Time) SetValid(v time.Time) {
142 t.Time = v
143 t.Valid = true
144 }
145
146 // Ptr returns a pointer to this Time's value,
147 // or a nil pointer if this Time is zero.
148 func (t Time) Ptr() *time.Time {
149 if !t.Valid {
150 return nil
151 }
152 return &t.Time
153 }
154
155 // IsZero returns true for null or zero Times, for potential future omitempty support.
156 func (t Time) IsZero() bool {
157 return !t.Valid || t.Time.IsZero()
158 }
159
160 // Equal returns true if both Time objects encode the same time or are both are either null or zero.
161 // Two times can be equal even if they are in different locations.
162 // For example, 6:00 +0200 CEST and 4:00 UTC are Equal.
163 func (t Time) Equal(other Time) bool {
164 return t.ValueOrZero().Equal(other.ValueOrZero())
165 }
166
167 // ExactEqual returns true if both Time objects are equal or both are either null or zero.
168 // ExactEqual returns false for times that are in different locations or
169 // have a different monotonic clock reading.
170 func (t Time) ExactEqual(other Time) bool {
171 return t.ValueOrZero() == other.ValueOrZero()
172 }