carbon/database.go

36 lines
867 B
Go
Raw Normal View History

2020-09-14 10:15:23 +08:00
package carbon
import (
2020-10-12 09:45:17 +08:00
"database/sql/driver"
2020-09-14 10:15:23 +08:00
"fmt"
2020-10-12 09:45:17 +08:00
"time"
2020-09-14 10:15:23 +08:00
)
2021-07-24 01:03:39 +08:00
// Scan an interface used by Scan in package database/sql for Scanning value from database to local golang variable.
2020-10-12 09:45:17 +08:00
func (c *Carbon) Scan(v interface{}) error {
2024-01-07 20:04:24 +08:00
if value, ok := v.(time.Time); ok {
loc, err := getLocationByTimezone(defaultTimezone)
if c.loc != nil {
loc = c.loc
}
*c = CreateFromStdTime(value)
2024-01-07 20:04:24 +08:00
c.loc, c.Error = loc, err
2020-10-12 09:45:17 +08:00
return nil
}
2021-08-06 10:19:12 +08:00
return fmt.Errorf("can not convert %v to carbon", v)
2020-10-12 09:45:17 +08:00
}
2020-10-12 09:51:16 +08:00
2021-08-10 10:57:22 +08:00
// Value the interface providing the Value method for package database/sql/driver.
2020-10-12 09:45:17 +08:00
func (c Carbon) Value() (driver.Value, error) {
2021-04-07 12:03:31 +08:00
if c.IsZero() {
2020-10-12 09:45:17 +08:00
return nil, nil
}
return c.ToStdTime(), nil
}
// GormDataType implements the interface GormDataTypeInterface for Carbon struct.
// 实现 GormDataTypeInterface 接口
func (c Carbon) GormDataType() string {
return "time"
2020-10-12 09:45:17 +08:00
}