-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtime.go
61 lines (50 loc) · 1.36 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
package sqle
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"time"
)
// Time represents a nullable time value.
type Time struct {
sql.NullTime
}
// NewTime creates a new Time object with the given time and valid flag.
func NewTime(t time.Time, valid bool) Time {
return Time{NullTime: sql.NullTime{Time: t, Valid: valid}}
}
// Scan implements the [sql.Scanner] interface.
func (t *Time) Scan(value any) error { // skipcq: GO-W1029
return t.NullTime.Scan(value)
}
// Value implements the [driver.Valuer] interface.
func (t Time) Value() (driver.Value, error) { // skipcq: GO-W1029
return t.NullTime.Value()
}
// Time returns the underlying time.Time value of the Time struct.
func (t *Time) Time() time.Time { // skipcq: GO-W1029
return t.NullTime.Time
}
// MarshalJSON implements the json.Marshaler interface
func (t Time) MarshalJSON() ([]byte, error) { // skipcq: GO-W1029
if t.Valid {
return json.Marshal(t.NullTime.Time)
}
return nullJsonBytes, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (t *Time) UnmarshalJSON(data []byte) error { // skipcq: GO-W1029
if len(data) == 0 || string(data) == nullJson {
t.NullTime.Time = time.Time{}
t.NullTime.Valid = false
return nil
}
var v time.Time
err := json.Unmarshal(data, &v)
if err != nil {
return err
}
t.NullTime.Time = v
t.NullTime.Valid = true
return nil
}