carbon/database.go

26 lines
573 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 {
value, ok := v.(time.Time)
if ok {
*c = Carbon{time: value, loc: time.Local}
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.time, nil
2020-10-12 09:45:17 +08:00
}