51 lines
909 B
Go
51 lines
909 B
Go
package domain
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type CustomDate struct {
|
|
time.Time
|
|
}
|
|
|
|
func (ct *CustomDate) UnmarshalJSON(data []byte) error {
|
|
str := string(data[1 : len(data)-1])
|
|
|
|
t, err := time.Parse(time.DateOnly, str)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ct.Time = t
|
|
return nil
|
|
}
|
|
|
|
func (ct CustomDate) MarshalJSON() ([]byte, error) {
|
|
return []byte(fmt.Sprintf("\"%s\"", ct.Format(time.DateOnly))), nil
|
|
}
|
|
|
|
func (ct CustomDate) String() string {
|
|
return ct.Format(time.DateOnly)
|
|
}
|
|
|
|
func (ct *CustomDate) Scan(value any) error {
|
|
switch v := value.(type) {
|
|
case string:
|
|
t, err := time.Parse(time.DateOnly, v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*ct = CustomDate{Time: t}
|
|
case time.Time:
|
|
*ct = CustomDate{Time: v}
|
|
default:
|
|
panic("incorrect time type received")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (ct CustomDate) Value() (driver.Value, error) {
|
|
return ct.Time.Format(time.DateOnly), nil
|
|
}
|