saasitone/pkg/db/time.go

38 lines
559 B
Go
Raw Permalink Normal View History

2024-07-11 21:09:15 -07:00
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
}