86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
package orm
|
|
|
|
import (
|
|
"database/sql"
|
|
"database/sql/driver"
|
|
"time"
|
|
)
|
|
|
|
type LocalDateTime time.Time
|
|
|
|
var formats = []string{
|
|
"2006-01-02 15:04:05.999999999-07:00",
|
|
"2006-01-02T15:04:05.999999999-07:00",
|
|
"2006-01-02 15:04:05.999999999",
|
|
"2006-01-02T15:04:05.999999999",
|
|
"2006-01-02 15:04:05",
|
|
"2006-01-02T15:04:05",
|
|
"2006-01-02 15:04",
|
|
"2006-01-02T15:04",
|
|
"2006-01-02",
|
|
}
|
|
|
|
//goland:noinspection GoMixedReceiverTypes
|
|
func (ldt *LocalDateTime) Scan(value interface{}) (err error) {
|
|
var t time.Time
|
|
|
|
if strValue, ok := value.(string); ok {
|
|
var timeValue time.Time
|
|
for _, format := range formats {
|
|
timeValue, err = time.Parse(format, strValue)
|
|
if err == nil {
|
|
t = timeValue
|
|
break
|
|
}
|
|
}
|
|
t = timeValue
|
|
} else {
|
|
nullTime := &sql.NullTime{}
|
|
err = nullTime.Scan(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if nullTime == nil {
|
|
return nil
|
|
}
|
|
t = nullTime.Time
|
|
}
|
|
*ldt = LocalDateTime(time.Date(
|
|
t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.Local,
|
|
))
|
|
return
|
|
}
|
|
|
|
//goland:noinspection GoMixedReceiverTypes
|
|
func (ldt LocalDateTime) Value() (driver.Value, error) {
|
|
return time.Time(ldt).Local(), nil
|
|
}
|
|
|
|
// GormDataType gorm common data type
|
|
//
|
|
//goland:noinspection GoMixedReceiverTy
|
|
//goland:noinspection GoMixedReceiverTypes
|
|
func (ldt LocalDateTime) GormDataType() string {
|
|
return "ldt"
|
|
}
|
|
|
|
//goland:noinspection GoMixedReceiverTypes
|
|
func (ldt LocalDateTime) GobEncode() ([]byte, error) {
|
|
return time.Time(ldt).GobEncode()
|
|
}
|
|
|
|
//goland:noinspection GoMixedReceiverTypes
|
|
func (ldt *LocalDateTime) GobDecode(b []byte) error {
|
|
return (*time.Time)(ldt).GobDecode(b)
|
|
}
|
|
|
|
//goland:noinspection GoMixedReceiverTypes
|
|
func (ldt LocalDateTime) MarshalJSON() ([]byte, error) {
|
|
return time.Time(ldt).MarshalJSON()
|
|
}
|
|
|
|
//goland:noinspection GoMixedReceiverTypes
|
|
func (ldt *LocalDateTime) UnmarshalJSON(b []byte) error {
|
|
return (*time.Time)(ldt).UnmarshalJSON(b)
|
|
}
|