Files
servicebase/pkg/utils/time_tool.go
2025-11-18 17:48:20 +08:00

38 lines
754 B
Go

package utils
import (
"database/sql/driver"
"fmt"
"github.com/anxpp/common-utils/str"
"time"
)
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 interface{}) 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)
}