optimize DateTime scan and marshalization
This commit is contained in:
parent
869d1cbcf7
commit
68a9782c03
|
@ -53,7 +53,7 @@ func (d DateTime) String() string {
|
||||||
|
|
||||||
// MarshalJSON implements the [json.Marshaler] interface.
|
// MarshalJSON implements the [json.Marshaler] interface.
|
||||||
func (d DateTime) MarshalJSON() ([]byte, error) {
|
func (d DateTime) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(d.String())
|
return []byte(`"` + d.String() + `"`), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON implements the [json.Unmarshaler] interface.
|
// UnmarshalJSON implements the [json.Unmarshaler] interface.
|
||||||
|
@ -80,6 +80,13 @@ func (d *DateTime) Scan(value any) error {
|
||||||
d.t = v
|
d.t = v
|
||||||
case int:
|
case int:
|
||||||
d.t = cast.ToTime(v)
|
d.t = cast.ToTime(v)
|
||||||
|
case string:
|
||||||
|
t, err := time.Parse(DefaultDateLayout, v)
|
||||||
|
if err != nil {
|
||||||
|
// check for other common date layouts
|
||||||
|
t = cast.ToTime(v)
|
||||||
|
}
|
||||||
|
d.t = t
|
||||||
default:
|
default:
|
||||||
str := cast.ToString(v)
|
str := cast.ToString(v)
|
||||||
if str == "" {
|
if str == "" {
|
||||||
|
|
Loading…
Reference in New Issue