1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package types
- import (
- "database/sql/driver"
- "errors"
- "time"
- )
- // DateText is an implementation of a string for the MySQL type date.
- type DateText string
- // TimeText is an implementation of a string for the MySQL type datetime.
- type TimeText string
- const (
- formtDate = `2006-01-02`
- formtTime = `2006-01-02 15:04:05`
- )
- // Value implements the driver.Valuer interface,
- // and turns the date into a DateText (date) for MySQL storage.
- func (d DateText) Value() (driver.Value, error) {
- t, err := time.Parse(formtDate, string(d))
- if err != nil {
- return nil, err
- }
- return DateText(t.Format(formtDate)), nil
- }
- // Scan implements the sql.Scanner interface,
- // and turns the bitfield incoming from MySQL into a Date
- func (d *DateText) Scan(src interface{}) error {
- v, ok := src.([]byte)
- if !ok {
- return errors.New("bad []byte type assertion")
- }
- t, err := time.Parse(formtDate, string(v))
- if err != nil {
- return err
- }
- *d = DateText(t.Format(formtDate))
- return nil
- }
- // Value implements the driver.Valuer interface,
- // and turns the date into a DateText (date) for MySQL storage.
- func (d TimeText) Value() (driver.Value, error) {
- t, err := time.Parse(formtDate, string(d))
- if err != nil {
- return nil, err
- }
- return TimeText(t.Format(formtTime)), nil
- }
- // Scan implements the sql.Scanner interface,
- // and turns the bitfield incoming from MySQL into a Date
- func (d *TimeText) Scan(src interface{}) error {
- v, ok := src.([]byte)
- if !ok {
- return errors.New("bad []byte type assertion")
- }
- t, err := time.Parse(formtDate, string(v))
- if err != nil {
- return err
- }
- *d = TimeText(t.Format(formtTime))
- return nil
- }
|