Repository navigation
Expand file tree
/
Copy pathtime.go
More file actions
96 lines (82 loc) · 1.94 KB
/
Copy pathtime.go
File metadata and controls
96 lines (82 loc) · 1.94 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
package nullable
// Do not modify. Generated by nullable-generate.
import (
"bytes"
"database/sql/driver"
"encoding/json"
"time"
)
// Time represents a time.Time value that may be null.
// This type implements the Scanner interface so it
// can be used as a scan destination, similar to NullString.
// It also implements the necessary interfaces to serialize
// to and from JSON.
type Time struct {
Time time.Time
Valid bool
}
// TimeFromPtr returns a Time whose value matches ptr.
func TimeFromPtr(ptr *time.Time) Time {
var v Time
return v.Assign(ptr)
}
// Assign the value of the pointer. If the pointer is nil,
// then then Valid is false, otherwise Valid is true.
func (tm *Time) Assign(ptr *time.Time) Time {
if ptr == nil {
tm.Valid = false
tm.Time = time.Time{}
} else {
tm.Valid = true
tm.Time = *ptr
}
return *tm
}
// Ptr returns a pointer to time.Time. If Valid is false
// then the pointer is nil, otherwise it is non-nil.
func (tm Time) Ptr() *time.Time {
if tm.Valid {
v := tm.Time
return &v
}
return nil
}
// Normalized returns a Time that can be compared with
// another Time for equality.
func (tm Time) Normalized() Time {
if tm.Valid {
return tm
}
// If !Valid, then Time could be any value.
// Normalized value can be compared for equality.
return Time{}
}
// Value implements the driver.Valuer interface.
func (tm Time) Value() (driver.Value, error) {
if !tm.Valid {
return nil, nil
}
return tm.Time, nil
}
// MarshalJSON implements the json.Marshaler interface.
func (tm Time) MarshalJSON() ([]byte, error) {
if tm.Valid {
return json.Marshal(tm.Time)
}
return []byte("null"), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (tm *Time) UnmarshalJSON(p []byte) error {
if bytes.Equal(p, jsonNull) {
tm.Time = time.Time{}
tm.Valid = false
return nil
}
var v time.Time
if err := json.Unmarshal(p, &v); err != nil {
return err
}
tm.Time = v
tm.Valid = true
return nil
}