-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.go
280 lines (247 loc) · 7.2 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package nullable
import (
"bytes"
"database/sql/driver"
"encoding/json"
"fmt"
"io"
"time"
"github.com/domonda/go-pretty"
)
// TimeNull is a null Time value.
//
// Note: use Time.IsNull or IsNotNull to check for null
// instead of comparing a Time with TimeNull
// because Time.IsNull uses time.Time.IsZero internally
// which can return true for times that are not
// the empty time.Time{} default value.
var TimeNull Time
// Time represents a time.Time where the zero time instant
// (which is the empty default value of the type)
// is interpreted as SQL NULL and JSON null.
// It implements the sql.Scanner and driver.Valuer interfaces
// and also json.Marshaler and json.Unmarshaler.
// It assumes that zero time instant is never used
// in any real life application so it's free
// to be used as magical value for representing NULL.
// IsNull uses time.Time.IsZero internally.
type Time struct {
time.Time
}
// TimeNow returns the current time
func TimeNow() Time {
return Time{Time: time.Now()}
}
// TimeParse parses a time value with the provided layout
// using time.Parse(layout, value)
// except for when value is on of "", "null", "NULL",
// then a null/zero time and no error are returned.
func TimeParse(layout, value string) (Time, error) {
if value == "" || value == "null" || value == "NULL" {
return Time{}, nil
}
t, err := time.Parse(layout, value)
if err != nil {
return Time{}, err
}
return Time{Time: t}, nil
}
// TimeParseInLocation parses a time value with the provided layout
// and location using time.ParseInLocation(layout, value, loc)
// except for when value is on of "", "null", "NULL",
// then a null/zero time and no error are returned.
func TimeParseInLocation(layout, value string, loc *time.Location) (Time, error) {
if value == "" || value == "null" || value == "NULL" {
return Time{}, nil
}
t, err := time.ParseInLocation(layout, value, loc)
if err != nil {
return Time{}, err
}
return Time{Time: t}, nil
}
// TimeFrom returns a nullable.Time from a time.Time
func TimeFrom(t time.Time) Time {
return Time{Time: t}
}
// TimeFromPtr returns a nullable.Time from a time.Time pointer
// with nil interpreted as null.
func TimeFromPtr(ptr *time.Time) Time {
if ptr == nil {
return Time{}
}
return Time{Time: *ptr}
}
// Ptr returns a pointer to Time or nil if n is null
func (n Time) Ptr() *time.Time {
if n.IsNull() {
return nil
}
return &n.Time
}
// UTC returns the time in UTC or null if n is null
func (n Time) UTC() Time {
if n.IsNull() {
return n
}
return Time{Time: n.Time.UTC()}
}
// Add returns n+duration or null if n is null
func (n Time) Add(duration time.Duration) Time {
if n.IsNull() {
return n
}
return Time{Time: n.Time.Add(duration)}
}
// AddDate returns the nullable time corresponding to adding the
// given number of years, months, and days to t.
// For example, AddDate(-1, 2, 3) applied to January 1, 2011
// returns March 4, 2010.
//
// AddDate normalizes its result in the same way that Date does,
// so, for example, adding one month to October 31 yields
// December 1, the normalized form for November 31.
//
// Returns null if n is null.
func (n Time) AddDate(years int, months int, days int) Time {
if n.IsNull() {
return n
}
return Time{Time: n.Time.AddDate(years, months, days)}
}
// Equal reports whether n and o represent the same time instant
// or both are null.
// Two times can be equal even if they are in different locations.
// For example, 6:00 +0200 and 4:00 UTC are Equal.
// See the documentation on the Time type for the pitfalls of using == with
// Time values; most code should use Equal instead.
func (n Time) Equal(o Time) bool {
return (n.IsNull() && o.IsNull()) || n.Time.Equal(o.Time)
}
// IsNull returns true if the Time is null.
// Uses time.Time.IsZero internally.
// IsNull implements the Nullable interface.
func (n Time) IsNull() bool {
return n.Time.IsZero()
}
// IsNotNull returns true if the Time is not null.
// Uses time.Time.IsZero internally.
func (n Time) IsNotNull() bool {
return !n.IsNull()
}
// String returns Time.String() or "NULL" if n is null.
func (n Time) String() string {
return n.StringOr("NULL")
}
// StringOr returns Time.String() or the passed nullStr if n is null.
func (n Time) StringOr(nullStr string) string {
if n.IsNull() {
return nullStr
}
return n.Time.String()
}
// Format the time using time.Time.Format
// or return and empty string if n is null.
func (n Time) Format(layout string) string {
if n.IsNull() {
return ""
}
return n.Time.Format(layout)
}
// AppendFormat the time to b using time.Time.AppendFormat
// or b if n is null.
func (n Time) AppendFormat(b []byte, layout string) []byte {
if n.IsNull() {
return b
}
return n.Time.AppendFormat(b, layout)
}
// Get returns the non nullable time.Time value
// or panics if the Time is null.
// Note: check with IsNull before using Get!
func (n Time) Get() time.Time {
if n.IsNull() {
panic("NULL nullable.Time")
}
return n.Time
}
// GetOr returns the non nullable time.Time value
// or the passed defaultTime if the Time is null.
func (n Time) GetOr(defaultTime time.Time) time.Time {
if n.IsNull() {
return defaultTime
}
return n.Time
}
// Set a time.Time.
// Note that if t.IsZero() then n will be set to null.
func (n *Time) Set(t time.Time) {
n.Time = t
}
// SetNull sets the time to its null value
func (n *Time) SetNull() {
n.Time = time.Time{}
}
// Scan implements the database/sql.Scanner interface.
func (n *Time) Scan(value any) error {
switch t := value.(type) {
case nil:
*n = Time{}
return nil
case time.Time:
n.Time = t
return nil
default:
return fmt.Errorf("can't scan %T as nullable.Time", value)
}
}
// Value implements the driver database/sql/driver.Valuer interface.
func (n Time) Value() (driver.Value, error) {
if n.IsNull() {
return nil, nil
}
return n.Time, nil
}
// UnarshalJSON implements encoding/json.Unmarshaler.
// Interprets []byte(nil), []byte(""), []byte("null") as null.
func (n *Time) UnmarshalJSON(sourceJSON []byte) error {
if len(sourceJSON) == 0 || bytes.Equal(sourceJSON, []byte("null")) /*|| bytes.Equal(sourceJSON, []byte(`"NULL"`))*/ {
*n = Time{}
return nil
}
return json.Unmarshal(sourceJSON, &n.Time)
}
// MarshalJSON implements encoding/json.Marshaler
func (n Time) MarshalJSON() ([]byte, error) {
if n.IsNull() {
return []byte("null"), nil
}
return json.Marshal(n.Time)
}
// MarshalText implements the encoding.TextMarshaler interface.
// The time is formatted in RFC 3339 format, with sub-second precision added if present.
// "NULL" is returned as text if the time is null.
func (n Time) MarshalText() ([]byte, error) {
if n.IsNull() {
return []byte("NULL"), nil
}
return n.Time.MarshalText()
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
// The time is expected to be in RFC 3339 format.
// Empty text, "null", or "NULL" will set the time to null.
func (n *Time) UnmarshalText(text []byte) error {
if len(text) == 0 || bytes.EqualFold(text, []byte("NULL")) {
n.SetNull()
return nil
}
return n.Time.UnmarshalText(text)
}
// PrettyPrint implements the pretty.Printable interface
func (n Time) PrettyPrint(w io.Writer) {
if n.IsNull() {
w.Write([]byte("null")) //#nosec G104 -- go-pretty does not check write errors
} else {
pretty.Fprint(w, n.Time)
}
}