forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
35 lines (31 loc) · 867 Bytes
/
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
package carbon
import (
"database/sql/driver"
"fmt"
"time"
)
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
func (c *Carbon) Scan(v interface{}) error {
if value, ok := v.(time.Time); ok {
loc, err := getLocationByTimezone(defaultTimezone)
if c.loc != nil {
loc = c.loc
}
*c = CreateFromStdTime(value)
c.loc, c.Error = loc, err
return nil
}
return fmt.Errorf("can not convert %v to carbon", v)
}
// Value the interface providing the Value method for package database/sql/driver.
func (c Carbon) Value() (driver.Value, error) {
if c.IsZero() {
return nil, nil
}
return c.ToStdTime(), nil
}
// GormDataType implements the interface GormDataTypeInterface for Carbon struct.
// 实现 GormDataTypeInterface 接口
func (c Carbon) GormDataType() string {
return "time"
}