将 LocalDateTime 迁移到 orm
This commit is contained in:
@@ -1,11 +1,5 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"time"
|
||||
)
|
||||
|
||||
// region page
|
||||
|
||||
type PageReq struct {
|
||||
@@ -46,85 +40,3 @@ type PageResp struct {
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region LocalDateTime
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
Reference in New Issue
Block a user