kefu/types/time.go

32 lines
584 B
Go
Raw Permalink Normal View History

2024-12-10 02:50:12 +00:00
package types
import (
"database/sql/driver"
"fmt"
"time"
)
type Time struct {
time.Time
}
func (t Time) MarshalJSON() ([]byte, error) {
localTime := t.Format("2006-01-02 15:04:05")
return []byte(fmt.Sprintf(`"%s"`, localTime)), nil
}
func (t Time) Value() (driver.Value, error) {
var zeroTime time.Time
if t.Time.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return t.Time, nil
}
func (t *Time) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*t = Time{Time: value}
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}