39 lines
747 B
Go
39 lines
747 B
Go
package utils
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/anxpp/common-utils/str"
|
|
)
|
|
|
|
type Time struct {
|
|
time.Time
|
|
}
|
|
|
|
func (t *Time) UnmarshalJSON(data []byte) (e error) {
|
|
t.Time, e = time.ParseInLocation(str.TimeLayout, string(data), time.Local)
|
|
return nil
|
|
}
|
|
|
|
func (t Time) MarshalJSON() ([]byte, error) {
|
|
return ([]byte)(fmt.Sprintf("\"%s\"", t.Time.Format(str.TimeLayout))), nil
|
|
}
|
|
func (t *Time) Scan(value any) error {
|
|
bytes, _ := value.([]byte)
|
|
tt, _ := time.ParseInLocation(str.TimeLayout, string(bytes), time.Local)
|
|
*t = Time{
|
|
Time: tt,
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (t Time) Value() (driver.Value, error) {
|
|
return t.Time.Format(str.TimeLayout), nil
|
|
}
|
|
|
|
func (t Time) String() string {
|
|
return str.TimeToString(t.Time)
|
|
}
|