forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
93 lines (73 loc) · 1.89 KB
/
database.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package carbon
import (
"database/sql/driver"
"fmt"
"time"
)
type ToDateTimeString struct {
Carbon
}
type ToDateString struct {
Carbon
}
type ToTimeString struct {
Carbon
}
type ToTimestamp struct {
Carbon
}
type ToTimestampWithSecond struct {
Carbon
}
type ToTimestampWithMillisecond struct {
Carbon
}
type ToTimestampWithMicrosecond struct {
Carbon
}
type ToTimestampWithNanosecond struct {
Carbon
}
func (c *Carbon) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*c = Carbon{Time: value}
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}
func (c Carbon) Value() (driver.Value, error) {
var zeroTime time.Time
var timeTime = c.Time
if timeTime.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return timeTime, nil
}
func (c Carbon) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, c.ToDateTimeString())), nil
}
func (c ToDateTimeString) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, c.ToDateTimeString())), nil
}
func (c ToDateString) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, c.ToDateString())), nil
}
func (c ToTimeString) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, c.ToTimeString())), nil
}
func (c ToTimestamp) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, c.ToTimestamp())), nil
}
func (c ToTimestampWithSecond) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithSecond())), nil
}
func (c ToTimestampWithMillisecond) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMillisecond())), nil
}
func (c ToTimestampWithMicrosecond) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMicrosecond())), nil
}
func (c ToTimestampWithNanosecond) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithNanosecond())), nil
}