@@ -13,31 +13,37 @@ import (
13
13
)
14
14
15
15
// UnixMilli is a nullable millisecond UNIX timestamp in databases and JSON.
16
- type UnixMilli time.Time
16
+ type UnixMilli struct {
17
+ sql.NullInt64
18
+ }
17
19
18
20
// Time returns the time.Time conversion of UnixMilli.
19
21
func (t UnixMilli ) Time () time.Time {
20
- return time . Time ( t )
22
+ return utils . FromUnixMilli ( t . Int64 )
21
23
}
22
24
23
25
// MarshalJSON implements the json.Marshaler interface.
24
26
// Marshals to milliseconds. Supports JSON null.
25
27
func (t UnixMilli ) MarshalJSON () ([]byte , error ) {
26
- if time . Time ( t ). IsZero () {
28
+ if ! t . Valid {
27
29
return nil , nil
28
30
}
29
31
30
- return []byte (strconv .FormatInt (utils . UnixMilli ( time . Time ( t )) , 10 )), nil
32
+ return []byte (strconv .FormatInt (t . Int64 , 10 )), nil
31
33
}
32
34
33
35
// UnmarshalText implements the encoding.TextUnmarshaler interface.
34
36
func (t * UnixMilli ) UnmarshalText (text []byte ) error {
35
- parsed , err := strconv .ParseFloat (string (text ), 64 )
37
+ ms , err := strconv .ParseFloat (string (text ), 64 )
36
38
if err != nil {
37
39
return internal .CantParseFloat64 (err , string (text ))
38
40
}
39
41
40
- * t = UnixMilli (utils .FromUnixMilli (int64 (parsed )))
42
+ * t = UnixMilli {sql.NullInt64 {
43
+ Int64 : int64 (ms ),
44
+ Valid : true ,
45
+ }}
46
+
41
47
return nil
42
48
}
43
49
@@ -52,8 +58,11 @@ func (t *UnixMilli) UnmarshalJSON(data []byte) error {
52
58
if err != nil {
53
59
return internal .CantParseFloat64 (err , string (data ))
54
60
}
55
- tt := utils .FromUnixMilli (int64 (ms ))
56
- * t = UnixMilli (tt )
61
+
62
+ * t = UnixMilli {sql.NullInt64 {
63
+ Int64 : int64 (ms ),
64
+ Valid : true ,
65
+ }}
57
66
58
67
return nil
59
68
}
@@ -65,24 +74,27 @@ func (t *UnixMilli) Scan(src interface{}) error {
65
74
return nil
66
75
}
67
76
68
- v , ok := src .(int64 )
77
+ ms , ok := src .(int64 )
69
78
if ! ok {
70
79
return errors .Errorf ("bad int64 type assertion from %#v" , src )
71
80
}
72
- tt := utils .FromUnixMilli (v )
73
- * t = UnixMilli (tt )
81
+
82
+ * t = UnixMilli {sql.NullInt64 {
83
+ Int64 : ms ,
84
+ Valid : true ,
85
+ }}
74
86
75
87
return nil
76
88
}
77
89
78
90
// Value implements the driver.Valuer interface.
79
91
// Returns milliseconds. Supports SQL NULL.
80
92
func (t UnixMilli ) Value () (driver.Value , error ) {
81
- if t . Time (). IsZero () {
93
+ if ! t . Valid {
82
94
return nil , nil
83
95
}
84
96
85
- return utils . UnixMilli ( t . Time ()) , nil
97
+ return t . Int64 , nil
86
98
}
87
99
88
100
// Assert interface compliance.
0 commit comments