package global
import (
"time"
)
//自定义gorm.Model
type GORM_MODEL struct {
ID uint `gorm:"primary_key"`
CreatedAt MyTime `gorm:"comment:'创建时间';type:datetime;"`
UpdatedAt MyTime `gorm:"comment:'修改时间';type:datetime;"`
//CreatedAt time.Time
//UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
}
package global
import (
"database/sql/driver"
"errors"
"fmt"
"strings"
"time"
)
//MyTime 自定义时间
type MyTime time.Time
func (t *MyTime) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
var err error
//前端接收的时间字符串
str := string(data)
//去除接收的str收尾多余的"
timeStr := strings.Trim(str, "\"")
t1, err := time.Parse("2006-01-02 15:04:05", timeStr)
*t = MyTime(t1)
return err
}
func (t MyTime) MarshalJSON() ([]byte, error) {
formatted := fmt.Sprintf("\"%v\"", time.Time(t).Format("2006-01-02 15:04:05"))
return []byte(formatted), nil
}
func (t MyTime) Value() (driver.Value, error) {
// MyTime 转换成 time.Time 类型
tTime := time.Time(t)
return tTime.Format("2006-01-02 15:04:05"), nil
}
func (t *MyTime) Scan(v interface{}) error {
switch vt := v.(type) {
case time.Time:
// 字符串转成 time.Time 类型
*t = MyTime(vt)
default:
return errors.New("类型处理错误")
}
return nil
}
func (t *MyTime) String() string {
return fmt.Sprintf("hhh:%s", time.Time(*t).String())
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)