38 lines
559 B
Go
38 lines
559 B
Go
|
package db
|
||
|
|
||
|
import (
|
||
|
"database/sql/driver"
|
||
|
"errors"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const timeFormat = "2006-01-02 15:04:05"
|
||
|
|
||
|
type Time struct {
|
||
|
time.Time
|
||
|
}
|
||
|
|
||
|
func (t *Time) Scan(value any) error {
|
||
|
valueStr := ""
|
||
|
switch s := value.(type) {
|
||
|
case string:
|
||
|
valueStr = s
|
||
|
case []byte:
|
||
|
valueStr = string(s)
|
||
|
default:
|
||
|
return errors.New("Cannot scan value into Time")
|
||
|
}
|
||
|
|
||
|
parsedTime, err := time.Parse(timeFormat, valueStr)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
t.Time = parsedTime
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (t Time) Value() (driver.Value, error) {
|
||
|
return t.Format(timeFormat), nil
|
||
|
}
|