-
Notifications
You must be signed in to change notification settings - Fork 5
/
time.go
55 lines (49 loc) · 1.05 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
package sql
import (
"database/sql"
"database/sql/driver"
"fmt"
"github.com/small-ek/antgo/utils/str"
"time"
)
// Time 时间类型
type Time struct {
sql.NullTime
}
// MarshalJSON 序列化为JSON
func (t Time) MarshalJSON() ([]byte, error) {
if t.Time.IsZero() {
return []byte("\"\""), nil
}
stamp := fmt.Sprintf("\"%s\"", t.Time.Format("2006-01-02 15:04:05"))
return []byte(stamp), nil
}
// UnmarshalJSON 反序列化为JSON
func (t *Time) UnmarshalJSON(data []byte) error {
times := str.ClearQuotes(string(data))
if times != "" {
var err error
t.Time, err = time.Parse("2006-01-02 15:04:05", times)
if err != nil {
panic(err)
}
}
return nil
}
// Scan 读取插入
func (t *Time) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*t = Time{sql.NullTime{Time: value}}
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}
// Value 插入
func (t Time) Value() (driver.Value, error) {
strs := t.Time.Format("2006-01-02 15:04:05")
if strs == "0001-01-01 00:00:00" {
return nil, nil
}
return strs, nil
}