-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
74 lines (65 loc) · 1.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
package type2json
import (
"database/sql/driver"
"regexp"
"strconv"
"time"
)
type Time time.Time
// sql driver exec
func (this *Time) Value() (driver.Value, error) {
return this.GetValue(), nil
}
// sql driver scan
func (this *Time) Scan(v interface{}) error {
switch buf := v.(type) {
case time.Time:
this.SetValue(buf)
default:
return ErrType
}
return nil
}
// 赋值
func (this *Time) SetValue(v time.Time) {
*this = Time(v)
}
// 取值
func (this *Time) GetValue() time.Time {
return time.Time(*this)
}
// 生成json
func (this *Time) MarshalJSON() ([]byte, error) {
stamp := strconv.Quote(timetoa(this.GetValue()))
return []byte(stamp), nil
}
// 解析json
func (this *Time) UnmarshalJSON(szData []byte) error {
strData := string(szData)
// 判断是否为null
if strData == "null" {
return nil
}
// 去掉双引号
t, err := strconv.Unquote(strData)
if err != nil {
t = strData
}
// 正则判断是否为数字
bNumber, err := regexp.MatchString("^\\d+$", t)
if err != nil {
return err
}
if !bNumber {
v, err := atotime(t)
this.SetValue(v)
return err
}
i64, err := atoi64(t)
if err != nil {
return err
}
v, err := i64totime(i64)
this.SetValue(v)
return err
}